Skip to content

Commit

Permalink
Move GetClonePath() to /pkg/library/projects/
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Obuchowicz <[email protected]>
  • Loading branch information
AObuchow committed Oct 19, 2023
1 parent b9044e1 commit 3ed1eba
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
8 changes: 8 additions & 0 deletions pkg/library/projects/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions project-clone/internal/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions project-clone/internal/git/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions project-clone/internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion project-clone/internal/zip/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down

0 comments on commit 3ed1eba

Please sign in to comment.