Skip to content

Commit

Permalink
feat: update ssh keys even for existing users
Browse files Browse the repository at this point in the history
  • Loading branch information
florian.metzger-noel committed Aug 14, 2024
1 parent 702db0d commit c733856
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 c733856

Please sign in to comment.