Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from mainawycliffe/find-user-output
Browse files Browse the repository at this point in the history
Add Output Formats Support For Find Firebase Auth User Commands
  • Loading branch information
mainawycliffe authored Apr 17, 2020
2 parents 71fb352 + 1b47f83 commit 986646e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
29 changes: 27 additions & 2 deletions cmd/byEmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cmd

import (
"context"
"fmt"
"os"

fAuth "firebase.google.com/go/auth"
"github.com/mainawycliffe/kamanda/firebase"
"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/mainawycliffe/kamanda/views"
"github.com/spf13/cobra"
)

Expand All @@ -17,12 +20,22 @@ var byEmailCmd = &cobra.Command{
Short: "Find a Firebase Auth user by email address",
Example: `kamanda auth find by-email [email protected]`,
Run: func(cmd *cobra.Command, args []string) {
output, err := cmd.Flags().GetString("output")
if err != nil {
utils.StdOutError(os.Stderr, "Error reading output: %s", err.Error())
os.Exit(1)
}
if output != "json" && output != "yaml" && output != "" {
utils.StdOutError(os.Stderr, "Unsupported output!")
os.Exit(1)
}
// args = list of UIDs
if len(args) == 0 {
utils.StdOutError(os.Stderr, "at least one email is required!")
os.Exit(1)
}
criteria := auth.ByUserEmailCriteria
users := make([]*fAuth.ExportedUserRecord, 0)
for _, email := range args {
user, err := auth.GetUser(context.Background(), email, criteria)
if err != nil {
Expand All @@ -33,9 +46,21 @@ var byEmailCmd = &cobra.Command{
utils.StdOutError(os.Stderr, "Error\t %s\t %s", email, err.Error())
continue
}
//@todo something with the output
utils.StdOutSuccess(os.Stdout, "Success\t%s\t%s", email, user.UID)
users = append(users, &fAuth.ExportedUserRecord{
UserRecord: user,
})
}
formatedUsers, err := utils.FormatResults(users, output)
if err != nil && err.Error() != "Unknown Format" {
utils.StdOutError(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
if formatedUsers != nil {
fmt.Printf("%s\n", formatedUsers)
os.Exit(0)
}
// draw table
views.ViewUsersTable(users, "")
os.Exit(0)
},
}
Expand Down
34 changes: 32 additions & 2 deletions cmd/byPhone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cmd

import (
"context"
"fmt"
"os"

fAuth "firebase.google.com/go/auth"
"github.com/mainawycliffe/kamanda/firebase"
"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/mainawycliffe/kamanda/views"
"github.com/spf13/cobra"
)

Expand All @@ -17,12 +20,27 @@ var byPhoneCmd = &cobra.Command{
Short: "find a Firebase Auth User by their phone number",
Example: `kamanda auth find by-phone +254712345678`,
Run: func(cmd *cobra.Command, args []string) {
output, err := cmd.Flags().GetString("output")
if err != nil {
utils.StdOutError(os.Stderr, "Error reading output: %s", err.Error())
os.Exit(1)
}
if output != "json" && output != "yaml" && output != "" {
utils.StdOutError(os.Stderr, "Unsupported output!")
os.Exit(1)
}
// args = list of uids
if len(args) == 0 {
utils.StdOutError(os.Stderr, "at least one Firebase user UID is required!")
os.Exit(1)
}
// args = list of phone numbers
if len(args) == 0 {
utils.StdOutError(os.Stderr, "at least one Firebase user UID is required!")
os.Exit(1)
}
criteria := auth.ByUserUIDCriteria
users := make([]*fAuth.ExportedUserRecord, 0)
for _, phone := range args {
user, err := auth.GetUser(context.Background(), phone, criteria)
if err != nil {
Expand All @@ -33,9 +51,21 @@ var byPhoneCmd = &cobra.Command{
utils.StdOutError(os.Stderr, "Error\t%s\t%s", phone, err.Error())
continue
}
// @todo expand this list of users
utils.StdOutSuccess(os.Stdout, "%s\tWas successfully Retrieved\n", user.UID)
users = append(users, &fAuth.ExportedUserRecord{
UserRecord: user,
})
}
formatedUsers, err := utils.FormatResults(users, output)
if err != nil && err.Error() != "Unknown Format" {
utils.StdOutError(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
if formatedUsers != nil {
fmt.Printf("%s\n", formatedUsers)
os.Exit(0)
}
// draw table
views.ViewUsersTable(users, "")
os.Exit(0)
},
}
Expand Down
29 changes: 27 additions & 2 deletions cmd/findUser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cmd

import (
"context"
"fmt"
"os"

fAuth "firebase.google.com/go/auth"
"github.com/mainawycliffe/kamanda/firebase"
"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/mainawycliffe/kamanda/views"
"github.com/spf13/cobra"
)

Expand All @@ -19,12 +22,22 @@ var findUserCmd = &cobra.Command{
To find user by email or by phone use "find by-email" or "find by-phone"`,
Example: `kamanda auth find [UID1] [UID2]`,
Run: func(cmd *cobra.Command, args []string) {
output, err := cmd.Flags().GetString("output")
if err != nil {
utils.StdOutError(os.Stderr, "Error reading output: %s", err.Error())
os.Exit(1)
}
if output != "json" && output != "yaml" && output != "" {
utils.StdOutError(os.Stderr, "Unsupported output!")
os.Exit(1)
}
// args = list of uids
if len(args) == 0 {
utils.StdOutError(os.Stderr, "at least one Firebase user UID is required!")
os.Exit(1)
}
criteria := auth.ByUserUIDCriteria
users := make([]*fAuth.ExportedUserRecord, 0)
for _, uid := range args {
user, err := auth.GetUser(context.Background(), uid, criteria)
if err != nil {
Expand All @@ -35,9 +48,21 @@ To find user by email or by phone use "find by-email" or "find by-phone"`,
utils.StdOutError(os.Stderr, "Error \t %s \t %s", uid, err.Error())
continue
}
//@todo something with the output
utils.StdOutSuccess(os.Stdout, "Success \t %s \t Was successfully Retrieved", user.UID)
users = append(users, &fAuth.ExportedUserRecord{
UserRecord: user,
})
}
formatedUsers, err := utils.FormatResults(users, output)
if err != nil && err.Error() != "Unknown Format" {
utils.StdOutError(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
if formatedUsers != nil {
fmt.Printf("%s\n", formatedUsers)
os.Exit(0)
}
// draw table
views.ViewUsersTable(users, "")
os.Exit(0)
},
}
Expand Down

0 comments on commit 986646e

Please sign in to comment.