From 47bf322f281c2a8d9bcc61585b62b83edfa82036 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 8 Oct 2018 16:06:28 +0200 Subject: [PATCH] Lint: Handle several linter errors Signed-off-by: Knut Ahlers --- bufferedLineWriter.go | 10 ++++---- configfile.go | 56 ++++++++++++++++++++++++------------------- main.go | 13 ++++++---- notification.go | 2 +- 4 files changed, 47 insertions(+), 34 deletions(-) diff --git a/bufferedLineWriter.go b/bufferedLineWriter.go index 3d4c905..ca752dd 100644 --- a/bufferedLineWriter.go +++ b/bufferedLineWriter.go @@ -15,9 +15,11 @@ func newMessageChanWriter(outputChannel chan string) *messageChanWriter { } } -func (m *messageChanWriter) Write(p []byte) (n int, err error) { - n = len(p) - err = nil +func (m *messageChanWriter) Write(p []byte) (int, error) { + var ( + n = len(p) + err error + ) m.buffer = append(m.buffer, p...) if strings.Contains(string(m.buffer), "\n") { @@ -28,5 +30,5 @@ func (m *messageChanWriter) Write(p []byte) (n int, err error) { m.buffer = []byte(lines[len(lines)-1]) } - return + return n, err } diff --git a/configfile.go b/configfile.go index 20e6b42..a60450d 100644 --- a/configfile.go +++ b/configfile.go @@ -160,7 +160,7 @@ func loadConfigFile(in io.Reader) (*configFile, error) { return nil, err } - hostname, _ := os.Hostname() + hostname, _ := os.Hostname() // #nosec G104 res := &configFile{ Hostname: hostname, @@ -221,8 +221,8 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine [] } else if len(argv) == 2 { dest = argv[1] } else { - err = errors.New("You need to specify one ore more parameters. See help message.") - return + err = errors.New("You need to specify one or more parameters: See help message") + return commandLine, env, logfilter, err } commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, restoreFile) @@ -237,8 +237,8 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine [] case commandRemove: commandLine, env, err = c.generateRemoveCommand() default: - err = fmt.Errorf("Did not understand command '%s', please see 'help' for details what to do.", command) - return + err = fmt.Errorf("Did not understand command '%s', please see 'help' for details what to do", command) + return commandLine, env, logfilter, err } // Add destination credentials @@ -248,20 +248,24 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine [] commandLine = c.cleanSlice(commandLine) env = c.cleanSlice(env) - return + return commandLine, env, logfilter, err } -func (c *configFile) cleanSlice(in []string) (out []string) { +func (c *configFile) cleanSlice(in []string) []string { + out := []string{} + for _, i := range in { if i != "" { out = append(out, i) } } - return + return out } -func (c *configFile) generateCredentialExport() (env []string) { +func (c *configFile) generateCredentialExport() []string { + env := []string{} + if c.AWS.AccessKeyID != "" { env = append(env, "AWS_ACCESS_KEY_ID="+c.AWS.AccessKeyID) env = append(env, "AWS_SECRET_ACCESS_KEY="+c.AWS.SecretAccessKey) @@ -280,11 +284,11 @@ func (c *configFile) generateCredentialExport() (env []string) { env = append(env, "FTP_PASSWORD="+c.FTPPassword) } - return + return env } -func (c *configFile) generateRemoveCommand() (commandLine []string, env []string, err error) { - var tmpArg, tmpEnv []string +func (c *configFile) generateRemoveCommand() ([]string, []string, error) { + var commandLine, env, tmpArg, tmpEnv []string // Assemble command commandLine = append(commandLine, c.Cleanup.Type, c.Cleanup.Value) // Static Options @@ -298,11 +302,11 @@ func (c *configFile) generateRemoveCommand() (commandLine []string, env []string // Remote repo commandLine = append(commandLine, c.Destination) - return + return commandLine, env, nil } -func (c *configFile) generateLiteCommand(option, time string, addTime bool) (commandLine []string, env []string, err error) { - var tmpArg, tmpEnv []string +func (c *configFile) generateLiteCommand(option, time string, addTime bool) ([]string, []string, error) { + var commandLine, env, tmpArg, tmpEnv []string // Assemble command commandLine = append(commandLine, option) // Static Options @@ -317,11 +321,11 @@ func (c *configFile) generateLiteCommand(option, time string, addTime bool) (com // Remote repo commandLine = append(commandLine, c.Destination) - return + return commandLine, env, nil } -func (c *configFile) generateFullCommand(option, time, root, dest string, addTime bool, restoreFile string) (commandLine []string, env []string, err error) { - var tmpArg, tmpEnv []string +func (c *configFile) generateFullCommand(option, time, root, dest string, addTime bool, restoreFile string) ([]string, []string, error) { + var commandLine, env, tmpArg, tmpEnv []string // Assemble command commandLine = append(commandLine, option) // Static Options @@ -345,10 +349,12 @@ func (c *configFile) generateFullCommand(option, time, root, dest string, addTim // Source / Destination commandLine = append(commandLine, root, dest) - return + return commandLine, env, nil } -func (c *configFile) generateIncludeExclude() (arguments []string, env []string) { +func (c *configFile) generateIncludeExclude() ([]string, []string) { + var arguments, env []string + if c.ExcludeDeviceFiles { arguments = append(arguments, "--exclude-device-files") } @@ -369,13 +375,15 @@ func (c *configFile) generateIncludeExclude() (arguments []string, env []string) arguments = append(arguments, "--exclude=**") } - return + return arguments, env } -func (c *configFile) generateEncryption(command string) (arguments []string, env []string) { +func (c *configFile) generateEncryption(command string) ([]string, []string) { + var arguments, env []string + if !c.Encryption.Enable { arguments = append(arguments, "--no-encryption") - return + return arguments, env } if c.Encryption.Passphrase != "" { @@ -398,5 +406,5 @@ func (c *configFile) generateEncryption(command string) (arguments []string, env arguments = append(arguments, "--encrypt-secret-keyring="+c.Encryption.SecretKeyRing) } - return + return arguments, env } diff --git a/main.go b/main.go index 536102b..89f905a 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ var ( DryRun bool `flag:"dry-run,n" default:"false" description:"Do a test-run without changes"` Debug bool `flag:"debug,d" default:"false" description:"Print duplicity commands to output"` - Silent bool `flag:"silent,s" default:"false" description:"Do not print to stdout, only write to logfile (for example usefull for crons)"` + Silent bool `flag:"silent,s" default:"false" description:"Do not print to stdout, only write to logfile (for example useful for crons)"` VersionAndExit bool `flag:"version" default:"false" description:"Print version and exit"` }{} @@ -86,7 +86,7 @@ func main() { // If no command is passed assume we're requesting "help" argv := rconfig.Args() if len(argv) == 1 || argv[1] == "help" { - helptext, _ := Asset("help.txt") + helptext, _ := Asset("help.txt") // #nosec G104 fmt.Println(string(helptext)) return } @@ -103,7 +103,10 @@ func main() { } // Initialize logfile - os.MkdirAll(config.LogDirectory, 0755) + if err := os.MkdirAll(config.LogDirectory, 0750); err != nil { + log.Fatalf("Unable to create log dir: %s", err) + } + logFilePath := path.Join(config.LogDirectory, time.Now().Format("duplicity-backup_2006-01-02_15-04-05.txt")) if logFile, err = os.Create(logFilePath); err != nil { log.Fatalf("Unable to open logfile %s: %s", logFilePath, err) @@ -113,7 +116,7 @@ func main() { logf("++++ duplicity-backup %s started with command '%s'", version, argv[1]) if err := lock.TryLock(); err != nil { - logf("Could not aquire lock: %s", err) + logf("Could not acquire lock: %s", err) return } defer lock.Unlock() @@ -177,7 +180,7 @@ func execute(config *configFile, argv []string) error { }(msgChan, logFilter) output := newMessageChanWriter(msgChan) - cmd := exec.Command(duplicityBinary, commandLine...) + cmd := exec.Command(duplicityBinary, commandLine...) // #nosec G204 cmd.Stdout = output cmd.Stderr = output cmd.Env = envMapToList(env) diff --git a/notification.go b/notification.go index 760d692..d32bc97 100644 --- a/notification.go +++ b/notification.go @@ -79,7 +79,7 @@ func (c *configFile) notifyMonDash(success bool, err error) error { c.Hostname, ) - req, _ := http.NewRequest(http.MethodPut, url, buf) + req, _ := http.NewRequest(http.MethodPut, url, buf) // #nosec G104 req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", c.Notifications.MonDash.Token) res, err := http.DefaultClient.Do(req)