Repository: dbt-labs/dbt-core

Issue:
[CT-918] `dbt clean` works in click



Discussion:

Resolving patch:
diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py
--- a/core/dbt/cli/main.py
+++ b/core/dbt/cli/main.py
@@ -10,9 +10,11 @@
 from dbt.config.runtime import load_project, load_profile
 from dbt.events.functions import setup_event_logger
 from dbt.profiler import profiler
+from dbt.tracking import initialize_from_flags, track_run
+
+from dbt.task.clean import CleanTask
 from dbt.task.deps import DepsTask
 from dbt.task.run import RunTask
-from dbt.tracking import initialize_from_flags, track_run
 
 
 def cli_runner():
@@ -141,7 +143,13 @@ def build(ctx, **kwargs):
 def clean(ctx, **kwargs):
     """Delete all folders in the clean-targets list (usually the dbt_packages and target directories.)"""
     flags = Flags()
-    click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {flags}")
+    project = ctx.obj["project"]
+
+    task = CleanTask(flags, project)
+
+    results = task.run()
+    success = task.interpret_results(results)
+    return results, success
 
 
 # dbt docs
diff --git a/core/dbt/task/clean.py b/core/dbt/task/clean.py
--- a/core/dbt/task/clean.py
+++ b/core/dbt/task/clean.py
@@ -1,9 +1,9 @@
 import os.path
 import os
 import shutil
+from typing import List
 
 from dbt import deprecations
-from dbt.task.base import BaseTask, move_to_nearest_project_dir
 from dbt.events.functions import fire_event
 from dbt.events.types import (
     CheckCleanPath,
@@ -11,27 +11,13 @@
     ProtectedCleanPath,
     FinishedCleanPaths,
 )
-from dbt.config import UnsetProfileConfig
+from dbt.task.base import (
+    BaseTask,
+    move_to_nearest_project_dir,
+)
 
 
 class CleanTask(BaseTask):
-    # Note: CleanTask is the last task that uses UnsetProfileConfig,
-    # and can be deleted once CleanTask no longer requires it.
-    ConfigType = UnsetProfileConfig
-
-    def __is_project_path(self, path):
-        proj_path = os.path.abspath(".")
-        return not os.path.commonprefix([proj_path, os.path.abspath(path)]) == proj_path
-
-    def __is_protected_path(self, path):
-        """
-        This function identifies protected paths, so as not to clean them.
-        """
-        abs_path = os.path.abspath(path)
-        protected_paths = self.config.model_paths + self.config.test_paths + ["."]
-        protected_abs_paths = [os.path.abspath(p) for p in protected_paths]
-        return abs_path in set(protected_abs_paths) or self.__is_project_path(abs_path)
-
     def run(self):
         """
         This function takes all the paths in the target file
@@ -39,16 +25,30 @@ def run(self):
         """
         move_to_nearest_project_dir(self.args.project_dir)
         if (
-            "dbt_modules" in self.config.clean_targets
+            "dbt_modules" in self.project.clean_targets
             and self.config.packages_install_path not in self.config.clean_targets
         ):
             deprecations.warn("install-packages-path")
-        for path in self.config.clean_targets:
+        for path in self.project.clean_targets:
             fire_event(CheckCleanPath(path=path))
-            if not self.__is_protected_path(path):
+            if not is_protected_path(path, self.project.model_paths, self.project.test_paths):
                 shutil.rmtree(path, True)
                 fire_event(ConfirmCleanPath(path=path))
             else:
                 fire_event(ProtectedCleanPath(path=path))
 
         fire_event(FinishedCleanPaths())
+
+
+def is_protected_path(path: str, model_paths: List[str], test_paths: List[str]) -> bool:
+    """This function identifies protected paths."""
+    abs_path = os.path.abspath(path)
+    protected_paths = model_paths + test_paths + ["."]
+    protected_abs_paths = [os.path.abspath(p) for p in protected_paths]
+    return abs_path in set(protected_abs_paths) or is_project_path(abs_path)
+
+
+def is_project_path(path: str) -> bool:
+    """This function identifies project paths."""
+    proj_path = os.path.abspath(".")
+    return not os.path.commonprefix([proj_path, os.path.abspath(path)]) == proj_path


Test patch: