diff --git a/README.md b/README.md index 87532bc..89a3b23 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -# Goguik (name TBD) \ No newline at end of file +# Goguik (name TBD) + +Only tested on Linux + +## Todo +- Implement for MacOS & Windows \ No newline at end of file diff --git a/cmd/generate.go b/cmd/generate.go index 28985dc..012505b 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -7,7 +7,6 @@ package cmd import ( "fmt" - "git.home.m-and-m.ovh/mderasse/boot/helpers" "github.com/spf13/cobra" ) @@ -19,14 +18,6 @@ var generateCmd = &cobra.Command{ You can also generate files for each 'modules' by using the command bellows`, Run: func(cmd *cobra.Command, args []string) { - if isStrap, err := helpers.IsStrapProject(); err != nil { - - fmt.Print("Fail to check if we are in a Strap Directory") - - } else if !isStrap { - fmt.Printf("You are not in a Strap Project directory\n") - return - } fmt.Println("generate called") }, } diff --git a/cmd/init.go b/cmd/init.go index 9b5ed6c..093cdcf 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -5,6 +5,9 @@ Copyright © 2022 Matthieu Derasse package cmd import ( + "os" + + "git.home.m-and-m.ovh/mderasse/boot/helpers" "github.com/spf13/cobra" log "github.com/sirupsen/logrus" @@ -28,20 +31,79 @@ func init() { rootCmd.AddCommand(initCmd) } +// runInitAction +// Steps: +// 1 - Check that we have the dependencies +// 2 - Check that we are not in project directory +// 3 - Ask info to the user func runInitAction(cmd *cobra.Command, args []string) { log.Debugf("Starting command Init") log.Debugf("Checking dependecies") - // isDependeciesOk, err := helpers.CheckDependencies() - // if err != nil { - // log.Error(err.Error()) - // return - // } else if isDependeciesOk { - // log.Error("Dependencies are not repescted") - // return - // } + if !checkDependencies() { + return + } + + // Get current path + currentPath, err := os.Getwd() + if err != nil { + log.Errorf("Fail to get current path. The following error happen: %s", err.Error()) + return + } + + log.Debugf("Working in directory: %s", currentPath) + + // Check if we are in goguik directory + log.Debug("Checking if we are in the same directory as Goguik") + + isGoguikDir, err := helpers.IsGoguikDirectory(currentPath) + if err != nil { + log.Errorf("Fail to check if we are in goguik directory. The following error happen: %s", err.Error()) + return + } + + if isGoguikDir { + log.Error("You cannot initialize a new project in the same dir than goguik\nGoguik should be added to your path") + return + } + + // Check if we are in a go project + log.Debug("Checking if we are in a golang project") + + isGoProject, err := helpers.IsGoProject(currentPath) + if err != nil { + log.Errorf("Fail to check if we are in a go project. The following error happen: %s", err.Error()) + return + } + + if isGoProject { + log.Error("You cannot initialize a new project in an already golang project") + return + } + + // Check if we are in a goguik project + log.Debug("Checking if we are in a goguik project") + + isGoguikProject, err := helpers.IsGoguikProject(currentPath) + if err != nil { + log.Errorf("Fail to check if we are in a goguik project. The following error happen: %s", err.Error()) + return + } + + if isGoguikProject { + log.Error("You cannot initialize a new project in an already goguik project") + return + } + + // XXX: + // Check we are in gopath or ask for the gomod name + // Create directory + // Move to dir + // git init + // create .goguik + // move templated file // if isGoProject, err := helpers.IsGoProject(); err != nil { // log.Error("An error occured when checking your directory.") diff --git a/cmd/root.go b/cmd/root.go index 15303d8..cffefcf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,6 +13,7 @@ import ( log "github.com/sirupsen/logrus" ) +var version = "0.0.1" var verbose = false var acceptAll = false var dependencyList = []dependencies.Dependency{ @@ -55,3 +56,24 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging") rootCmd.PersistentFlags().BoolVarP(&acceptAll, "yes", "y", false, "accept all change, disable interactive process") } + +// checkDependencies +func checkDependencies() bool { + + for _, dependency := range dependencyList { + + isSupported, err := dependency.IsVersionSupported() + if err != nil { + log.Errorf("Fail to check %s version. The following error happen: %s", dependency.GetName(), err.Error()) + return false + } + + if isSupported { + continue + } + + log.Infof("Dependency %s missing or not supported, please run the 'upgrade' command.", dependency.GetName()) + } + + return true +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..6a70275 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,27 @@ +/* +Copyright © 2022 Matthieu Derasse + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Show goguik version", + Run: runVersion, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} + +func runVersion(cmd *cobra.Command, args []string) { + // could probably have a look at runtime/debug + fmt.Printf("version: v%s\n", version) +} diff --git a/helpers/constant.go b/helpers/constant.go index 54a18b0..68e6a72 100644 --- a/helpers/constant.go +++ b/helpers/constant.go @@ -1,3 +1,3 @@ package helpers -const configFile = ".strap.yaml" +const configFile = ".goguik.yaml" diff --git a/helpers/dependencies/golang.go b/helpers/dependencies/golang.go index d6bde77..bfb9009 100644 --- a/helpers/dependencies/golang.go +++ b/helpers/dependencies/golang.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "regexp" "runtime" "strings" @@ -242,7 +243,7 @@ func (g Golang) PostInstall(path string) error { log.Debug("Adding env variable to .bashrc") fh, err := os.OpenFile( - fmt.Sprintf("%s.bashrc", homeDir), + filepath.Join(homeDir, ".bashrc"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644, ) @@ -263,7 +264,7 @@ func (g Golang) PostInstall(path string) error { log.Debug("creating gopath directory") - err = helpers.CheckAndCreateDir(fmt.Sprintf("%sgo", homeDir)) + err = helpers.CheckAndCreateDir(filepath.Join(homeDir, "go")) if err != nil { return errors.Trace(err) } @@ -289,7 +290,7 @@ func (g Golang) GetInstallDirectory() (string, error) { // concat default install dir with home and we have our path if !isInstalled { - return fmt.Sprintf("%s%s", homeDir, defaultGolangInstallDir), nil + return filepath.Join(homeDir, defaultGolangInstallDir), nil } // now let's play and find the current install path @@ -308,7 +309,7 @@ func (g Golang) GetInstallDirectory() (string, error) { cleanOut := strings.TrimSpace(string(stdout)) if !strings.Contains(cleanOut, homeDir) { - return fmt.Sprintf("%s%s", homeDir, defaultGolangInstallDir), nil + return filepath.Join(homeDir, defaultGolangInstallDir), nil } return cleanOut, nil diff --git a/helpers/dependencies/swagger.go b/helpers/dependencies/swagger.go index 09294fe..8ace917 100644 --- a/helpers/dependencies/swagger.go +++ b/helpers/dependencies/swagger.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "regexp" "runtime" "strings" @@ -163,7 +164,7 @@ func (s Swagger) Install(path string) error { } fh, err := os.OpenFile( - fmt.Sprintf("%s/swagger", path), + filepath.Join(path, "swagger"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0744, ) @@ -226,7 +227,7 @@ func (s Swagger) PostInstall(path string) error { log.Debug("Adding env variable to .bashrc") fh, err := os.OpenFile( - fmt.Sprintf("%s.bashrc", homeDir), + filepath.Join(homeDir, ".bashrc"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644, ) @@ -263,7 +264,7 @@ func (s Swagger) GetInstallDirectory() (string, error) { // concat default install dir with home and we have our path if !isInstalled { - return fmt.Sprintf("%s%s", homeDir, defaultSwaggerInstallDir), nil + return filepath.Join(homeDir, defaultSwaggerInstallDir), nil } // now let's play and find the current install path diff --git a/helpers/file.go b/helpers/file.go index 8d5f374..82d0274 100644 --- a/helpers/file.go +++ b/helpers/file.go @@ -1,7 +1,6 @@ package helpers import ( - "fmt" "io/fs" "io/ioutil" "os" @@ -12,45 +11,77 @@ import ( log "github.com/sirupsen/logrus" ) -func IsStrapProject() (bool, error) { +func IsGoguikDirectory(path string) (bool, error) { - currentPath, err := os.Getwd() + binPath, err := os.Executable() if err != nil { - return false, err + return false, errors.Trace(err) } - if !fileExists(fmt.Sprintf("%s/%s", currentPath, configFile)) { + goguikPath := filepath.Dir(binPath) + + if goguikPath != path { return false, nil } return true, nil } -func IsGoProject() (bool, error) { +func IsGoguikProject(path string) (bool, error) { - currentPath, err := os.Getwd() + exist, err := fileExists(filepath.Join(path, configFile)) if err != nil { - return false, err + return false, errors.Trace(err) } - if fileExists(fmt.Sprintf("%s/%s", currentPath, "go.mod")) { + if !exist { + return false, nil + } + + return true, nil +} + +func IsGoProject(path string) (bool, error) { + + exist, err := fileExists(filepath.Join(path, "go.mod")) + if err != nil { + return false, errors.Trace(err) + } + + if exist { return true, nil } - if fileExists(fmt.Sprintf("%s/%s", currentPath, "go.sum")) { + exist, err = fileExists(filepath.Join(path, "go.sum")) + if err != nil { + return false, errors.Trace(err) + } + + if exist { return true, nil } return false, nil } -func fileExists(filename string) bool { - info, err := os.Stat(filename) +func fileExists(path string) (bool, error) { - if errors.Is(err, fs.ErrNotExist) { - return false + fileInfo, err := os.Stat(path) + if err != nil { + + if errors.Is(err, fs.ErrPermission) { + return false, errors.NewForbidden(err, "file is not readeable") + } else if errors.Is(err, fs.ErrNotExist) { + return false, nil + } else { + return false, errors.Trace(err) + } } - return !info.IsDir() + if fileInfo.IsDir() { + return false, errors.NotValidf("%s is actually a directory", path) + } + + return true, nil } // isDirectoryWritable @@ -126,9 +157,7 @@ func RemoveDirectoryContent(path string) error { for _, d := range dir { err := os.RemoveAll( - filepath.Join( - []string{"path", d.Name()}..., - ), + filepath.Join(path, d.Name()), ) if err != nil { return errors.Trace(err)