feat(multiple): Add version command, improve dependencies file managment

This commit is contained in:
Matthieu 'JP' DERASSE
2022-07-16 22:16:29 +00:00
parent ce433832f7
commit 5b50b5409b
9 changed files with 182 additions and 44 deletions

View File

@ -1,3 +1,3 @@
package helpers
const configFile = ".strap.yaml"
const configFile = ".goguik.yaml"

View File

@ -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

View File

@ -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

View File

@ -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)