Skip to content

Commit

Permalink
Merge pull request #12 from ML-PA-Consulting-GmbH/fix/add-user
Browse files Browse the repository at this point in the history
feat: update ssh keys even for existing users
  • Loading branch information
tokurz authored Aug 14, 2024
2 parents 702db0d + c733856 commit 9a46460
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,27 @@ func AddUser(name string, opts *AddUserOptions) error {
return fmt.Errorf("cannot add user %q: name contains invalid characters", name)
}

cmdStr := []string{
"adduser",
"--force-badname",
"--gecos", opts.Gecos,
"--disabled-password",
}
if opts.ExtraUsers {
cmdStr = append(cmdStr, "--extrausers")
}
cmdStr = append(cmdStr, name)
// Does user already exist?
_, err := userLookup(name)
if err != nil {
fmt.Printf("userLookup failed, assuming '%s' is a new user. Lookup error: %s\n", name, err)
cmdStr := []string{
"adduser",
"--force-badname",
"--gecos", opts.Gecos,
"--disabled-password",
}
if opts.ExtraUsers {
cmdStr = append(cmdStr, "--extrausers")
}
cmdStr = append(cmdStr, name)

cmd := exec.Command(cmdStr[0], cmdStr[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("adduser failed with: %s", OutputErr(output, err))
cmd := exec.Command(cmdStr[0], cmdStr[1:]...)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("adduser failed with: %s", OutputErr(output, err))
}
} else {
fmt.Printf("userLookup succeeded, assuming '%s' already exists\n", name)
}

if opts.Sudoer {
Expand Down

0 comments on commit 9a46460

Please sign in to comment.