fix(global): Add windows support(need testing), fix userInput, refacto

This commit is contained in:
Matthieu 'JP' DERASSE 2022-07-25 20:19:38 +00:00
parent 94975d5f33
commit 66ff28f6cd
Signed by: mderasse
GPG Key ID: 55141C777B16A705
7 changed files with 156 additions and 32 deletions

View File

@ -16,7 +16,7 @@ import (
var version = "0.0.1" var version = "0.0.1"
var verbose = false var verbose = false
var acceptAll = false var acceptAll = false
var dependencyList = []dependencies.Dependency{ var dependencyList = []dependencies.DependencyInterface{
dependencies.Git{}, dependencies.Git{},
dependencies.Golang{}, dependencies.Golang{},
dependencies.Swagger{}, dependencies.Swagger{},

View File

@ -79,7 +79,7 @@ func runUpgradeAction(cmd *cobra.Command, args []string) {
answer := helpers.YesOrNoInput() answer := helpers.YesOrNoInput()
if !answer { if !answer {
log.Infof("Where do you want to install %s ?", dependency.GetName()) log.Infof("Where do you want to install %s ?", dependency.GetName())
installDirectory = helpers.IsValidPathInput() installDirectory = helpers.PathInput()
} }
} else { } else {
@ -96,7 +96,7 @@ func runUpgradeAction(cmd *cobra.Command, args []string) {
return return
} }
log.Infof("Where do you want to install %s ?", dependency.GetName()) log.Infof("Where do you want to install %s ?", dependency.GetName())
installDirectory = helpers.IsValidPathInput() installDirectory = helpers.PathInput()
} }
log.Infof("%s", dependency.DescribeInstall(installDirectory)) log.Infof("%s", dependency.DescribeInstall(installDirectory))

View File

@ -0,0 +1,10 @@
package dependencies
// DependencyName
type DependencyName string
const (
DependencyName_GIT DependencyName = "Git"
DependencyName_GOLANG DependencyName = "Golang"
DependencyName_GO_SWAGGER DependencyName = "Go Swagger"
)

View File

@ -168,17 +168,32 @@ func (g Golang) Install(path string) error {
return errors.Trace(err) return errors.Trace(err)
} }
gzipReader, err := unGzip(content) // zip on windows, tar gz on other platform
if err != nil { if runtime.GOOS == "windows" {
log.Warnf("fail to un-gzip downloaded file from %s, error:", downloadUrl)
return errors.Trace(err)
}
// XXX: unTar should take a subdir log.Debug("Working on zip, Unzip")
err = unTar(gzipReader, "go/", path)
if err != nil { err = unZip(content, "go/", path)
log.Warnf("fail to un-tar downloaded file from %s", downloadUrl) if err != nil {
return errors.Trace(err) log.Warnf("fail to un-zip downloaded file from %s", downloadUrl)
return errors.Trace(err)
}
} else {
log.Debug("Working on tar gz, UnGzip & unTar")
gzipReader, err := unGzip(content)
if err != nil {
log.Warnf("fail to un-gzip downloaded file from %s, error:", downloadUrl)
return errors.Trace(err)
}
err = unTar(gzipReader, "go/", path)
if err != nil {
log.Warnf("fail to un-tar downloaded file from %s", downloadUrl)
return errors.Trace(err)
}
} }
return nil return nil
@ -207,6 +222,11 @@ You will have to reopen a new terminal to apply the changes or execute the follo
// PostInstall // PostInstall
func (g Golang) PostInstall(path string) error { func (g Golang) PostInstall(path string) error {
if runtime.GOOS == "windows" {
log.Warnf("Unable to environement variable on windows. Please add it by yourself")
return nil
}
lineBashRc := []string{ lineBashRc := []string{
"# Golang - Added by gouik", "# Golang - Added by gouik",
} }
@ -317,5 +337,9 @@ func (g Golang) GetInstallDirectory() (string, error) {
func (g Golang) getDownloadUrl() string { func (g Golang) getDownloadUrl() string {
return fmt.Sprintf("https://dl.google.com/go/go%s.%s-%s.tar.gz", minimumGolangVersion, runtime.GOOS, runtime.GOARCH) if runtime.GOOS == "windows" {
return fmt.Sprintf("https://dl.google.com/go/go%s.%s-%s.zip", minimumGolangVersion, runtime.GOOS, runtime.GOARCH)
} else {
return fmt.Sprintf("https://dl.google.com/go/go%s.%s-%s.tar.gz", minimumGolangVersion, runtime.GOOS, runtime.GOARCH)
}
} }

View File

@ -1,7 +1,7 @@
package dependencies package dependencies
// Dependency // DependencyInterface
type Dependency interface { type DependencyInterface interface {
CanBeInstalled() bool CanBeInstalled() bool
DescribeInstall(path string) string DescribeInstall(path string) string
DescribePostInstall(path string) string DescribePostInstall(path string) string

View File

@ -2,6 +2,7 @@ package dependencies
import ( import (
"archive/tar" "archive/tar"
"archive/zip"
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
@ -104,3 +105,71 @@ func unTar(reader io.Reader, subdir string, dest string) error {
} }
} }
} }
func unZip(reader io.Reader, subdir string, dest string) error {
if subdir != "" && !strings.HasSuffix(subdir, "/") {
subdir = fmt.Sprintf("%s/", subdir)
}
// transform io.Reader
buff := bytes.NewBuffer([]byte{})
size, err := io.Copy(buff, reader)
if err != nil {
return errors.Trace(err)
}
readerBytes := bytes.NewReader(buff.Bytes())
// Open a zip archive for reading.
zipReader, err := zip.NewReader(readerBytes, size)
if err != nil {
return errors.Trace(err)
}
for _, file := range zipReader.File {
filename := file.Name
if subdir != "" && strings.HasPrefix(filename, subdir) {
filename = strings.TrimPrefix(filename, subdir)
if filename == "" {
continue
}
}
target := filepath.Join(dest, filename)
log.Debugf("Extacting %s", target)
if file.FileInfo().IsDir() {
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
return errors.Trace(err)
}
}
} else {
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, file.Mode())
if err != nil {
return errors.Trace(err)
}
defer f.Close()
fileInArchive, err := file.Open()
if err != nil {
return errors.Trace(err)
}
defer fileInArchive.Close()
// copy contents to file
if _, err := io.Copy(f, fileInArchive); err != nil {
return errors.Trace(err)
}
}
}
return nil
}

View File

@ -1,23 +1,22 @@
package helpers package helpers
import ( import (
"fmt" "bufio"
"os"
"strings" "strings"
"git.home.m-and-m.ovh/mderasse/boot/helpers/api_type"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// YesOrNoInput // YesOrNoInput
func YesOrNoInput() bool { func YesOrNoInput() bool {
var userInput string scanner := bufio.NewScanner(os.Stdin)
for { for {
_, err := fmt.Scanf("%s", &userInput) scanner.Scan()
if err != nil { userInput := scanner.Text()
log.Infof("failed to read input, try again (%s)", err.Error())
continue
}
lUserInput := strings.ToLower(userInput) lUserInput := strings.ToLower(userInput)
@ -36,18 +35,16 @@ func YesOrNoInput() bool {
} }
} }
// IsValidPathInput // PathInput
func IsValidPathInput() string { func PathInput() string {
var userInput string
scanner := bufio.NewScanner(os.Stdin)
for { for {
_, err := fmt.Scanf("%s", &userInput) scanner.Scan()
if err != nil { userInput := scanner.Text()
log.Infof("failed to read input, try again (%s)", err.Error())
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
@ -56,3 +53,27 @@ func IsValidPathInput() string {
return userInput return userInput
} }
} }
// ApiTypeInput
func ApiTypeInput() api_type.ApiTypeName {
var possibleApiTypes []string
for _, apiType := range api_type.GetListOfApiTypeName() {
possibleApiTypes = append(possibleApiTypes, string(apiType))
}
scanner := bufio.NewScanner(os.Stdin)
for {
scanner.Scan()
userInput := scanner.Text()
apiTypeName, err := api_type.NewApiTypeFromInput(userInput)
if err != nil {
log.Warnf("invalid API type (possible values: %s)", strings.Join(possibleApiTypes, ", "))
continue
}
return apiTypeName
}
}