diff --git a/.golangci.yaml b/.golangci.yaml index 1cd670d..6279e86 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -67,7 +67,6 @@ linters: disable-all: true enable: - bodyclose - - deadcode - dogsled - dupl - errcheck @@ -91,13 +90,11 @@ linters: - nolintlint - rowserrcheck - staticcheck - - structcheck - stylecheck - typecheck - unconvert - unparam - unused - - varcheck - whitespace # don't enable: diff --git a/internal/cmd/vault/decrypt_file.go b/internal/cmd/vault/decrypt_file.go index 646552a..5173063 100644 --- a/internal/cmd/vault/decrypt_file.go +++ b/internal/cmd/vault/decrypt_file.go @@ -24,7 +24,7 @@ package vault import ( "fmt" - "io/ioutil" + "os" "github.com/spf13/cobra" @@ -34,8 +34,9 @@ import ( var deOutputFile string -//nolint:dupl // decryptFileCmd represents the vault decrypt-file command +// +//nolint:dupl var decryptFileCmd = &cobra.Command{ Use: "decrypt-file FILENAME", Short: "Decrypt vault encrypted file", @@ -93,7 +94,7 @@ func init() { } func decryptFile(file, vaultPass string) (string, error) { - p, err := ioutil.ReadFile(file) + p, err := os.ReadFile(file) if err != nil { return "", err } diff --git a/internal/cmd/vault/encrypt_file.go b/internal/cmd/vault/encrypt_file.go index 6857929..7fbe5bb 100644 --- a/internal/cmd/vault/encrypt_file.go +++ b/internal/cmd/vault/encrypt_file.go @@ -25,7 +25,6 @@ package vault import ( "bytes" "fmt" - "io/ioutil" "os" "github.com/spf13/cobra" @@ -36,8 +35,9 @@ import ( var outputFile string -//nolint:dupl // encryptFileCmd represents the vault encrypt-file command +// +//nolint:dupl var encryptFileCmd = &cobra.Command{ Use: "encrypt-file FILENAME", Short: "Encrypt a file", @@ -110,7 +110,7 @@ func handleOutput(content, originalFile, newFile string) { } func encryptFile(file, vaultPass string) (string, error) { - p, err := ioutil.ReadFile(file) + p, err := os.ReadFile(file) if err != nil { return "", err } diff --git a/internal/cmd/vault/vault.go b/internal/cmd/vault/vault.go index 0a3254f..3860ea3 100644 --- a/internal/cmd/vault/vault.go +++ b/internal/cmd/vault/vault.go @@ -25,7 +25,6 @@ package vault import ( "errors" "fmt" - "io/ioutil" "os" "os/exec" "strings" @@ -166,7 +165,7 @@ func getVaultPasswordFromFile() string { return vaultPass } - passwordContent, err := ioutil.ReadFile(vaultPassFile) + passwordContent, err := os.ReadFile(vaultPassFile) if err != nil { err = fmt.Errorf("read vault password file '%s' failed: %w", vaultPassFile, err) } diff --git a/internal/pkg/sshtask/sshtask.go b/internal/pkg/sshtask/sshtask.go index f43f974..039a21b 100644 --- a/internal/pkg/sshtask/sshtask.go +++ b/internal/pkg/sshtask/sshtask.go @@ -492,8 +492,8 @@ func (t *Task) getAllHosts() ([]*batchssh.Host, error) { if len(v.Keys) != 0 { keys := parseItentityFiles(v.Keys) - assignRealPass(&v.Passphrase, v.Alias, "passphrase") - sshSigners := getSigners(keys, v.Passphrase, "Individual") + passphrase := getRealPass(v.Passphrase, v.Alias, "passphrase") + sshSigners := getSigners(keys, passphrase, "Individual") if len(sshSigners) == 0 { log.Debugf("Individual Auth: no valid individual identity files for '%s'", v.Alias) } else { @@ -502,11 +502,11 @@ func (t *Task) getAllHosts() ([]*batchssh.Host, error) { } } + realPassword := "" if v.Password == "" { - v.Password = *t.defaultPass - assignRealPass(&v.Password, v.Alias, "password") + realPassword = getRealPass(*t.defaultPass, v.Alias, "password") } else { - assignRealPass(&v.Password, v.Alias, "password") + realPassword = getRealPass(v.Password, v.Alias, "password") hostSSHAuths = append(hostSSHAuths, ssh.Password(v.Password)) log.Debugf("Individual Auth: add individual password for '%s'", v.Alias) } @@ -518,7 +518,7 @@ func (t *Task) getAllHosts() ([]*batchssh.Host, error) { Host: v.Host, Port: v.Port, User: v.User, - Password: v.Password, + Password: realPassword, Keys: v.Keys, SSHAuths: hostSSHAuths, }) @@ -725,14 +725,14 @@ func getDefaultPassword(auth *configflags.Auth) string { log.Debugf("Default Auth: received password of user '%s' from commandline flag or configuration file", auth.User) } - assignRealPass(&password, "default", "password") + realPassword := getRealPass(password, "default", "password") if auth.AskPass { log.Debugf("Default Auth: ask for password of user '%s' by flag '-k/--auth.ask-pass'", auth.User) - password = getPasswordFromPrompt(auth.User) + realPassword = getPasswordFromPrompt(auth.User) } - return password + return realPassword } func parseItentityFiles(identityFiles []string) (keyFiles []string) { @@ -812,20 +812,22 @@ func getPasswordFromPrompt(loginUser string) string { return password } -func assignRealPass(pass *string, host, objectType string) { - var err error - - if aes.IsAES256CipherText(*pass) { +func getRealPass(pass string, host, objectType string) string { + if aes.IsAES256CipherText(pass) { vaultPass := vault.GetVaultPassword() - *pass, err = aes.AES256Decode(*pass, vaultPass) + realPass, err := aes.AES256Decode(pass, vaultPass) if err != nil { log.Debugf("Vault: decrypt %s for '%s' failed: %s", objectType, host, err) util.CheckErr(err) } log.Debugf("Vault: decrypt %s for '%s' success", objectType, host) + + return realPass } + + return pass } func deDuplicate(hosts []string) []string { diff --git a/pkg/aes/aes.go b/pkg/aes/aes.go index 6a2586e..2f2e73b 100644 --- a/pkg/aes/aes.go +++ b/pkg/aes/aes.go @@ -33,9 +33,10 @@ import ( // Encode plain text. // Param keyLen available values: 16, 24, 32. -// AES-128 uses a 128 bit key (16 bytes), -// AES-192 uses a 192 bit key (24 bytes), -// AES-256 uses a 256 bit key (32 bytes). +// +// AES-128 uses a 128 bit key (16 bytes), +// AES-192 uses a 192 bit key (24 bytes), +// AES-256 uses a 256 bit key (32 bytes). func Encode(plainText, key []byte, keyLen int) ([]byte, error) { if keyLen != 16 && keyLen != 24 && keyLen != 32 { return nil, errors.New("invalid key length, available length: 16, 24, 32") diff --git a/pkg/batchssh/fetch_zip.go b/pkg/batchssh/fetch_zip.go index d5ff774..e879322 100644 --- a/pkg/batchssh/fetch_zip.go +++ b/pkg/batchssh/fetch_zip.go @@ -66,7 +66,7 @@ fi`, log.Debugf("%s: zip '%s' cost %s", host.Host, srcFile, time.Since(timeStart)) - if err := fetchZipFile(ftpC, zippedFileFullpath, dstDir); err != nil { + if err = fetchZipFile(ftpC, zippedFileFullpath, dstDir); err != nil { log.Errorf("%s: fetch zip file '%s' failed: %s", host.Host, zippedFileFullpath, err) return err } diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index b8be37c..a37d4db 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -2,89 +2,89 @@ // // The traditional error handling idiom in Go is roughly akin to // -// if err != nil { -// return err -// } +// if err != nil { +// return err +// } // // which when applied recursively up the call stack results in error reports // without context or debugging information. The errors package allows // programmers to add context to the failure path in their code in a way // that does not destroy the original value of the error. // -// Adding context to an error +// # Adding context to an error // // The errors.Wrap function returns a new error that adds context to the // original error by recording a stack trace at the point Wrap is called, // together with the supplied message. For example // -// _, err := ioutil.ReadAll(r) -// if err != nil { -// return errors.Wrap(err, "read failed") -// } +// _, err := ioutil.ReadAll(r) +// if err != nil { +// return errors.Wrap(err, "read failed") +// } // // If additional control is required, the errors.WithStack and // errors.WithMessage functions destructure errors.Wrap into its component // operations: annotating an error with a stack trace and with a message, // respectively. // -// Retrieving the cause of an error +// # Retrieving the cause of an error // // Using errors.Wrap constructs a stack of errors, adding context to the // preceding error. Depending on the nature of the error it may be necessary // to reverse the operation of errors.Wrap to retrieve the original error // for inspection. Any error value which implements this interface // -// type causer interface { -// Cause() error -// } +// type causer interface { +// Cause() error +// } // // can be inspected by errors.Cause. errors.Cause will recursively retrieve // the topmost error that does not implement causer, which is assumed to be // the original cause. For example: // -// switch err := errors.Cause(err).(type) { -// case *MyError: -// // handle specifically -// default: -// // unknown error -// } +// switch err := errors.Cause(err).(type) { +// case *MyError: +// // handle specifically +// default: +// // unknown error +// } // // Although the causer interface is not exported by this package, it is // considered a part of its stable public interface. // -// Formatted printing of errors +// # Formatted printing of errors // // All error values returned from this package implement fmt.Formatter and can // be formatted by the fmt package. The following verbs are supported: // -// %s print the error. If the error has a Cause it will be -// printed recursively. -// %v see %s -// %+v extended format. Each Frame of the error's StackTrace will -// be printed in detail. +// %s print the error. If the error has a Cause it will be +// printed recursively. +// %v see %s +// %+v extended format. Each Frame of the error's StackTrace will +// be printed in detail. // -// Retrieving the stack trace of an error or wrapper +// # Retrieving the stack trace of an error or wrapper // // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are // invoked. This information can be retrieved with the following interface: // -// type stackTracer interface { -// StackTrace() errors.StackTrace -// } +// type stackTracer interface { +// StackTrace() errors.StackTrace +// } // // The returned errors.StackTrace type is defined as // -// type StackTrace []Frame +// type StackTrace []Frame // // The Frame type represents a call site in the stack trace. Frame supports // the fmt.Formatter interface that can be used for printing information about // the stack trace of this error. For example: // -// if err, ok := err.(stackTracer); ok { -// for _, f := range err.StackTrace() { -// fmt.Printf("%+s:%d\n", f, f) -// } -// } +// if err, ok := err.(stackTracer); ok { +// for _, f := range err.StackTrace() { +// fmt.Printf("%+s:%d\n", f, f) +// } +// } // // Although the stackTracer interface is not exported by this package, it is // considered a part of its stable public interface. @@ -356,9 +356,9 @@ func (w *withCode) Unwrap() error { return w.cause } // An error value has a cause if it implements the following // interface: // -// type causer interface { -// Cause() error -// } +// type causer interface { +// Cause() error +// } // // If the error does not implement Cause, the original error will // be returned. If the error is nil, nil will be returned without further diff --git a/pkg/errors/format.go b/pkg/errors/format.go index 3df370d..2a78485 100644 --- a/pkg/errors/format.go +++ b/pkg/errors/format.go @@ -16,27 +16,31 @@ type formatInfo struct { stack *stack } -//nolint:lll // Format implements fmt.Formatter. https://golang.org/pkg/fmt/#hdr-Printing // // Verbs: -// %s - Returns the user-safe error string mapped to the error code or -// ┊ the error message if none is specified. -// %v Alias for %s +// +// %s - Returns the user-safe error string mapped to the error code or +// ┊ the error message if none is specified. +// %v Alias for %s // // Flags: -// # JSON formatted output, useful for logging -// - Output caller details, useful for troubleshooting -// + Output full error stack details, useful for debugging +// +// # JSON formatted output, useful for logging +// - Output caller details, useful for troubleshooting +// + Output full error stack details, useful for debugging // // Examples: -// %s: error for internal read B -// %v: error for internal read B -// %-v: error for internal read B - #0 [/gobackend/main.go:12 (main.main)] (#100102) Internal Server Error -// %+v: error for internal read B - #0 [/gobackend/main.go:12 (main.main)] (#100102) Internal Server Error; error for internal read A - #1 [/go-web-demo/main.go:35 (main.newErrorB)] (#100104) Validation failed -// %#v: [{"error":"error for internal read B"}] -// %#-v: [{"caller":"#0 /gobackend/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"}] -// %#+v: [{"caller":"#0 /gobackend/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"},{"caller":"#1 /go-web-demo/main.go:35 (main.newErrorB)","error":"error for internal read A","message":"(#100104) Validation failed"}] +// +// %s: error for internal read B +// %v: error for internal read B +// %-v: error for internal read B - #0 [/gobackend/main.go:12 (main.main)] (#100102) Internal Server Error +// %+v: error for internal read B - #0 [/gobackend/main.go:12 (main.main)] (#100102) Internal Server Error; error for internal read A - #1 [/go-web-demo/main.go:35 (main.newErrorB)] (#100104) Validation failed +// %#v: [{"error":"error for internal read B"}] +// %#-v: [{"caller":"#0 /gobackend/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"}] +// %#+v: [{"caller":"#0 /gobackend/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"},{"caller":"#1 /go-web-demo/main.go:35 (main.newErrorB)","error":"error for internal read A","message":"(#100104) Validation failed"}] +// +//nolint:lll func (w *withCode) Format(state fmt.State, verb rune) { switch verb { case 'v': diff --git a/pkg/errors/stack.go b/pkg/errors/stack.go index 82e856f..accc5e8 100644 --- a/pkg/errors/stack.go +++ b/pkg/errors/stack.go @@ -58,16 +58,16 @@ func (f Frame) name() string { // Format formats the frame according to the fmt.Formatter interface. // -// %s source file -// %d source line -// %n function name -// %v equivalent to %s:%d +// %s source file +// %d source line +// %n function name +// %v equivalent to %s:%d // // Format accepts flags that alter the printing of some verbs, as follows: // -// %+s function name and path of source file relative to the compile time -// GOPATH separated by \n\t (\n\t) -// %+v equivalent to %+s:%d +// %+s function name and path of source file relative to the compile time +// GOPATH separated by \n\t (\n\t) +// %+v equivalent to %+s:%d func (f Frame) Format(s fmt.State, verb rune) { switch verb { case 's': @@ -106,12 +106,12 @@ type StackTrace []Frame // Format formats the stack of Frames according to the fmt.Formatter interface. // -// %s lists source files for each Frame in the stack -// %v lists the source file and line number for each Frame in the stack +// %s lists source files for each Frame in the stack +// %v lists the source file and line number for each Frame in the stack // // Format accepts flags that alter the printing of some verbs, as follows: // -// %+v Prints filename, function, and line number for each Frame in the stack. +// %+v Prints filename, function, and line number for each Frame in the stack. func (st StackTrace) Format(s fmt.State, verb rune) { switch verb { case 'v': diff --git a/pkg/inventory/inventory.go b/pkg/inventory/inventory.go index 41812ec..540bbad 100644 --- a/pkg/inventory/inventory.go +++ b/pkg/inventory/inventory.go @@ -24,7 +24,7 @@ package inventory import ( "fmt" - "io/ioutil" + "os" "strconv" "strings" @@ -291,7 +291,7 @@ func checkLine(line string) error { } func parse(file string) ([]string, error) { - content, err := ioutil.ReadFile(file) + content, err := os.ReadFile(file) if err != nil { return nil, err }