-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete_user.go
More file actions
61 lines (55 loc) · 1.4 KB
/
delete_user.go
File metadata and controls
61 lines (55 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cmd
import (
"fmt"
"strconv"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
)
var deleteUserCmd = &cobra.Command{
Use: "user",
Aliases: []string{"u"},
SuggestFor: []string{"users"},
Short: "Delete a Gitlab user by specifying the username",
Example: `# delete a user by username
gitlabctl delete user john.smith
# delete a user with user id (15)
gitlabctl delete user 15`,
Args: cobra.ExactArgs(1),
SilenceErrors: true,
SilenceUsage: true,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return deleteUser(args[0])
},
}
func init() {
deleteCmd.AddCommand(deleteUserCmd)
}
// deleteUser can accept a username or user id and deletes it
func deleteUser(username string) error {
git, err := newGitlabClient()
if err != nil {
return err
}
id, err := strconv.Atoi(username)
// if there is an error, the username is not a number
// therefore, search the username's user id, and then assign it to id
if err != nil {
users, _, err2 := git.Users.ListUsers(&gitlab.ListUsersOptions{
Username: gitlab.String(username),
})
if err2 != nil {
return err2
}
if len(users) < 1 {
return fmt.Errorf("username %s not found", username)
}
id = users[0].ID
}
_, err = git.Users.DeleteUser(id)
if err != nil {
return err
}
fmt.Printf("User (%s) with id (%d) has been deleted\n", username, id)
return nil
}