taskcafe/internal/command/user_list.go
Jordan Knott 64093e19f6 redesign
2022-05-06 16:44:31 -05:00

42 lines
1017 B
Go

package command
import (
"context"
"database/sql"
"os"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jordanknott/taskcafe/internal/config"
"github.com/jordanknott/taskcafe/internal/db"
"github.com/spf13/cobra"
)
func newUserListCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
RunE: func(cmd *cobra.Command, args []string) error {
appConfig, err := config.GetAppConfig()
if err != nil {
return err
}
dbConn, err := sql.Open("postgres", appConfig.Database.GetDatabaseConnectionUri())
if err != nil {
return err
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Full Name", "Username", "Email"})
repo := db.NewRepository(dbConn)
accounts, err := repo.GetUserAccounts(context.Background())
if err != nil {
return nil
}
for _, account := range accounts {
t.AppendRow(table.Row{account.UserID.String(), account.Fullname, account.Username, account.Email})
}
t.Render()
return nil
},
}
}