diff --git a/cmd/dwelling-upload-clear/main.go b/cmd/dwelling-upload-clear/main.go index 7d094f2..3e4d152 100644 --- a/cmd/dwelling-upload-clear/main.go +++ b/cmd/dwelling-upload-clear/main.go @@ -18,6 +18,26 @@ import ( var configPath *string = flag.String("conf", "config.yaml", "path to configuration file") var logToStdout *bool = flag.Bool("log-stdout", false, "write logs to stdout") +func substituteUser(username string) error { + dwu, err := user.Lookup(username) + if err != nil { + return err + } + + dwuid, err := strconv.Atoi(dwu.Uid) + if err != nil { + return err + } + + if os.Getuid() != dwuid { + if err := syscall.Setuid(dwuid); err != nil { + return err + } + } + + return nil +} + func main() { flag.Parse() @@ -26,23 +46,10 @@ func main() { log.Fatalln(err) } - // Substitute user to the one in config - dwu, err := user.Lookup(config.User) - if err != nil { + if err := substituteUser(config.User); err != nil { log.Fatalln(err) } - dwuid, err := strconv.Atoi(dwu.Uid) - if err != nil { - log.Fatalln(err) - } - - if os.Getuid() != dwuid { - if err := syscall.Setuid(dwuid); err != nil { - log.Fatalln(err) - } - } - if *logToStdout { config.Log.ToStdout = true }