From 3ed1ebaf7d88ec463f47e15bef0f81b21c769d25 Mon Sep 17 00:00:00 2001 From: Andrew Obuchowicz Date: Tue, 17 Oct 2023 15:14:27 -0400 Subject: [PATCH] Move GetClonePath() to /pkg/library/projects/ Signed-off-by: Andrew Obuchowicz --- pkg/library/projects/clone.go | 8 ++++++++ project-clone/internal/devfile.go | 8 -------- project-clone/internal/git/setup.go | 9 +++++---- project-clone/internal/utils.go | 5 +++-- project-clone/internal/zip/setup.go | 3 ++- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/library/projects/clone.go b/pkg/library/projects/clone.go index 48130c66b..ab3b9c35b 100644 --- a/pkg/library/projects/clone.go +++ b/pkg/library/projects/clone.go @@ -115,6 +115,14 @@ func GetStarterProject(workspace *dw.DevWorkspaceTemplateSpec) (*dw.StarterProje return nil, fmt.Errorf("selected starter project %s not found in workspace starterProjects", selectedStarterProject) } +// GetClonePath gets the correct clonePath for a project, given the semantics in devfile/api +func GetClonePath(project *dw.Project) string { + if project.ClonePath != "" { + return project.ClonePath + } + return project.Name +} + func hasContainerComponents(workspace *dw.DevWorkspaceTemplateSpec) bool { for _, component := range workspace.Components { if component.Container != nil { diff --git a/project-clone/internal/devfile.go b/project-clone/internal/devfile.go index 1133d18c1..956a99462 100644 --- a/project-clone/internal/devfile.go +++ b/project-clone/internal/devfile.go @@ -32,14 +32,6 @@ const ( ProjectSubDir = "subDir" ) -// GetClonePath gets the correct clonePath for a project, given the semantics in devfile/api -func GetClonePath(project *dw.Project) string { - if project.ClonePath != "" { - return project.ClonePath - } - return project.Name -} - // ReadFlattenedDevWorkspace reads the flattened DevWorkspaceTemplateSpec from disk. The location of the flattened // yaml is determined from the DevWorkspace Operator-provisioned environment variable. func ReadFlattenedDevWorkspace() (*dw.DevWorkspaceTemplateSpec, error) { diff --git a/project-clone/internal/git/setup.go b/project-clone/internal/git/setup.go index 4d824914b..a3052e52e 100644 --- a/project-clone/internal/git/setup.go +++ b/project-clone/internal/git/setup.go @@ -22,6 +22,7 @@ import ( "path" dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + projectslib "github.com/devfile/devworkspace-operator/pkg/library/projects" "github.com/devfile/devworkspace-operator/project-clone/internal" ) @@ -44,7 +45,7 @@ func SetupGitProject(project dw.Project) error { func doInitialGitClone(project *dw.Project) error { // Clone into a temp dir and then move set up project to PROJECTS_ROOT to try and make clone atomic in case // project-clone container is terminated - tmpClonePath := path.Join(internal.CloneTmpDir, internal.GetClonePath(project)) + tmpClonePath := path.Join(internal.CloneTmpDir, projectslib.GetClonePath(project)) err := CloneProject(project, tmpClonePath) if err != nil { return fmt.Errorf("failed to clone project: %s", err) @@ -83,7 +84,7 @@ func doInitialGitClone(project *dw.Project) error { } func setupRemotesForExistingProject(project *dw.Project) error { - projectPath := path.Join(internal.ProjectsRoot, internal.GetClonePath(project)) + projectPath := path.Join(internal.ProjectsRoot, projectslib.GetClonePath(project)) repo, err := internal.OpenRepo(projectPath) if err != nil { return fmt.Errorf("failed to open existing project in filesystem: %s", err) @@ -105,13 +106,13 @@ func copyProjectFromTmpDir(project *dw.Project, tmpClonePath string) error { return fmt.Errorf("failed to process subDir on project: %w", err) } subDirPath := path.Join(tmpClonePath, subDirSubPath) - projectPath := path.Join(internal.ProjectsRoot, internal.GetClonePath(project)) + projectPath := path.Join(internal.ProjectsRoot, projectslib.GetClonePath(project)) log.Printf("Moving subdirectory %s in project %s from temporary directory to %s", subDirSubPath, project.Name, projectPath) if err := os.Rename(subDirPath, projectPath); err != nil { return fmt.Errorf("failed to move subdirectory of cloned project to %s: %w", internal.ProjectsRoot, err) } } else { - projectPath := path.Join(internal.ProjectsRoot, internal.GetClonePath(project)) + projectPath := path.Join(internal.ProjectsRoot, projectslib.GetClonePath(project)) log.Printf("Moving cloned project %s from temporary directory %s to %s", project.Name, tmpClonePath, projectPath) if err := os.Rename(tmpClonePath, projectPath); err != nil { return fmt.Errorf("failed to move cloned project to %s: %w", internal.ProjectsRoot, err) diff --git a/project-clone/internal/utils.go b/project-clone/internal/utils.go index 8b1698cc1..fc150b5bd 100644 --- a/project-clone/internal/utils.go +++ b/project-clone/internal/utils.go @@ -23,6 +23,7 @@ import ( "path/filepath" dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + projectslib "github.com/devfile/devworkspace-operator/pkg/library/projects" "github.com/go-git/go-git/v5" ) @@ -37,7 +38,7 @@ func CheckProjectState(project *dw.Project) (needClone, needRemotes bool, err er if project.Attributes.Exists(ProjectSubDir) { return checkSubPathProjectState(project) } - repo, err := OpenRepo(path.Join(ProjectsRoot, GetClonePath(project))) + repo, err := OpenRepo(path.Join(ProjectsRoot, projectslib.GetClonePath(project))) if err != nil { return false, false, err } @@ -105,7 +106,7 @@ func checkSubPathProjectState(project *dw.Project) (needClone, needRemotes bool, } // Result here won't be a git repository, so we only check whether the directory exists - clonePath := path.Join(ProjectsRoot, GetClonePath(project)) + clonePath := path.Join(ProjectsRoot, projectslib.GetClonePath(project)) stat, err := os.Stat(clonePath) if err != nil { if os.IsNotExist(err) { diff --git a/project-clone/internal/zip/setup.go b/project-clone/internal/zip/setup.go index 39e30e655..014dcd094 100644 --- a/project-clone/internal/zip/setup.go +++ b/project-clone/internal/zip/setup.go @@ -27,6 +27,7 @@ import ( "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + projectslib "github.com/devfile/devworkspace-operator/pkg/library/projects" "github.com/devfile/devworkspace-operator/project-clone/internal" ) @@ -40,7 +41,7 @@ func SetupZipProject(project v1alpha2.Project, httpClient *http.Client) error { return fmt.Errorf("project has no 'zip' source") } url := project.Zip.Location - clonePath := internal.GetClonePath(&project) + clonePath := projectslib.GetClonePath(&project) projectPath := path.Join(internal.ProjectsRoot, clonePath) if exists, err := internal.DirExists(projectPath); exists { // Assume project is already set up