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

Commit

Permalink
feat: add 3 commands for finding users
Browse files Browse the repository at this point in the history
find user by uid, email or phone
  • Loading branch information
mainawycliffe committed Feb 9, 2020
1 parent b9fdf25 commit 7410f3e
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/byEmail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"context"
"os"

"github.com/mainawycliffe/kamanda/firebase"
"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/spf13/cobra"
)

// byEmailCmd represents the byEmail command
var byEmailCmd = &cobra.Command{
Use: "byEmail",
Aliases: []string{"email"},
Short: "find a user by email address",
Run: func(cmd *cobra.Command, args []string) {
// args = list of uids
if len(args) == 0 {
utils.StdOutError("atleast one email is required!")
os.Exit(1)
}
criteria := auth.ByUserEmailCriteria
for _, email := range args {
user, err := auth.GetUser(context.Background(), email, criteria)
if err != nil {
if firebase.IsUserNotFound(err) {
utils.StdOutError("Not Found\t %s \t User was not found", email, err.Error())
continue
}
utils.StdOutError("Error\t %s\t %s", email, err.Error())
continue
}
//@todo something with the output
utils.StdOutSuccess("Success\t%s\t%s", email, user.UID)
}
os.Exit(0)
},
}

func init() {
findUserCmd.AddCommand(byEmailCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// byEmailCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// byEmailCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
54 changes: 54 additions & 0 deletions cmd/byPhone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"context"
"os"

"github.com/mainawycliffe/kamanda/firebase"
"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/spf13/cobra"
)

// byPhoneCmd represents the byPhone command
var byPhoneCmd = &cobra.Command{
Use: "byPhone",
Aliases: []string{"phone"},
Short: "find a user by phone number",
Run: func(cmd *cobra.Command, args []string) {
// args = list of phone numbers
if len(args) == 0 {
utils.StdOutError("atleast one Firebase user UID is required!")
os.Exit(1)
}
criteria := auth.ByUserUIDCriteria
for _, phone := range args {
user, err := auth.GetUser(context.Background(), phone, criteria)
if err != nil {
if firebase.IsUserNotFound(err) {
utils.StdOutError("Not Found\t %s \t %s", phone, err.Error())
continue
}
utils.StdOutError("Error\t%s\t%s", phone, err.Error())
continue
}
// @todo expand this list of users
utils.StdOutSuccess("%s\tWas successfully Retrieved\n", user.UID)
}
os.Exit(0)
},
}

func init() {
findUserCmd.AddCommand(byPhoneCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// byPhoneCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// byPhoneCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
54 changes: 54 additions & 0 deletions cmd/findUser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"context"
"os"

"github.com/mainawycliffe/kamanda/firebase"
"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/spf13/cobra"
)

// findUserCmd represents the findUser command
var findUserCmd = &cobra.Command{
Use: "find",
Aliases: []string{"findUser"},
Short: "Find a user by uid. To find user by email or by phone use `find byEmail` or `find byPhone`",
Run: func(cmd *cobra.Command, args []string) {
// args = list of uids
if len(args) == 0 {
utils.StdOutError("atleast one Firebase user UID is required!")
os.Exit(1)
}
criteria := auth.ByUserUIDCriteria
for _, uid := range args {
user, err := auth.GetUser(context.Background(), uid, criteria)
if err != nil {
if firebase.IsUserNotFound(err) {
utils.StdOutError("Not Found\t %s \t %s", uid, err.Error())
continue
}
utils.StdOutError("Error \t %s \t %s", uid, err.Error())
continue
}
//@todo something with the output
utils.StdOutSuccess("Success \t %s \t Was successfully Retrieved", user.UID)
}
os.Exit(0)
},
}

func init() {
authCmd.AddCommand(findUserCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// findUserCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
findUserCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

0 comments on commit 7410f3e

Please sign in to comment.