feat(init): Continue Init with directory creation

This commit is contained in:
Matthieu 'JP' DERASSE 2022-08-02 20:30:46 +00:00
parent cc0e94690e
commit 5434cc15ce
Signed by: mderasse
GPG Key ID: 55141C777B16A705
6 changed files with 37 additions and 11 deletions

View File

@ -6,6 +6,7 @@ package cmd
import ( import (
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/juju/errors" "github.com/juju/errors"
@ -140,9 +141,32 @@ func runInitAction(cmd *cobra.Command, args []string) {
return 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: // XXX:
// Create directory
// Move to dir
// git init // git init
// create .gouick // create .gouick
// move templated file // move templated file

View File

@ -89,7 +89,7 @@ func runUpgradeAction(cmd *cobra.Command, args []string) {
log.Debug("Checking directory and creating it if needed") 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 // 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 { if err != nil {
log.Errorf("Impossible to create the instalation directory for %s", dependency.GetName()) log.Errorf("Impossible to create the instalation directory for %s", dependency.GetName())
if acceptAll { if acceptAll {

View File

@ -1,3 +1,4 @@
// Package helpers contains all basic function that might make development easier / avoid code duplication
package helpers package helpers
const configFile = ".gouick.yaml" const configFile = ".gouick.yaml"

View File

@ -284,7 +284,7 @@ func (g Golang) PostInstall(path string) error {
log.Debug("creating gopath directory") log.Debug("creating gopath directory")
err = helpers.CheckAndCreateDir(filepath.Join(homeDir, "go")) _, err = helpers.CheckAndCreateDir(filepath.Join(homeDir, "go"))
if err != nil { if err != nil {
return errors.Trace(err) return errors.Trace(err)
} }

View File

@ -101,27 +101,28 @@ func createDirectory(path string) error {
} }
// CheckAndCreateDir will check if path is writable and create directory if needed // 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) dirWritable, err := isDirectoryWritable(path)
if dirWritable { if dirWritable {
return nil return false, nil
} else if err != nil && !errors.Is(err, errors.NotFound) { } else if err != nil && !errors.Is(err, errors.NotFound) {
log.Warnf("impossible to check if the directory is writable") log.Warnf("impossible to check if the directory is writable")
return err return false, err
} else if err == nil && !dirWritable { } else if err == nil && !dirWritable {
log.Warnf("directory is not writable") log.Warnf("directory is not writable")
return errors.Forbiddenf("directory is not writable") return false, errors.Forbiddenf("directory is not writable")
} }
err = createDirectory(path) err = createDirectory(path)
if err != nil { if err != nil {
log.Warnf("impossible to create directory (%s), please try again", err.Error()) 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 { func RemoveDirectoryContent(path string) error {

View File

@ -93,7 +93,7 @@ func PathInput() string {
continue continue
} }
err := CheckAndCreateDir(userInput) _, err := CheckAndCreateDir(userInput)
if err != nil { if err != nil {
log.Warnf("please, try again") log.Warnf("please, try again")
continue continue