This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to add multiple users from file
- Loading branch information
1 parent
c773f73
commit 489bcb5
Showing
1 changed file
with
48 additions
and
12 deletions.
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 |
---|---|---|
@@ -1,31 +1,67 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/logrusorgru/aurora" | ||
"github.com/mainawycliffe/kamanda/firebase/auth" | ||
"github.com/mainawycliffe/kamanda/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// addUsersCmd represents the addUsers command | ||
var addUsersCmd = &cobra.Command{ | ||
Use: "addUsers", | ||
Aliases: []string{"add-users"}, | ||
Short: "Add multiple Firebase Auth Users from file - JSON or YAML Supported!", | ||
Short: "Add multiple users from file (JSON/YAML)", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("addUsers called") | ||
sourceFile, _ := cmd.Flags().GetString("source") | ||
sourceFileExtension, _ := cmd.Flags().GetString("extension") | ||
file, err := os.Stat(sourceFile) | ||
if err != nil && !os.IsNotExist(err) { | ||
utils.StdOutError("Source file doesn't exist!") | ||
os.Exit(1) | ||
} | ||
if file.IsDir() { | ||
utils.StdOutError("%s Source file is a directory not folder!", sourceFile) | ||
os.Exit(1) | ||
} | ||
usersToCreate := make([]auth.NewUser, 0) | ||
err = utils.UnmashalMarkupFile(sourceFile, sourceFileExtension, usersToCreate) | ||
if err != nil { | ||
utils.StdOutError("Error decoding your config file: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
failedAccountCreation := 0 | ||
for _, v := range usersToCreate { | ||
userRecord, err := auth.NewFirebaseUser(context.Background(), &v) | ||
if err != nil { | ||
utils.StdOutError("%s Failed!", v.Email) | ||
failedAccountCreation++ | ||
continue | ||
} | ||
utils.StdOutSuccess("✔✔ email: %s SUCCESS \t uid: %s \n", userRecord.Email, userRecord.UID) | ||
// @todo probably also add custom claims | ||
} | ||
if failedAccountCreation > 0 { | ||
os.Exit(1) | ||
} | ||
os.Exit(0) | ||
}, | ||
} | ||
|
||
func init() { | ||
authCmd.AddCommand(addUsersCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// addUsersCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// addUsersCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
addUsersCmd.Flags().StringP("source", "s", "", "file with list of users to create") | ||
if err := addUsersCmd.MarkFlagRequired("source"); err != nil { | ||
fmt.Print(aurora.Sprintf(aurora.Red("%s\n"), err.Error())) | ||
os.Exit(1) | ||
} | ||
addUsersCmd.Flags().StringP("extension", "e", "yaml", "Source file type - json or yaml") | ||
if err := addUsersCmd.MarkFlagRequired("extension"); err != nil { | ||
fmt.Print(aurora.Sprintf(aurora.Red("%s\n"), err.Error())) | ||
os.Exit(1) | ||
} | ||
} |