fix(lint): Continue to apply linter recommandation
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@ -3,28 +3,29 @@ package dependencies
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
||||
"github.com/juju/errors"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Git represent an empty struct respecting the DependencyInterface.
|
||||
type Git struct{}
|
||||
|
||||
// regroup all git dependencies function
|
||||
|
||||
// GetName
|
||||
func (g Git) GetName() string {
|
||||
return "Git"
|
||||
// GetName return the name of the dependency using DependencyName enum.
|
||||
func (g Git) GetName() models.DependencyName {
|
||||
return models.DependencyName_GIT
|
||||
}
|
||||
|
||||
// GetMinimumVersion
|
||||
// GetMinimumVersion return the minimum version required for that dependency.
|
||||
func (g Git) GetMinimumVersion() string {
|
||||
return "0.0.0"
|
||||
}
|
||||
|
||||
// IsInstalled
|
||||
// IsInstalled check if the dependency is installed on the system.
|
||||
func (g Git) IsInstalled() (bool, error) {
|
||||
|
||||
_, err := g.GetBinaryPath()
|
||||
if err != nil && !errors.Is(err, exec.ErrNotFound) {
|
||||
return false, errors.Trace(err)
|
||||
@ -35,7 +36,7 @@ func (g Git) IsInstalled() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetBinaryPath
|
||||
// GetBinaryPath will search for the binary and return the path if found.
|
||||
func (g Git) GetBinaryPath() (string, error) {
|
||||
log.Debug("looking for git binary")
|
||||
|
||||
@ -49,9 +50,8 @@ func (g Git) GetBinaryPath() (string, error) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// GetVersion
|
||||
// GetVersion will find the current version of the dependency.
|
||||
func (g Git) GetVersion() (string, error) {
|
||||
|
||||
isInstalled, err := g.IsInstalled()
|
||||
if err != nil {
|
||||
return "", errors.Trace(err)
|
||||
@ -64,9 +64,8 @@ func (g Git) GetVersion() (string, error) {
|
||||
return "0.0.0", nil
|
||||
}
|
||||
|
||||
// IsVersionSupported
|
||||
// IsVersionSupported will compare the current version with the minimum expected version.
|
||||
func (g Git) IsVersionSupported() (bool, error) {
|
||||
|
||||
isInstalled, err := g.IsInstalled()
|
||||
if err != nil {
|
||||
return false, errors.Trace(err)
|
||||
@ -78,32 +77,32 @@ func (g Git) IsVersionSupported() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// CanBeInstalled
|
||||
// CanBeInstalled define if the dependency installation is handled by the app.
|
||||
func (g Git) CanBeInstalled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// DescribeInstall
|
||||
// DescribeInstall will do nothing for that dependency.
|
||||
func (g Git) DescribeInstall(path string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Install
|
||||
// Install will do nothing for that dependency.
|
||||
func (g Git) Install(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DescribePostInstall
|
||||
// DescribePostInstall will do nothing for that dependency.
|
||||
func (g Git) DescribePostInstall(path string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// PostInstall
|
||||
// PostInstall will do nothing for that dependency.
|
||||
func (g Git) PostInstall(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInstallDirectory
|
||||
// GetInstallDirectory will do nothing for that dependency.
|
||||
func (g Git) GetInstallDirectory() (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
@ -13,36 +13,37 @@ import (
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// minimum minor required for the app to work
|
||||
// minimumGolangVersion is the minimum minor required for the app to work.
|
||||
const minimumGolangVersion = "1.18.4"
|
||||
|
||||
// installation directory for fresh install.
|
||||
// will be prefixed by $HOME
|
||||
// will be prefixed by $HOME.
|
||||
const defaultGolangInstallDir = "/local/go"
|
||||
|
||||
var regexGolangVersion = regexp.MustCompile(`^go version go(\d+\.\d+\.\d+)`)
|
||||
|
||||
// Golang represent an empty struct respecting the DependencyInterface.
|
||||
type Golang struct{}
|
||||
|
||||
// regroup all golang dependencies function
|
||||
// regroup all golang dependencies function.
|
||||
|
||||
// GetName
|
||||
func (g Golang) GetName() string {
|
||||
return "Golang"
|
||||
// GetName return the name of the dependency using DependencyName enum.
|
||||
func (g Golang) GetName() models.DependencyName {
|
||||
return models.DependencyName_GOLANG
|
||||
}
|
||||
|
||||
// GetMinimumVersion
|
||||
// GetMinimumVersion return the minimum version required for that dependency.
|
||||
func (g Golang) GetMinimumVersion() string {
|
||||
return minimumGolangVersion
|
||||
}
|
||||
|
||||
// IsInstalled
|
||||
// IsInstalled check if the dependency is installed on the system.
|
||||
func (g Golang) IsInstalled() (bool, error) {
|
||||
|
||||
_, err := g.GetBinaryPath()
|
||||
if err != nil && !errors.Is(err, exec.ErrNotFound) {
|
||||
return false, errors.Trace(err)
|
||||
@ -53,7 +54,7 @@ func (g Golang) IsInstalled() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetBinaryPath
|
||||
// GetBinaryPath will search for the binary and return the path if found.
|
||||
func (g Golang) GetBinaryPath() (string, error) {
|
||||
log.Debug("looking for golang binary")
|
||||
|
||||
@ -67,7 +68,7 @@ func (g Golang) GetBinaryPath() (string, error) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// GetVersion
|
||||
// GetVersion will find the current version of the dependency.
|
||||
func (g Golang) GetVersion() (string, error) {
|
||||
|
||||
isInstalled, err := g.IsInstalled()
|
||||
@ -86,6 +87,7 @@ func (g Golang) GetVersion() (string, error) {
|
||||
|
||||
log.Debug("executing go version command")
|
||||
|
||||
//nolint:gosec // we trust the binary we are launching
|
||||
cmd := exec.Command(binaryPath, "version")
|
||||
stdout, err := cmd.Output()
|
||||
if err != nil {
|
||||
@ -103,9 +105,8 @@ func (g Golang) GetVersion() (string, error) {
|
||||
return parseOutput[1], nil
|
||||
}
|
||||
|
||||
// IsVersionSupported
|
||||
// IsVersionSupported will compare the current version with the minimum expected version.
|
||||
func (g Golang) IsVersionSupported() (bool, error) {
|
||||
|
||||
isInstalled, err := g.IsInstalled()
|
||||
if err != nil {
|
||||
return false, errors.Trace(err)
|
||||
@ -136,14 +137,13 @@ func (g Golang) IsVersionSupported() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// CanBeInstalled
|
||||
// CanBeInstalled define if the dependency installation is handled by the app.
|
||||
func (g Golang) CanBeInstalled() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DescribeInstall
|
||||
// DescribeInstall will list the aciton that will be executed for the dependency update or installation.
|
||||
func (g Golang) DescribeInstall(path string) string {
|
||||
|
||||
commands := []string{
|
||||
"The following commands will be executed",
|
||||
fmt.Sprintf("rm -rf %s/* ", path),
|
||||
@ -153,9 +153,8 @@ func (g Golang) DescribeInstall(path string) string {
|
||||
return strings.Join(commands, "\n")
|
||||
}
|
||||
|
||||
// Install
|
||||
// Install will install or update the dependency.
|
||||
func (g Golang) Install(path string) error {
|
||||
|
||||
err := helpers.RemoveDirectoryContent(path)
|
||||
if err != nil {
|
||||
log.Warnf("fail to delete content of directory %s", path)
|
||||
@ -170,7 +169,6 @@ func (g Golang) Install(path string) error {
|
||||
|
||||
// zip on windows, tar gz on other platform
|
||||
if runtime.GOOS == "windows" {
|
||||
|
||||
log.Debug("Working on zip, Unzip")
|
||||
|
||||
err = unZip(content, "go/", path)
|
||||
@ -178,9 +176,7 @@ func (g Golang) Install(path string) error {
|
||||
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)
|
||||
@ -199,9 +195,8 @@ func (g Golang) Install(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DescribePostInstall
|
||||
// DescribePostInstall will list the post installation action that will be executed.
|
||||
func (g Golang) DescribePostInstall(path string) string {
|
||||
|
||||
descriptions := []string{
|
||||
`For your environment to work correctly, we will add if needed the following environment variable to your .bashrc:
|
||||
|
||||
@ -219,9 +214,8 @@ You will have to reopen a new terminal to apply the changes or execute the follo
|
||||
return strings.Join(descriptions, "\n")
|
||||
}
|
||||
|
||||
// PostInstall
|
||||
// PostInstall will execute the post installation or update of the dependency.
|
||||
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
|
||||
@ -231,15 +225,13 @@ func (g Golang) PostInstall(path string) error {
|
||||
"# Golang - Added by gouick",
|
||||
}
|
||||
|
||||
gopath := os.Getenv("GOPATH")
|
||||
createGoPath := false
|
||||
if gopath == "" {
|
||||
if os.Getenv("GOPATH") == "" {
|
||||
lineBashRc = append(lineBashRc, "export GOPATH=\"$HOME/go\"")
|
||||
createGoPath = true
|
||||
}
|
||||
|
||||
goroot := os.Getenv("GOROOT")
|
||||
if goroot == "" {
|
||||
if os.Getenv("GOROOT") == "" {
|
||||
lineBashRc = append(lineBashRc, fmt.Sprintf("export GOROOT=\"%s\"", path))
|
||||
}
|
||||
|
||||
@ -265,12 +257,17 @@ func (g Golang) PostInstall(path string) error {
|
||||
fh, err := os.OpenFile(
|
||||
filepath.Join(homeDir, ".bashrc"),
|
||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
|
||||
0644,
|
||||
0600,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
defer fh.Close()
|
||||
|
||||
defer func() {
|
||||
if err := fh.Close(); err != nil {
|
||||
log.Errorf("Error closing file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = fh.WriteString(
|
||||
fmt.Sprintf("\n\n%s\n", strings.Join(lineBashRc, "\n")),
|
||||
@ -281,7 +278,6 @@ func (g Golang) PostInstall(path string) error {
|
||||
}
|
||||
|
||||
if createGoPath {
|
||||
|
||||
log.Debug("creating gopath directory")
|
||||
|
||||
_, err = helpers.CheckAndCreateDir(filepath.Join(homeDir, "go"))
|
||||
@ -295,9 +291,8 @@ func (g Golang) PostInstall(path string) error {
|
||||
|
||||
// GetInstallDirectory will try to find the current golang directory. If it doesn't exist or if it's in a
|
||||
// not userspace directory, it will provide the "default"
|
||||
// It doesn't mean that the directory is "writable"
|
||||
// It doesn't mean that the directory is "writable".
|
||||
func (g Golang) GetInstallDirectory() (string, error) {
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", errors.Trace(err)
|
||||
@ -321,6 +316,7 @@ func (g Golang) GetInstallDirectory() (string, error) {
|
||||
|
||||
log.Debug("executing go env GOROOT command")
|
||||
|
||||
//nolint:gosec // we trust the binary we are launching
|
||||
cmd := exec.Command(binaryPath, "env", "GOROOT")
|
||||
stdout, err := cmd.Output()
|
||||
if err != nil {
|
||||
@ -336,10 +332,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)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("https://dl.google.com/go/go%s.%s-%s.tar.gz", minimumGolangVersion, runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
|
@ -1,13 +1,15 @@
|
||||
package dependencies
|
||||
|
||||
// DependencyInterface
|
||||
import "git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
||||
|
||||
// DependencyInterface is the interface that need to be respected for a dependency.
|
||||
type DependencyInterface interface {
|
||||
CanBeInstalled() bool
|
||||
DescribeInstall(path string) string
|
||||
DescribePostInstall(path string) string
|
||||
GetBinaryPath() (string, error)
|
||||
GetInstallDirectory() (string, error)
|
||||
GetName() string
|
||||
GetName() models.DependencyName
|
||||
GetMinimumVersion() string
|
||||
GetVersion() (string, error)
|
||||
Install(path string) error
|
||||
|
@ -2,7 +2,7 @@ package dependencies
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@ -10,38 +10,39 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
||||
"github.com/blang/semver"
|
||||
"github.com/juju/errors"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// minimum minor required for the app to work
|
||||
// minimumGolangVersion is the minimum minor required for the app to work.
|
||||
const minimumSwaggerVersion = "0.29.0"
|
||||
|
||||
// installation directory for fresh install.
|
||||
// will be prefixed by $HOME
|
||||
// will be prefixed by $HOME.
|
||||
const defaultSwaggerInstallDir = "/bin"
|
||||
|
||||
var regexSwaggerVersion = regexp.MustCompile(`^version: v(\d+\.\d+\.\d+)`)
|
||||
|
||||
// Swagger represent an empty struct respecting the DependencyInterface.
|
||||
type Swagger struct{}
|
||||
|
||||
// regroup all swagger dependencies function
|
||||
// regroup all swagger dependencies function.
|
||||
|
||||
// GetName
|
||||
func (s Swagger) GetName() string {
|
||||
return "Go Swagger"
|
||||
// GetName return the name of the dependency using DependencyName enum.
|
||||
func (s Swagger) GetName() models.DependencyName {
|
||||
return models.DependencyName_GO_SWAGGER
|
||||
}
|
||||
|
||||
// GetMinimumVersion
|
||||
// GetMinimumVersion return the minimum version required for that dependency.
|
||||
func (s Swagger) GetMinimumVersion() string {
|
||||
return minimumSwaggerVersion
|
||||
}
|
||||
|
||||
// IsInstalled
|
||||
// IsInstalled check if the dependency is installed on the system.
|
||||
func (s Swagger) IsInstalled() (bool, error) {
|
||||
|
||||
_, err := s.GetBinaryPath()
|
||||
if err != nil && !errors.Is(err, exec.ErrNotFound) {
|
||||
return false, errors.Trace(err)
|
||||
@ -52,7 +53,7 @@ func (s Swagger) IsInstalled() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetBinaryPath
|
||||
// GetBinaryPath will search for the binary and return the path if found.
|
||||
func (s Swagger) GetBinaryPath() (string, error) {
|
||||
log.Debug("looking for swagger binary")
|
||||
|
||||
@ -66,7 +67,7 @@ func (s Swagger) GetBinaryPath() (string, error) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// GetVersion
|
||||
// GetVersion will find the current version of the dependency.
|
||||
func (s Swagger) GetVersion() (string, error) {
|
||||
|
||||
isInstalled, err := s.IsInstalled()
|
||||
@ -85,6 +86,7 @@ func (s Swagger) GetVersion() (string, error) {
|
||||
|
||||
log.Debug("executing swagger version command")
|
||||
|
||||
//nolint:gosec // we trust the binary we are launching
|
||||
cmd := exec.Command(binaryPath, "version")
|
||||
stdout, err := cmd.Output()
|
||||
if err != nil {
|
||||
@ -102,9 +104,8 @@ func (s Swagger) GetVersion() (string, error) {
|
||||
return parseOutput[1], nil
|
||||
}
|
||||
|
||||
// IsVersionSupported
|
||||
// IsVersionSupported will compare the current version with the minimum expected version.
|
||||
func (s Swagger) IsVersionSupported() (bool, error) {
|
||||
|
||||
isInstalled, err := s.IsInstalled()
|
||||
if err != nil {
|
||||
return false, errors.Trace(err)
|
||||
@ -135,14 +136,13 @@ func (s Swagger) IsVersionSupported() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// CanBeInstalled
|
||||
// CanBeInstalled define if the dependency installation is handled by the app.
|
||||
func (s Swagger) CanBeInstalled() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DescribeInstall
|
||||
// DescribeInstall will list the aciton that will be executed for the dependency update or installation.
|
||||
func (s Swagger) DescribeInstall(path string) string {
|
||||
|
||||
commands := []string{
|
||||
"The following commands will be executed",
|
||||
fmt.Sprintf("rm -rf %s/swagger ", path),
|
||||
@ -153,9 +153,8 @@ func (s Swagger) DescribeInstall(path string) string {
|
||||
return strings.Join(commands, "\n")
|
||||
}
|
||||
|
||||
// Install
|
||||
// Install will install or update the dependency.
|
||||
func (s Swagger) Install(path string) error {
|
||||
|
||||
downloadURL := s.getDownloadURL()
|
||||
content, err := downloadFile(downloadURL)
|
||||
if err != nil {
|
||||
@ -166,14 +165,19 @@ func (s Swagger) Install(path string) error {
|
||||
fh, err := os.OpenFile(
|
||||
filepath.Join(path, "swagger"),
|
||||
os.O_RDWR|os.O_CREATE|os.O_TRUNC,
|
||||
0744,
|
||||
0600,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
defer fh.Close()
|
||||
|
||||
bContent, err := ioutil.ReadAll(content)
|
||||
defer func() {
|
||||
if err := fh.Close(); err != nil {
|
||||
log.Errorf("Error closing file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
bContent, err := io.ReadAll(content)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
@ -186,9 +190,8 @@ func (s Swagger) Install(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DescribePostInstall
|
||||
// DescribePostInstall will list the post installation action that will be executed.
|
||||
func (s Swagger) DescribePostInstall(path string) string {
|
||||
|
||||
return `For your environment to work correctly, we will add if needed the following environment variable to your .bashrc:
|
||||
|
||||
PATH=\"$HOME/bin:$PATH\"
|
||||
@ -200,9 +203,8 @@ You will have to reopen a new terminal to apply the changes or execute the follo
|
||||
`
|
||||
}
|
||||
|
||||
// PostInstall
|
||||
// PostInstall will execute the post installation or update of the dependency.
|
||||
func (s Swagger) PostInstall(path string) error {
|
||||
|
||||
lineBashRc := []string{
|
||||
"# Swagger - Added by gouick",
|
||||
}
|
||||
@ -229,12 +231,17 @@ func (s Swagger) PostInstall(path string) error {
|
||||
fh, err := os.OpenFile(
|
||||
filepath.Join(homeDir, ".bashrc"),
|
||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
|
||||
0644,
|
||||
0600,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
defer fh.Close()
|
||||
|
||||
defer func() {
|
||||
if err := fh.Close(); err != nil {
|
||||
log.Errorf("Error closing file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = fh.WriteString(
|
||||
fmt.Sprintf("\n\n%s\n", strings.Join(lineBashRc, "\n")),
|
||||
@ -249,9 +256,8 @@ func (s Swagger) PostInstall(path string) error {
|
||||
|
||||
// GetInstallDirectory will try to find the current swagger directory. If it doesn't exist or if it's in a
|
||||
// not userspace directory, it will provide the "default"
|
||||
// It doesn't mean that the directory is "writable"
|
||||
// It doesn't mean that the directory is "writable".
|
||||
func (s Swagger) GetInstallDirectory() (string, error) {
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", errors.Trace(err)
|
||||
@ -277,6 +283,5 @@ func (s Swagger) GetInstallDirectory() (string, error) {
|
||||
}
|
||||
|
||||
func (s Swagger) getDownloadURL() string {
|
||||
|
||||
return fmt.Sprintf("https://github.com/go-swagger/go-swagger/releases/download/v%s/swagger_%s_%s", minimumSwaggerVersion, runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
|
@ -20,13 +20,17 @@ import (
|
||||
// downloadFile will download file from a given url and store in memory the content
|
||||
// content will probably be untar, ungzip, ....
|
||||
func downloadFile(url string) (io.Reader, error) {
|
||||
|
||||
// Get the data
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
log.Errorf("Error closing body: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
_, err = buf.ReadFrom(resp.Body)
|
||||
@ -38,12 +42,16 @@ func downloadFile(url string) (io.Reader, error) {
|
||||
}
|
||||
|
||||
func unGzip(reader io.Reader) (io.Reader, error) {
|
||||
|
||||
gzipReader, err := gzip.NewReader(reader)
|
||||
if err != nil {
|
||||
return nil, errors.Trace(err)
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
|
||||
defer func() {
|
||||
if err := gzipReader.Close(); err != nil {
|
||||
log.Errorf("Error closing gzip reader: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return gzipReader, nil
|
||||
}
|
||||
@ -59,7 +67,7 @@ func unTar(reader io.Reader, subdir string, dest string) error {
|
||||
header, err := tr.Next()
|
||||
switch {
|
||||
// no more files
|
||||
case err == io.EOF:
|
||||
case errors.Is(err, io.EOF):
|
||||
return nil
|
||||
case err != nil:
|
||||
return errors.Trace(err)
|
||||
@ -70,7 +78,6 @@ func unTar(reader io.Reader, subdir string, dest string) error {
|
||||
filename := header.Name
|
||||
|
||||
if subdir != "" && strings.HasPrefix(filename, subdir) {
|
||||
|
||||
filename = strings.TrimPrefix(filename, subdir)
|
||||
|
||||
if filename == "" {
|
||||
@ -78,7 +85,10 @@ func unTar(reader io.Reader, subdir string, dest string) error {
|
||||
}
|
||||
}
|
||||
|
||||
target := filepath.Join(dest, filename)
|
||||
target, err := sanitizeArchivePath(dest, filename)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("Extacting %s", target)
|
||||
|
||||
@ -86,7 +96,7 @@ func unTar(reader io.Reader, subdir string, dest string) error {
|
||||
// create directory if doesn't exit
|
||||
case tar.TypeDir:
|
||||
if _, err := os.Stat(target); err != nil {
|
||||
if err := os.MkdirAll(target, 0755); err != nil {
|
||||
if err := os.MkdirAll(target, 0750); err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
@ -96,7 +106,12 @@ func unTar(reader io.Reader, subdir string, dest string) error {
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
log.Errorf("Error closing file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// copy contents to file
|
||||
if _, err := io.Copy(f, tr); err != nil {
|
||||
@ -107,7 +122,6 @@ 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)
|
||||
}
|
||||
@ -128,11 +142,9 @@ func unZip(reader io.Reader, subdir string, dest string) error {
|
||||
}
|
||||
|
||||
for _, file := range zipReader.File {
|
||||
|
||||
filename := file.Name
|
||||
|
||||
if subdir != "" && strings.HasPrefix(filename, subdir) {
|
||||
|
||||
filename = strings.TrimPrefix(filename, subdir)
|
||||
|
||||
if filename == "" {
|
||||
@ -140,29 +152,41 @@ func unZip(reader io.Reader, subdir string, dest string) error {
|
||||
}
|
||||
}
|
||||
|
||||
target := filepath.Join(dest, filename)
|
||||
target, err := sanitizeArchivePath(dest, filename)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("Extacting %s", target)
|
||||
|
||||
if file.FileInfo().IsDir() {
|
||||
if _, err := os.Stat(target); err != nil {
|
||||
if err := os.MkdirAll(target, 0755); err != nil {
|
||||
if err := os.MkdirAll(target, 0750); 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()
|
||||
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
log.Errorf("Error closing file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
fileInArchive, err := file.Open()
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
defer fileInArchive.Close()
|
||||
|
||||
defer func() {
|
||||
if err := fileInArchive.Close(); err != nil {
|
||||
log.Errorf("Error closing file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// copy contents to file
|
||||
if _, err := io.Copy(f, fileInArchive); err != nil {
|
||||
@ -173,3 +197,12 @@ func unZip(reader io.Reader, subdir string, dest string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sanitizeArchivePath will sanitize archive file pathing from "G305: Zip Slip vulnerability".
|
||||
func sanitizeArchivePath(d string, t string) (string, error) {
|
||||
if v := filepath.Join(d, t); strings.HasPrefix(v, filepath.Clean(d)) {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("%s: %s", "content filepath is tainted", t)
|
||||
}
|
||||
|
Reference in New Issue
Block a user