Skip to content

Commit

Permalink
add missing nil checks on group and user lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
taigrr committed May 26, 2024
1 parent 60a2027 commit 42a8b65
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ingredients/group/groupPresent.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (g Group) present(ctx context.Context, test bool) (types.Result, error) {
result.Changed = true
return result, nil
}
if groupByName.Gid != gid {
if groupByName == nil || groupByName.Gid != gid {
cmd := exec.CommandContext(ctx, "groupmod", args...)
if test {
result.Succeeded = true
Expand Down
5 changes: 2 additions & 3 deletions ingredients/user/userPresent.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func (u User) present(ctx context.Context, test bool) (types.Result, error) {
userCmd = "useradd"
}
args := []string{userName}
if uid != "" && uid != user.Uid {
if uid != "" && user == nil || uid != user.Uid {
args = append(args, "-u"+uid)
}
if gid != "" && gid != user.Gid {
if gid != "" && user == nil || gid != user.Gid {
args = append(args, "-g"+gid)
}
if shell != "" {
Expand All @@ -72,7 +72,6 @@ func (u User) present(ctx context.Context, test bool) (types.Result, error) {
return result, nil
}
err = cmd.Run()

if err != nil {
result.Failed = true
result.Succeeded = false
Expand Down
11 changes: 11 additions & 0 deletions testing/recipes/userCreation.grlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
steps:
create void user:
user.present:
- name: void
- uid: "1000"
- gid: "100"
- home: /home/void
- shell: /usr/bin/zsh
- groups:
- wheel
- docker

0 comments on commit 42a8b65

Please sign in to comment.