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

Commit

Permalink
refactor: using internal stdout util
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Jan 29, 2020
1 parent 9de0aa2 commit ea465d3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 41 deletions.
14 changes: 6 additions & 8 deletions cmd/addUser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ 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"
Expand Down Expand Up @@ -38,20 +36,20 @@ var addUserCmd = &cobra.Command{
}
userRecord, err := auth.NewFirebaseUser(context.Background(), user)
if err != nil {
fmt.Print(aurora.Sprintf(aurora.Red("%s\n"), err.Error()))
utils.StdOutError("%s\n", err.Error())
os.Exit(1)
}
fmt.Print(aurora.Sprintf(aurora.Green("✔✔ user added - uid: %s email: %s\n"), userRecord.UID, userRecord.Email))
utils.StdOutSuccess("✔✔ user added - uid: %s email: %s\n", userRecord.UID, userRecord.Email)
if len(customClaimsInput) == 0 {
os.Exit(0)
}
customClaims := utils.ProcessCustomClaimInput(customClaimsInput)
err = auth.AddCustomClaimToFirebaseUser(context.Background(), userRecord.UID, customClaims)
if err != nil {
fmt.Print(aurora.Sprintf(aurora.Red("%s\n"), err.Error()))
utils.StdOutError("%s\n", err.Error())
os.Exit(1)
}
fmt.Print(aurora.Sprintf(aurora.Green("✔✔ custom claims added\n")))
utils.StdOutSuccess("✔✔ custom claims added\n")
os.Exit(0)
},
}
Expand All @@ -68,11 +66,11 @@ func init() {
addUserCmd.Flags().Bool("isDisabled", false, "is the user account disabled")
addUserCmd.Flags().StringToStringP("customClaims", "c", nil, "user custom claims i.e. --customClaims \"admin=true\"")
if err := addUserCmd.MarkFlagRequired("email"); err != nil {
fmt.Print(aurora.Sprintf(aurora.Red("%s\n"), err.Error()))
utils.StdOutError("%s\n", err.Error())
os.Exit(1)
}
if err := addUserCmd.MarkFlagRequired("password"); err != nil {
fmt.Print(aurora.Sprintf(aurora.Red("%s\n"), err.Error()))
utils.StdOutError("%s\n", err.Error())
os.Exit(1)
}
}
6 changes: 2 additions & 4 deletions cmd/addUsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ 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"
Expand Down Expand Up @@ -66,12 +64,12 @@ func init() {
authCmd.AddCommand(addUsersCmd)
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()))
utils.StdOutError("%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()))
utils.StdOutError("%s\n", err.Error())
os.Exit(1)
}
}
10 changes: 4 additions & 6 deletions cmd/customclaims.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ 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"
Expand All @@ -19,18 +17,18 @@ var customclaimsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// args = list of uids
if len(args) == 0 {
fmt.Print(aurora.Sprintf(aurora.Red("atleast one Firebase user uid is required!")))
utils.StdOutError("atleast one Firebase user uid is required!")
os.Exit(1)
}
customClaimsInput, _ := cmd.Flags().GetStringToString("customClaims")
customClaims := utils.ProcessCustomClaimInput(customClaimsInput)
for _, uid := range args {
err := auth.AddCustomClaimToFirebaseUser(context.Background(), uid, customClaims)
if err != nil {
fmt.Print(aurora.Sprintf(aurora.Red("%s - Failed: %s\n"), uid, err.Error()))
utils.StdOutError("%s - Failed: %s\n", uid, err.Error())
continue
}
fmt.Print(aurora.Sprintf(aurora.Green("%s - Added Successfully\n"), uid))
utils.StdOutSuccess("%s - Added Successfully\n", uid)
}
os.Exit(0)
},
Expand All @@ -40,7 +38,7 @@ func init() {
authCmd.AddCommand(customclaimsCmd)
customclaimsCmd.Flags().StringToStringP("customClaims", "c", nil, "user custom claims i.e. --customClaims \"admin=true\"")
if err := customclaimsCmd.MarkFlagRequired("customClaims"); err != nil {
fmt.Print(aurora.Sprintf(aurora.Red("%s\n"), err.Error()))
utils.StdOutError("%s\n", err.Error())
os.Exit(1)
}
}
9 changes: 4 additions & 5 deletions cmd/deleteUser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ 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"
)

Expand All @@ -18,17 +17,17 @@ var deleteUserCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// args = list of uids
if len(args) == 0 {
fmt.Printf("atleast one Firebase user uid is required!")
utils.StdOutError("atleast one Firebase user uid is required!")
os.Exit(1)
}
// delete all listed user accounts
for _, uid := range args {
err := auth.DeleteFirebaseUser(context.Background(), uid)
if err != nil {
fmt.Print(aurora.Sprintf(aurora.Red("%s - Not Deleted: %s\n"), uid, err.Error()))
utils.StdOutError("%s - Not Deleted: %s\n", uid, err.Error())
continue
}
fmt.Print(aurora.Sprintf(aurora.Green("%s - Deleted\n"), uid))
utils.StdOutSuccess("%s - Deleted\n", uid)
}
os.Exit(0)
},
Expand Down
7 changes: 3 additions & 4 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cmd

import (
"fmt"
"os"

"github.com/logrusorgru/aurora"
"github.com/mainawycliffe/kamanda/configs"
"github.com/mainawycliffe/kamanda/oauth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -18,7 +17,7 @@ var loginCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
if viper.IsSet(configs.FirebaseRefreshTokenViperConfigKey) {
email := viper.GetString(configs.FirebaseLoggedInUserEmailViperConfigKey)
fmt.Fprint(os.Stdout, aurora.Sprintf("Already logged in as %s\n", aurora.Green(email)))
utils.StdOutSuccess("Already logged in as %s\n", email)
os.Exit(1)
}
noLocalhostFlag, _ := cmd.Flags().GetBool("no-localhost")
Expand All @@ -27,7 +26,7 @@ var loginCmd = &cobra.Command{
os.Exit(0)
}
if err := oauth.LoginWithoutLocalhost(false); err != nil {
fmt.Fprint(os.Stdout, aurora.Sprintf(aurora.Red("\n\n%s\n\n"), err.Error()))
utils.StdOutError("\n\n%s\n\n", err.Error())
os.Exit(1)
}
os.Exit(0)
Expand Down
5 changes: 2 additions & 3 deletions cmd/login:ci.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cmd

import (
"fmt"
"os"

"github.com/logrusorgru/aurora"
"github.com/mainawycliffe/kamanda/oauth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +19,7 @@ var loginCICmd = &cobra.Command{
os.Exit(0)
}
if err := oauth.LoginWithoutLocalhost(true); err != nil {
fmt.Fprint(os.Stdout, aurora.Sprintf(aurora.Red("\n\n%s\n\n"), err.Error()))
utils.StdOutError("\n\n%s\n\n", err.Error())
os.Exit(1)
}
os.Exit(0)
Expand Down
8 changes: 4 additions & 4 deletions cmd/logout.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cmd

import (
"fmt"
"os"

"github.com/logrusorgru/aurora"
"github.com/mainawycliffe/kamanda/configs"
"github.com/mainawycliffe/kamanda/oauth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -18,14 +18,14 @@ var logoutCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
email := viper.GetString(configs.FirebaseLoggedInUserEmailViperConfigKey)
if !viper.IsSet(configs.FirebaseRefreshTokenViperConfigKey) {
fmt.Fprint(os.Stdout, aurora.Sprintf("%s\n", aurora.Red("You are not logged in!")))
utils.StdOutError("%s\n", aurora.Red("You are not logged in!"))
os.Exit(1)
}
if err := oauth.RevokeRefreshToken(); err != nil {
fmt.Fprint(os.Stdout, aurora.Sprintf(aurora.Red("%s\n"), err.Error()))
utils.StdOutError("%s\n", err.Error())
os.Exit(1)
}
fmt.Fprint(os.Stdout, aurora.Sprintf(aurora.Green("Logged out from %s\n\n"), email))
utils.StdOutError("Logged out from %s\n\n", email)
os.Exit(0)
},
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package cmd

import (
"fmt"
"os"

"github.com/mainawycliffe/kamanda/configs"
"github.com/mainawycliffe/kamanda/utils"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

// "In this context, the client secret is obviously not treated as a secret"
Expand Down Expand Up @@ -34,7 +36,7 @@ var rootCmd = &cobra.Command{

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
utils.StdOutError("%s", err.Error())
os.Exit(1)
}
}
Expand All @@ -56,13 +58,13 @@ func initConfig() {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
utils.StdOutError("%s", err.Error())
os.Exit(1)
}
configPath := fmt.Sprintf("%s/.kamanda.yaml", home)
_, err = os.Stat(configPath)
if err != nil && !os.IsNotExist(err) {
fmt.Printf("Error checking if config file exists: %s\n", err.Error())
utils.StdOutError("Error checking if config file exists: %s\n", err.Error())
os.Exit(1)
}
viper.SetConfigFile(configPath)
Expand All @@ -75,15 +77,15 @@ func initConfig() {
_ = viper.SafeWriteConfig()
// bind token flag to refresh token config, overriding incase token is supplied
if err := viper.BindPFlag(configs.FirebaseRefreshTokenViperConfigKey, rootCmd.Flags().Lookup("token")); err != nil {
fmt.Printf("Error bind token flag: %s\n", err.Error())
utils.StdOutError("Error bind token flag: %s\n", err.Error())
os.Exit(1)
}
if err := viper.BindPFlag("project", rootCmd.Flags().Lookup("project")); err != nil {
fmt.Printf("Error bind project flag: %s\n", err.Error())
utils.StdOutError("Error bind project flag: %s\n", err.Error())
os.Exit(1)
}
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading configs: %s\n", err.Error())
utils.StdOutError("Error reading configs: %s\n", err.Error())
os.Exit(1)
}
}

0 comments on commit ea465d3

Please sign in to comment.