From 5434cc15ceb84e9f37963cfd9a8401e69ba606fa Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Tue, 2 Aug 2022 20:30:46 +0000 Subject: [PATCH] feat(init): Continue Init with directory creation --- cmd/init.go | 28 ++++++++++++++++++++++++++-- cmd/upgrade.go | 2 +- helpers/constant.go | 1 + helpers/dependencies/golang.go | 2 +- helpers/file.go | 13 +++++++------ helpers/input.go | 2 +- 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 30329e5..8915208 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -6,6 +6,7 @@ package cmd import ( "os" + "path/filepath" "strings" "github.com/juju/errors" @@ -140,9 +141,32 @@ func runInitAction(cmd *cobra.Command, args []string) { return } + projectDirPath := filepath.Join(currentPath, userInput.ProjectDirectory) + + log.Debugf("Our project directory path is: %s", projectDirPath) + + // create project directory + log.Debug("Creating project directory") + + dirCreated, err := helpers.CheckAndCreateDir(projectDirPath) + if err != nil { + log.Errorf("Fail to create project directory. The following error happen: %s", err.Error()) + return + } + + if !dirCreated { + log.Error("A directory already exist with your project directory name") + return + } + + // move to project directory + err = os.Chdir(projectDirPath) + if err != nil { + log.Errorf("Fail to move to project directory. The following error happen: %s", err.Error()) + return + } + // XXX: - // Create directory - // Move to dir // git init // create .gouick // move templated file diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 6f3197e..c218b21 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -89,7 +89,7 @@ func runUpgradeAction(cmd *cobra.Command, args []string) { log.Debug("Checking directory and creating it if needed") // check if the path is ok, else ask user for new path if we are in a interactive mode, else stop - err = helpers.CheckAndCreateDir(installDirectory) + _, err = helpers.CheckAndCreateDir(installDirectory) if err != nil { log.Errorf("Impossible to create the instalation directory for %s", dependency.GetName()) if acceptAll { diff --git a/helpers/constant.go b/helpers/constant.go index 95d3bf3..c22464d 100644 --- a/helpers/constant.go +++ b/helpers/constant.go @@ -1,3 +1,4 @@ +// Package helpers contains all basic function that might make development easier / avoid code duplication package helpers const configFile = ".gouick.yaml" diff --git a/helpers/dependencies/golang.go b/helpers/dependencies/golang.go index 8926ea3..07953f3 100644 --- a/helpers/dependencies/golang.go +++ b/helpers/dependencies/golang.go @@ -284,7 +284,7 @@ func (g Golang) PostInstall(path string) error { log.Debug("creating gopath directory") - err = helpers.CheckAndCreateDir(filepath.Join(homeDir, "go")) + _, err = helpers.CheckAndCreateDir(filepath.Join(homeDir, "go")) if err != nil { return errors.Trace(err) } diff --git a/helpers/file.go b/helpers/file.go index 5b8f054..82e69c1 100644 --- a/helpers/file.go +++ b/helpers/file.go @@ -101,27 +101,28 @@ func createDirectory(path string) error { } // CheckAndCreateDir will check if path is writable and create directory if needed -func CheckAndCreateDir(path string) error { +// return a bool true if directory created, false if not +func CheckAndCreateDir(path string) (bool, error) { dirWritable, err := isDirectoryWritable(path) if dirWritable { - return nil + return false, nil } else if err != nil && !errors.Is(err, errors.NotFound) { log.Warnf("impossible to check if the directory is writable") - return err + return false, err } else if err == nil && !dirWritable { log.Warnf("directory is not writable") - return errors.Forbiddenf("directory is not writable") + return false, errors.Forbiddenf("directory is not writable") } err = createDirectory(path) if err != nil { log.Warnf("impossible to create directory (%s), please try again", err.Error()) - return err + return false, err } - return nil + return true, nil } func RemoveDirectoryContent(path string) error { diff --git a/helpers/input.go b/helpers/input.go index df3957e..3922fea 100644 --- a/helpers/input.go +++ b/helpers/input.go @@ -93,7 +93,7 @@ func PathInput() string { continue } - err := CheckAndCreateDir(userInput) + _, err := CheckAndCreateDir(userInput) if err != nil { log.Warnf("please, try again") continue