fix(global): Add windows support(need testing), fix userInput, refacto
This commit is contained in:
parent
94975d5f33
commit
66ff28f6cd
@ -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{},
|
||||
|
@ -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))
|
||||
|
10
helpers/dependencies/enum.go
Normal file
10
helpers/dependencies/enum.go
Normal 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"
|
||||
)
|
@ -168,18 +168,33 @@ func (g Golang) Install(path string) error {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// zip on windows, tar gz on other platform
|
||||
if runtime.GOOS == "windows" {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package dependencies
|
||||
|
||||
// Dependency
|
||||
type Dependency interface {
|
||||
// DependencyInterface
|
||||
type DependencyInterface interface {
|
||||
CanBeInstalled() bool
|
||||
DescribeInstall(path string) string
|
||||
DescribePostInstall(path string) string
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user