1
0
Fork 0
mirror of https://github.com/Luzifer/duplicity-backup.git synced 2024-11-08 15:10:06 +00:00

Lint: Handle several linter errors

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-10-08 16:06:28 +02:00
parent 7041537209
commit 47bf322f28
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 47 additions and 34 deletions

View file

@ -15,9 +15,11 @@ func newMessageChanWriter(outputChannel chan string) *messageChanWriter {
} }
} }
func (m *messageChanWriter) Write(p []byte) (n int, err error) { func (m *messageChanWriter) Write(p []byte) (int, error) {
n = len(p) var (
err = nil n = len(p)
err error
)
m.buffer = append(m.buffer, p...) m.buffer = append(m.buffer, p...)
if strings.Contains(string(m.buffer), "\n") { 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]) m.buffer = []byte(lines[len(lines)-1])
} }
return return n, err
} }

View file

@ -160,7 +160,7 @@ func loadConfigFile(in io.Reader) (*configFile, error) {
return nil, err return nil, err
} }
hostname, _ := os.Hostname() hostname, _ := os.Hostname() // #nosec G104
res := &configFile{ res := &configFile{
Hostname: hostname, Hostname: hostname,
@ -221,8 +221,8 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine []
} else if len(argv) == 2 { } else if len(argv) == 2 {
dest = argv[1] dest = argv[1]
} else { } else {
err = errors.New("You need to specify one ore more parameters. See help message.") err = errors.New("You need to specify one or more parameters: See help message")
return return commandLine, env, logfilter, err
} }
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, restoreFile) 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: case commandRemove:
commandLine, env, err = c.generateRemoveCommand() commandLine, env, err = c.generateRemoveCommand()
default: default:
err = fmt.Errorf("Did not understand command '%s', please see 'help' for details what to do.", command) err = fmt.Errorf("Did not understand command '%s', please see 'help' for details what to do", command)
return return commandLine, env, logfilter, err
} }
// Add destination credentials // Add destination credentials
@ -248,20 +248,24 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine []
commandLine = c.cleanSlice(commandLine) commandLine = c.cleanSlice(commandLine)
env = c.cleanSlice(env) 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 { for _, i := range in {
if i != "" { if i != "" {
out = append(out, 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 != "" { if c.AWS.AccessKeyID != "" {
env = append(env, "AWS_ACCESS_KEY_ID="+c.AWS.AccessKeyID) env = append(env, "AWS_ACCESS_KEY_ID="+c.AWS.AccessKeyID)
env = append(env, "AWS_SECRET_ACCESS_KEY="+c.AWS.SecretAccessKey) 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) env = append(env, "FTP_PASSWORD="+c.FTPPassword)
} }
return return env
} }
func (c *configFile) generateRemoveCommand() (commandLine []string, env []string, err error) { func (c *configFile) generateRemoveCommand() ([]string, []string, error) {
var tmpArg, tmpEnv []string var commandLine, env, tmpArg, tmpEnv []string
// Assemble command // Assemble command
commandLine = append(commandLine, c.Cleanup.Type, c.Cleanup.Value) commandLine = append(commandLine, c.Cleanup.Type, c.Cleanup.Value)
// Static Options // Static Options
@ -298,11 +302,11 @@ func (c *configFile) generateRemoveCommand() (commandLine []string, env []string
// Remote repo // Remote repo
commandLine = append(commandLine, c.Destination) 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) { func (c *configFile) generateLiteCommand(option, time string, addTime bool) ([]string, []string, error) {
var tmpArg, tmpEnv []string var commandLine, env, tmpArg, tmpEnv []string
// Assemble command // Assemble command
commandLine = append(commandLine, option) commandLine = append(commandLine, option)
// Static Options // Static Options
@ -317,11 +321,11 @@ func (c *configFile) generateLiteCommand(option, time string, addTime bool) (com
// Remote repo // Remote repo
commandLine = append(commandLine, c.Destination) 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) { func (c *configFile) generateFullCommand(option, time, root, dest string, addTime bool, restoreFile string) ([]string, []string, error) {
var tmpArg, tmpEnv []string var commandLine, env, tmpArg, tmpEnv []string
// Assemble command // Assemble command
commandLine = append(commandLine, option) commandLine = append(commandLine, option)
// Static Options // Static Options
@ -345,10 +349,12 @@ func (c *configFile) generateFullCommand(option, time, root, dest string, addTim
// Source / Destination // Source / Destination
commandLine = append(commandLine, root, dest) 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 { if c.ExcludeDeviceFiles {
arguments = append(arguments, "--exclude-device-files") arguments = append(arguments, "--exclude-device-files")
} }
@ -369,13 +375,15 @@ func (c *configFile) generateIncludeExclude() (arguments []string, env []string)
arguments = append(arguments, "--exclude=**") 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 { if !c.Encryption.Enable {
arguments = append(arguments, "--no-encryption") arguments = append(arguments, "--no-encryption")
return return arguments, env
} }
if c.Encryption.Passphrase != "" { 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) arguments = append(arguments, "--encrypt-secret-keyring="+c.Encryption.SecretKeyRing)
} }
return return arguments, env
} }

13
main.go
View file

@ -26,7 +26,7 @@ var (
DryRun bool `flag:"dry-run,n" default:"false" description:"Do a test-run without changes"` 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"` 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"` 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" // If no command is passed assume we're requesting "help"
argv := rconfig.Args() argv := rconfig.Args()
if len(argv) == 1 || argv[1] == "help" { if len(argv) == 1 || argv[1] == "help" {
helptext, _ := Asset("help.txt") helptext, _ := Asset("help.txt") // #nosec G104
fmt.Println(string(helptext)) fmt.Println(string(helptext))
return return
} }
@ -103,7 +103,10 @@ func main() {
} }
// Initialize logfile // 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")) 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 { if logFile, err = os.Create(logFilePath); err != nil {
log.Fatalf("Unable to open logfile %s: %s", logFilePath, err) 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]) logf("++++ duplicity-backup %s started with command '%s'", version, argv[1])
if err := lock.TryLock(); err != nil { if err := lock.TryLock(); err != nil {
logf("Could not aquire lock: %s", err) logf("Could not acquire lock: %s", err)
return return
} }
defer lock.Unlock() defer lock.Unlock()
@ -177,7 +180,7 @@ func execute(config *configFile, argv []string) error {
}(msgChan, logFilter) }(msgChan, logFilter)
output := newMessageChanWriter(msgChan) output := newMessageChanWriter(msgChan)
cmd := exec.Command(duplicityBinary, commandLine...) cmd := exec.Command(duplicityBinary, commandLine...) // #nosec G204
cmd.Stdout = output cmd.Stdout = output
cmd.Stderr = output cmd.Stderr = output
cmd.Env = envMapToList(env) cmd.Env = envMapToList(env)

View file

@ -79,7 +79,7 @@ func (c *configFile) notifyMonDash(success bool, err error) error {
c.Hostname, 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("Content-Type", "application/json")
req.Header.Set("Authorization", c.Notifications.MonDash.Token) req.Header.Set("Authorization", c.Notifications.MonDash.Token)
res, err := http.DefaultClient.Do(req) res, err := http.DefaultClient.Do(req)