feat(multiple): Add version command, improve dependencies file managment
This commit is contained in:
parent
ce433832f7
commit
5b50b5409b
@ -1 +1,6 @@
|
||||
# Goguik (name TBD)
|
||||
# Goguik (name TBD)
|
||||
|
||||
Only tested on Linux
|
||||
|
||||
## Todo
|
||||
- Implement for MacOS & Windows
|
@ -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")
|
||||
},
|
||||
}
|
||||
|
78
cmd/init.go
78
cmd/init.go
@ -5,6 +5,9 @@ Copyright © 2022 Matthieu Derasse <git@derasse.fr>
|
||||
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.")
|
||||
|
22
cmd/root.go
22
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
|
||||
}
|
||||
|
27
cmd/version.go
Normal file
27
cmd/version.go
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
Copyright © 2022 Matthieu Derasse <git@derasse.fr>
|
||||
|
||||
*/
|
||||
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)
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package helpers
|
||||
|
||||
const configFile = ".strap.yaml"
|
||||
const configFile = ".goguik.yaml"
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user