-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to delete users via import csv
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 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) | ||
|
@@ -226,4 +268,5 @@ func init() { | |
listCmd.AddCommand(listUserCmd) | ||
deleteCmd.AddCommand(deleteUserCmd) | ||
importCmd.AddCommand(importUsersCmd) | ||
importCmd.AddCommand(bulkDeleteUsersCmd) | ||
} |