Skip to content

Commit

Permalink
add userAbsent code
Browse files Browse the repository at this point in the history
  • Loading branch information
taigrr committed Nov 7, 2023
1 parent 19489fa commit 7eece77
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ingredients/user/userAbsent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package user

import (
"context"
"errors"
"os/exec"

"github.com/gogrlx/grlx/types"
)

func (u User) absent(ctx context.Context, test bool) (types.Result, error) {
var result types.Result
result.Succeeded = true
result.Failed = false
userName, ok := u.params["name"].(string)
if !ok {
result.Failed = true
result.Succeeded = false
return result, errors.New("invalid user; name must be a string")
}
if userExists(userName) {
if test {
return types.Result{
Succeeded: true,
Failed: false,
Notes: append(result.Notes, types.SimpleNote("user "+userName+" would be deleted")),
}, nil
}
cmd := exec.CommandContext(ctx, "userdel", userName)
err := cmd.Run()
if err != nil {
result.Failed = true
result.Succeeded = false
result.Notes = append(result.Notes, types.SimpleNote("user "+userName+" could not be deleted"))
result.Notes = append(result.Notes, types.SimpleNote(err.Error()))
return result, err
}
if !userExists(userName) {
result.Notes = append(result.Notes, types.SimpleNote("user "+userName+" deleted"))
return result, nil
}
result.Failed = true
result.Succeeded = false
result.Notes = append(result.Notes, types.SimpleNote("user "+userName+" could not be deleted"))
return result, errors.New("user " + userName + " could not be deleted")

}
result.Notes = append(result.Notes, types.SimpleNote("user "+userName+" already absent, nothing to do"))
return result, nil
}

0 comments on commit 7eece77

Please sign in to comment.