Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #293 from jcockbain/1504-check-download-path
Browse files Browse the repository at this point in the history
1504 - check destination is empty before downloading project
  • Loading branch information
tobespc authored Dec 13, 2019
2 parents ec3d841 + a752e16 commit d3c8386
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
28 changes: 22 additions & 6 deletions pkg/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ type (
func DownloadTemplate(c *cli.Context) *ProjectError {
destination := c.Args().Get(0)

if destination == "" {
log.Fatal("destination not set")
}
checkProjectDirIsEmpty(destination)

projectDir := path.Base(destination)

Expand Down Expand Up @@ -139,7 +137,7 @@ func checkIsExtension(conID, projectPath string, c *cli.Context) (string, error)
func ValidateProject(c *cli.Context) *ProjectError {
projectPath := c.Args().Get(0)
conID := strings.TrimSpace(strings.ToLower(c.String("conid")))
checkProjectPath(projectPath)
checkProjectPathExists(projectPath)
validationStatus := "success"
// result could be ProjectType or string, so define as an interface
var validationResult interface{}
Expand Down Expand Up @@ -188,8 +186,26 @@ func writeCwSettingsIfNotInProject(conID string, projectPath string, BuildType s
}
}

// checkProjectPath will stop the process and return an error if path does not exist or is invalid
func checkProjectPath(projectPath string) {
// checkProjectDirIsEmpty stops the process if the given local filepath already exists, or is an empty string
func checkProjectDirIsEmpty(projectPath string) {
if projectPath == "" {
log.Fatal("destination not set")
}

// if the project dir already exists, continue if empty and exit if not
if utils.PathExists(projectPath) {
dirIsEmpty, err := utils.DirIsEmpty(projectPath)
if err != nil {
log.Fatal(err)
}
if !dirIsEmpty {
log.Fatal("Non empty project at given path")
}
}
}

// checkProjectPathExists stops the process if the given local filepath does not exist, or is an empty string
func checkProjectPathExists(projectPath string) {
if projectPath == "" {
log.Fatal("Project path not given")
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/utils/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ func PathExists(path string) bool {
return false
}

// DirIsEmpty returns true if the directory at the given path if empty
func DirIsEmpty(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()

_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}

// ReplaceInFiles the placeholder string "[PROJ_NAME_PLACEHOLDER]" with a generated name based on the project directory
func ReplaceInFiles(projectPath string, oldStr string, newStr string) error {

Expand Down

0 comments on commit d3c8386

Please sign in to comment.