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
7 changed files with 156 additions and 32 deletions

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

View File

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

View File

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