From 66ff28f6cd224e17306d4667ef8e987ac0a989f2 Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Mon, 25 Jul 2022 20:19:38 +0000 Subject: [PATCH] fix(global): Add windows support(need testing), fix userInput, refacto --- cmd/root.go | 2 +- cmd/upgrade.go | 4 +- helpers/dependencies/enum.go | 10 +++++ helpers/dependencies/golang.go | 46 ++++++++++++++++----- helpers/dependencies/interface.go | 4 +- helpers/dependencies/utils.go | 69 +++++++++++++++++++++++++++++++ helpers/input.go | 53 +++++++++++++++++------- 7 files changed, 156 insertions(+), 32 deletions(-) create mode 100644 helpers/dependencies/enum.go diff --git a/cmd/root.go b/cmd/root.go index cffefcf..455d5f7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,7 +16,7 @@ import ( var version = "0.0.1" var verbose = false var acceptAll = false -var dependencyList = []dependencies.Dependency{ +var dependencyList = []dependencies.DependencyInterface{ dependencies.Git{}, dependencies.Golang{}, dependencies.Swagger{}, diff --git a/cmd/upgrade.go b/cmd/upgrade.go index a981dd3..f7b0138 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -79,7 +79,7 @@ func runUpgradeAction(cmd *cobra.Command, args []string) { answer := helpers.YesOrNoInput() if !answer { log.Infof("Where do you want to install %s ?", dependency.GetName()) - installDirectory = helpers.IsValidPathInput() + installDirectory = helpers.PathInput() } } else { @@ -96,7 +96,7 @@ func runUpgradeAction(cmd *cobra.Command, args []string) { return } log.Infof("Where do you want to install %s ?", dependency.GetName()) - installDirectory = helpers.IsValidPathInput() + installDirectory = helpers.PathInput() } log.Infof("%s", dependency.DescribeInstall(installDirectory)) diff --git a/helpers/dependencies/enum.go b/helpers/dependencies/enum.go new file mode 100644 index 0000000..25a5a67 --- /dev/null +++ b/helpers/dependencies/enum.go @@ -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" +) diff --git a/helpers/dependencies/golang.go b/helpers/dependencies/golang.go index 05ef650..378ea32 100644 --- a/helpers/dependencies/golang.go +++ b/helpers/dependencies/golang.go @@ -168,17 +168,32 @@ func (g Golang) Install(path string) error { return errors.Trace(err) } - gzipReader, err := unGzip(content) - if err != nil { - log.Warnf("fail to un-gzip downloaded file from %s, error:", downloadUrl) - return errors.Trace(err) - } + // zip on windows, tar gz on other platform + if runtime.GOOS == "windows" { - // XXX: unTar should take a subdir - err = unTar(gzipReader, "go/", path) - if err != nil { - log.Warnf("fail to un-tar downloaded file from %s", downloadUrl) - return errors.Trace(err) + log.Debug("Working on zip, Unzip") + + err = unZip(content, "go/", path) + if err != nil { + 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 @@ -207,6 +222,11 @@ You will have to reopen a new terminal to apply the changes or execute the follo // PostInstall 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{ "# Golang - Added by gouik", } @@ -317,5 +337,9 @@ func (g Golang) GetInstallDirectory() (string, error) { 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) + } } diff --git a/helpers/dependencies/interface.go b/helpers/dependencies/interface.go index 0e8d7ab..06cdde9 100644 --- a/helpers/dependencies/interface.go +++ b/helpers/dependencies/interface.go @@ -1,7 +1,7 @@ package dependencies -// Dependency -type Dependency interface { +// DependencyInterface +type DependencyInterface interface { CanBeInstalled() bool DescribeInstall(path string) string DescribePostInstall(path string) string diff --git a/helpers/dependencies/utils.go b/helpers/dependencies/utils.go index c511e98..ff1f37c 100644 --- a/helpers/dependencies/utils.go +++ b/helpers/dependencies/utils.go @@ -2,6 +2,7 @@ package dependencies import ( "archive/tar" + "archive/zip" "bytes" "compress/gzip" "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 +} diff --git a/helpers/input.go b/helpers/input.go index 311d306..427dfaa 100644 --- a/helpers/input.go +++ b/helpers/input.go @@ -1,23 +1,22 @@ package helpers import ( - "fmt" + "bufio" + "os" "strings" + "git.home.m-and-m.ovh/mderasse/boot/helpers/api_type" log "github.com/sirupsen/logrus" ) // YesOrNoInput func YesOrNoInput() bool { - var userInput string + scanner := bufio.NewScanner(os.Stdin) for { - _, err := fmt.Scanf("%s", &userInput) - if err != nil { - log.Infof("failed to read input, try again (%s)", err.Error()) - continue - } + scanner.Scan() + userInput := scanner.Text() lUserInput := strings.ToLower(userInput) @@ -36,18 +35,16 @@ func YesOrNoInput() bool { } } -// IsValidPathInput -func IsValidPathInput() string { - var userInput string +// PathInput +func PathInput() string { + + scanner := bufio.NewScanner(os.Stdin) for { - _, err := fmt.Scanf("%s", &userInput) - if err != nil { - log.Infof("failed to read input, try again (%s)", err.Error()) - continue - } + scanner.Scan() + userInput := scanner.Text() - err = CheckAndCreateDir(userInput) + err := CheckAndCreateDir(userInput) if err != nil { log.Warnf("please, try again") continue @@ -56,3 +53,27 @@ func IsValidPathInput() string { 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 + } +}