Skip to content

Commit

Permalink
Delint
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksmaus committed Oct 3, 2022
1 parent dc3870a commit 7f35077
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
6 changes: 3 additions & 3 deletions internal/pkg/agent/application/upgrade/service_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (p *darwinPidProvider) Close() {}

func (p *darwinPidProvider) PID(ctx context.Context) (int, error) {
piders := []func(context.Context) (int, error){
p.piderFromCmd(ctx, "launchctl", "list", paths.ServiceName),
p.piderFromCmd("launchctl", "list", paths.ServiceName),
}

// if release is specifically built to be upgradeable (using DEV flag)
// we dont require to run as a service and will need sudo fallback
if release.Upgradeable() {
piders = append(piders, p.piderFromCmd(ctx, "sudo", "launchctl", "list", paths.ServiceName))
piders = append(piders, p.piderFromCmd("sudo", "launchctl", "list", paths.ServiceName))
}

var pidErrors error
Expand All @@ -71,7 +71,7 @@ func (p *darwinPidProvider) PID(ctx context.Context) (int, error) {
return 0, pidErrors
}

func (p *darwinPidProvider) piderFromCmd(ctx context.Context, name string, args ...string) func(context.Context) (int, error) {
func (p *darwinPidProvider) piderFromCmd(name string, args ...string) func(context.Context) (int, error) {
return func(context.Context) (int, error) {
listCmd := exec.Command(name, args...)
listCmd.SysProcAttr = &syscall.SysProcAttr{
Expand Down
13 changes: 9 additions & 4 deletions internal/pkg/agent/application/upgrade/step_relink.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"github.com/elastic/elastic-agent/pkg/core/logger"
)

const (
windows = "windows"
exe = ".exe"
)

// ChangeSymlink updates symlink paths to match current version.
func ChangeSymlink(ctx context.Context, log *logger.Logger, targetHash string) error {
// create symlink to elastic-agent-{hash}
Expand All @@ -28,9 +33,9 @@ func ChangeSymlink(ctx context.Context, log *logger.Logger, targetHash string) e
newPath := paths.BinaryPath(filepath.Join(paths.Top(), "data", hashedDir), agentName)

// handle windows suffixes
if runtime.GOOS == "windows" {
symlinkPath += ".exe"
newPath += ".exe"
if runtime.GOOS == windows {
symlinkPath += exe
newPath += exe
}

prevNewPath := prevSymlinkPath()
Expand All @@ -53,7 +58,7 @@ func prevSymlinkPath() string {
agentPrevName := agentName + ".prev"

// handle windows suffixes
if runtime.GOOS == "windows" {
if runtime.GOOS == windows {
agentPrevName = agentName + ".exe.prev"
}

Expand Down
20 changes: 10 additions & 10 deletions internal/pkg/agent/application/upgrade/step_unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"archive/tar"
"archive/zip"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -25,13 +23,13 @@ import (
)

// unpack unpacks archive correctly, skips root (symlink, config...) unpacks data/*
func (u *Upgrader) unpack(ctx context.Context, version, archivePath string) (string, error) {
func (u *Upgrader) unpack(version, archivePath string) (string, error) {
// unpack must occur in directory that holds the installation directory
// or the extraction will be double nested
var hash string
var err error
if runtime.GOOS == "windows" {
hash, err = unzip(u.log, version, archivePath)
if runtime.GOOS == windows {
hash, err = unzip(u.log, archivePath)
} else {
hash, err = untar(u.log, version, archivePath)
}
Expand All @@ -45,7 +43,7 @@ func (u *Upgrader) unpack(ctx context.Context, version, archivePath string) (str
return hash, nil
}

func unzip(log *logger.Logger, version string, archivePath string) (string, error) {
func unzip(log *logger.Logger, archivePath string) (string, error) {
var hash, rootDir string
r, err := zip.OpenReader(archivePath)
if err != nil {
Expand All @@ -69,7 +67,7 @@ func unzip(log *logger.Logger, version string, archivePath string) (string, erro
//get hash
fileName := strings.TrimPrefix(f.Name, fileNamePrefix)
if fileName == agentCommitFile {
hashBytes, err := ioutil.ReadAll(rc)
hashBytes, err := io.ReadAll(rc)
if err != nil || len(hashBytes) < hashLen {
return err
}
Expand All @@ -87,10 +85,10 @@ func unzip(log *logger.Logger, version string, archivePath string) (string, erro

if f.FileInfo().IsDir() {
log.Debugw("Unpacking directory", "archive", "zip", "file.path", path)
os.MkdirAll(path, f.Mode())
_ = os.MkdirAll(path, f.Mode())
} else {
log.Debugw("Unpacking file", "archive", "zip", "file.path", path)
os.MkdirAll(filepath.Dir(path), f.Mode())
_ = os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
Expand All @@ -101,6 +99,7 @@ func unzip(log *logger.Logger, version string, archivePath string) (string, erro
}
}()

//nolint:gosec // legacy
if _, err = io.Copy(f, rc); err != nil {
return err
}
Expand Down Expand Up @@ -163,7 +162,7 @@ func untar(log *logger.Logger, version string, archivePath string) (string, erro
fileName := strings.TrimPrefix(f.Name, fileNamePrefix)

if fileName == agentCommitFile {
hashBytes, err := ioutil.ReadAll(tr)
hashBytes, err := io.ReadAll(tr)
if err != nil || len(hashBytes) < hashLen {
return "", err
}
Expand Down Expand Up @@ -200,6 +199,7 @@ func untar(log *logger.Logger, version string, archivePath string) (string, erro
return "", errors.New(err, "TarInstaller: creating file "+abs, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, abs))
}

//nolint:gosec // legacy
_, err = io.Copy(wf, tr)
if closeErr := wf.Close(); closeErr != nil && err == nil {
err = closeErr
Expand Down
7 changes: 3 additions & 4 deletions internal/pkg/agent/application/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package upgrade
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -151,7 +150,7 @@ func (u *Upgrader) Upgrade(ctx context.Context, a Action, reexecNow bool) (_ ree
return nil, err
}

newHash, err := u.unpack(ctx, a.Version(), archivePath)
newHash, err := u.unpack(a.Version(), archivePath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -306,7 +305,7 @@ func copyActionStore(log *logger.Logger, newHash string) error {
for _, currentActionStorePath := range storePaths {
newActionStorePath := filepath.Join(newHome, filepath.Base(currentActionStorePath))
log.Debugw("Copying action store path", "from", currentActionStorePath, "to", newActionStorePath)
currentActionStore, err := ioutil.ReadFile(currentActionStorePath)
currentActionStore, err := os.ReadFile(currentActionStorePath)
if os.IsNotExist(err) {
// nothing to copy
continue
Expand All @@ -315,7 +314,7 @@ func copyActionStore(log *logger.Logger, newHash string) error {
return err
}

if err := ioutil.WriteFile(newActionStorePath, currentActionStore, 0600); err != nil {
if err := os.WriteFile(newActionStorePath, currentActionStore, 0600); err != nil {
return err
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/agent/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package install

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -82,7 +81,7 @@ func Install(cfgFile string) error {
err = os.MkdirAll(filepath.Dir(paths.ShellWrapperPath), 0755)
if err == nil {
//nolint: gosec // this is intended to be an executable shell script, not chaning the permissions for the linter
err = ioutil.WriteFile(paths.ShellWrapperPath, []byte(paths.ShellWrapper), 0755)
err = os.WriteFile(paths.ShellWrapperPath, []byte(paths.ShellWrapper), 0755)
}
if err != nil {
return errors.New(
Expand Down

0 comments on commit 7f35077

Please sign in to comment.