Skip to content

Commit

Permalink
Added support to delete users via import csv
Browse files Browse the repository at this point in the history
  • Loading branch information
Toms Zacharia authored and Toms Zacharia committed Nov 12, 2024
1 parent e6e5ad9 commit 1021507
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,26 @@ var listUserCmd = &cobra.Command{
Example: `
opslevel list user
opslevel list user --ignore-deactivated
opslevel list user --deactivated
opslevel list user -o json | jq 'map({"key": .Name, "value": .Role}) | from_entries'
`,
Run: func(cmd *cobra.Command, args []string) {
// payloadVars should remain nil if '--ignore-deactivated' not set
// payloadVars should remain nil if '--ignore-deactivated' or --deactivated not set
var payloadVars *opslevel.PayloadVariables

ignoreDeactivated, err := cmd.Flags().GetBool("ignore-deactivated")
// deactivated, err := cmd.Flags().GetBool("deactivated")
cobra.CheckErr(err)

client := getClientGQL()
if ignoreDeactivated {
payloadVars = client.InitialPageVariablesPointer().WithoutDeactivedUsers()
}

//if deactivated {
// payloadVars = client.InitialPageVariablesPointer().DeactivedUsers()
//}

resp, err := getClientGQL().ListUsers(payloadVars)
cobra.CheckErr(err)
list := resp.Nodes
Expand Down Expand Up @@ -215,9 +221,45 @@ EOF
},
}

var bulkDeleteUsersCmd = &cobra.Command{
Use: "delete users",
Aliases: []string{"bulk delete users"},
Short: "Deletes users from a CSV",
Long: `Deletes a list of users from a CSV file with the column headers:
Email`,
Example: `
cat << EOF | opslevel import delete users -f -
Email
[email protected]
[email protected]
[email protected]
EOF
`,
Run: func(cmd *cobra.Command, args []string) {
reader, err := readImportFilepathAsCSV()
cobra.CheckErr(err)
for reader.Rows() {
email := reader.Text("Email")
if email == "" {
log.Error().Msgf("user has invalid email '%s'", email)
continue
} else {
err := getClientGQL().DeleteUser(email)
if err != nil {
log.Error().Err(err).Msgf("error deleting user '%s'", email)
continue
} else {
log.Info().Msgf("deleted user '%s'", email)
}
}
}
},
}

func init() {
createUserCmd.Flags().Bool("skip-welcome-email", false, "If this flag is set the welcome e-mail will be skipped from being sent")
listUserCmd.Flags().Bool("ignore-deactivated", false, "If this flag is set only return active users")
//listUserCmd.Flags().Bool("deactivated", false, "If this flag is set only return deactivated users")

exampleCmd.AddCommand(exampleUserCmd)
createCmd.AddCommand(createUserCmd)
Expand All @@ -226,4 +268,5 @@ func init() {
listCmd.AddCommand(listUserCmd)
deleteCmd.AddCommand(deleteUserCmd)
importCmd.AddCommand(importUsersCmd)
importCmd.AddCommand(bulkDeleteUsersCmd)
}

0 comments on commit 1021507

Please sign in to comment.