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

Commit

Permalink
Output the List of Users in JSON and YAML
Browse files Browse the repository at this point in the history
This makes it easier for other programs to read the output from the
cli.
  • Loading branch information
mainawycliffe committed Apr 12, 2020
1 parent cad619e commit 23409d9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 49 deletions.
78 changes: 29 additions & 49 deletions cmd/listUsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/gdamore/tcell"
"github.com/mainawycliffe/kamanda/firebase/auth"
"github.com/mainawycliffe/kamanda/utils"
"github.com/rivo/tview"
"github.com/mainawycliffe/kamanda/views"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

// listUsersCmd represents the listUsers command
Expand All @@ -19,65 +19,45 @@ var listUsersCmd = &cobra.Command{
Aliases: []string{"list", "listUsers"},
Short: "Get a list of users in firebase auth.",
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)
}
token, err := cmd.Flags().GetString("nextPageToken")
if err != nil {
utils.StdOutError(os.Stderr, "An error occurred while parsing next page token")
utils.StdOutError(os.Stderr, "Error reading nextPageToken: %s", err.Error())
os.Exit(1)
}
getUsers, err := auth.ListUsers(context.Background(), 0, token)
if err != nil {
utils.StdOutError(os.Stderr, "Error! %s", err.Error())
os.Exit(1)
}
if getUsers.NextPageToken != "" {
utils.StdOutSuccess(os.Stdout, "Next Page Token %s", getUsers.NextPageToken)
}
// @todo: set the width of the table to 100%
app := tview.NewApplication()
table := tview.NewTable()
tableHeaderColumnNames := []string{"#", "UID", "Email", "Email Verified", "Name", "Phone Number", "Provider"}
for index, value := range tableHeaderColumnNames {
tableCell := tview.NewTableCell(value).
SetTextColor(tcell.ColorBlue).
SetSelectable(false).
SetStyle(tcell.StyleDefault).
SetAlign(tview.AlignLeft)
table.SetCell(0, index, tableCell)
}
for index, user := range getUsers.Users {
isEmailVerified := "Yes"
if !user.EmailVerified {
isEmailVerified = "No"
if output == "json" {
json, err := json.Marshal(getUsers)
if err != nil {
utils.StdOutError(os.Stderr, "Error marsalling json: %s", err.Error())
os.Exit(1)
}
table.SetCell(index+1, 0, tview.NewTableCell(fmt.Sprintf("%d", index+1))).SetSelectable(false, false)
table.SetCell(index+1, 1, tview.NewTableCell(user.UID).SetExpansion(3)).SetSelectable(true, false)
table.SetCell(index+1, 2, tview.NewTableCell(user.Email).SetExpansion(2)).SetSelectable(true, false)
table.SetCell(index+1, 3, tview.NewTableCell(isEmailVerified).SetExpansion(1)).SetSelectable(true, false)
table.SetCell(index+1, 4, tview.NewTableCell(user.DisplayName).SetExpansion(3)).SetSelectable(true, false)
table.SetCell(index+1, 5, tview.NewTableCell(user.PhoneNumber).SetExpansion(1)).SetSelectable(true, false)
table.SetCell(index+1, 6, tview.NewTableCell(strings.ToUpper(user.ProviderID)).SetExpansion(1)).SetSelectable(true, false)
fmt.Printf("%s\n", json)
os.Exit(0)
}
table.Select(1, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) {
// exit when the following keys ae pressed
if key == tcell.KeyEscape || key == tcell.KeyCtrlW {
app.Stop()
}
if key == tcell.KeyEnter {
table.SetSelectable(true, true)
if output == "yaml" {
yaml, err := yaml.Marshal(getUsers)
if err != nil {
utils.StdOutError(os.Stderr, "Error marsalling yaml: %s", err.Error())
os.Exit(1)
}
}).
// @todo: show more info about the user when a row is clicked
SetSelectedFunc(func(row int, column int) {
// @todo: fetch from firebase the user details
// uid := table.GetCell(row, 1).Text
})
tableContainer := tview.NewFlex().SetDirection(tview.FlexRow).AddItem(table, 0, 1, true)
appContainer := tview.NewFlex().AddItem(tableContainer, 0, 1, true)
// @todo add pagination to get more request
if err := app.SetRoot(appContainer, true).Run(); err != nil {
utils.StdOutError(os.Stderr, "Error! %s", err.Error())
os.Exit(1)
fmt.Printf("%s\n", yaml)
os.Exit(0)
}
// draw table
views.ViewUsersTable(getUsers.Users, getUsers.NextPageToken)
},
}

Expand Down
61 changes: 61 additions & 0 deletions views/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package views

import (
"fmt"
"os"
"strings"

"firebase.google.com/go/auth"
"github.com/gdamore/tcell"
"github.com/mainawycliffe/kamanda/utils"
"github.com/rivo/tview"
)

// Draw a Table View For List of Users
func ViewUsersTable(users []*auth.ExportedUserRecord, nextPageToken string) {
app := tview.NewApplication()
table := tview.NewTable()
tableHeaderColumnNames := []string{"#", "UID", "Email", "Email Verified", "Name", "Phone Number", "Provider"}
for index, value := range tableHeaderColumnNames {
tableCell := tview.NewTableCell(value).
SetTextColor(tcell.ColorBlue).
SetSelectable(false).
SetStyle(tcell.StyleDefault).
SetAlign(tview.AlignLeft)
table.SetCell(0, index, tableCell)
}
for index, user := range users {
isEmailVerified := "Yes"
if !user.EmailVerified {
isEmailVerified = "No"
}
table.SetCell(index+1, 0, tview.NewTableCell(fmt.Sprintf("%d", index+1))).SetSelectable(false, false)
table.SetCell(index+1, 1, tview.NewTableCell(user.UID).SetExpansion(3)).SetSelectable(true, false)
table.SetCell(index+1, 2, tview.NewTableCell(user.Email).SetExpansion(2)).SetSelectable(true, false)
table.SetCell(index+1, 3, tview.NewTableCell(isEmailVerified).SetExpansion(1)).SetSelectable(true, false)
table.SetCell(index+1, 4, tview.NewTableCell(user.DisplayName).SetExpansion(3)).SetSelectable(true, false)
table.SetCell(index+1, 5, tview.NewTableCell(user.PhoneNumber).SetExpansion(1)).SetSelectable(true, false)
table.SetCell(index+1, 6, tview.NewTableCell(strings.ToUpper(user.ProviderID)).SetExpansion(1)).SetSelectable(true, false)
}
table.Select(1, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) {
// exit when the following keys ae pressed
if key == tcell.KeyEscape || key == tcell.KeyCtrlW {
app.Stop()
}
if key == tcell.KeyEnter {
table.SetSelectable(true, true)
}
}).
// @todo: show more info about the user when a row is clicked
SetSelectedFunc(func(row int, column int) {
// @todo: fetch from firebase the user details
// uid := table.GetCell(row, 1).Text
})
tableContainer := tview.NewFlex().SetDirection(tview.FlexRow).AddItem(table, 0, 1, true)
appContainer := tview.NewFlex().AddItem(tableContainer, 0, 1, true)
// @todo add pagination to get more request
if err := app.SetRoot(appContainer, true).Run(); err != nil {
utils.StdOutError(os.Stderr, "Error! %s", err.Error())
os.Exit(1)
}
}

0 comments on commit 23409d9

Please sign in to comment.