feat(init): Continue Init with directory creation
This commit is contained in:
parent
cc0e94690e
commit
5434cc15ce
28
cmd/init.go
28
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
|
||||
|
@ -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 {
|
||||
|
@ -1,3 +1,4 @@
|
||||
// Package helpers contains all basic function that might make development easier / avoid code duplication
|
||||
package helpers
|
||||
|
||||
const configFile = ".gouick.yaml"
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -93,7 +93,7 @@ func PathInput() string {
|
||||
continue
|
||||
}
|
||||
|
||||
err := CheckAndCreateDir(userInput)
|
||||
_, err := CheckAndCreateDir(userInput)
|
||||
if err != nil {
|
||||
log.Warnf("please, try again")
|
||||
continue
|
||||
|
Loading…
Reference in New Issue
Block a user