Skip to content

Commit

Permalink
refactor: logs flags and examples (#172)
Browse files Browse the repository at this point in the history
* refactor: logs flags and examples

* Update internal/cli/logs.go

Co-authored-by: Rita Zerrizuela <[email protected]>

* Update internal/cli/logs.go

Co-authored-by: Rita Zerrizuela <[email protected]>

* apply feedback

Co-authored-by: Rita Zerrizuela <[email protected]>
  • Loading branch information
jfatta and Widcket authored Mar 20, 2021
1 parent 5edead8 commit 5cd6550
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 41 deletions.
32 changes: 32 additions & 0 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ func (f *Flag) RegisterStringSliceU(cmd *cobra.Command, value *[]string, default
registerStringSlice(cmd, f, value, defaultValue, true)
}

func (f *Flag) RegisterInt(cmd *cobra.Command, value *int, defaultValue int) {
registerInt(cmd, f, value, defaultValue, false)
}

func (f *Flag) RegisterBool(cmd *cobra.Command, value *bool, defaultValue bool) {
registerBool(cmd, f, value, defaultValue, false)
}

func (f *Flag) RegisterBoolU(cmd *cobra.Command, value *bool, defaultValue bool) {
registerBool(cmd, f, value, defaultValue, true)
}

func (f *Flag) RegisterIntU(cmd *cobra.Command, value *int, defaultValue int) {
registerInt(cmd, f, value, defaultValue, true)
}

func (f *Flag) label() string {
return fmt.Sprintf("%s:", f.Name)
}
Expand Down Expand Up @@ -91,6 +107,22 @@ func registerStringSlice(cmd *cobra.Command, f *Flag, value *[]string, defaultVa
}
}

func registerInt(cmd *cobra.Command, f *Flag, value *int, defaultValue int, isUpdate bool) {
cmd.Flags().IntVarP(value, f.LongForm, f.ShortForm, defaultValue, f.Help)

if err := markFlagRequired(cmd, f, isUpdate); err != nil {
panic(unexpectedError(err)) // TODO: Handle
}
}

func registerBool(cmd *cobra.Command, f *Flag, value *bool, defaultValue bool, isUpdate bool) {
cmd.Flags().BoolVarP(value, f.LongForm, f.ShortForm, defaultValue, f.Help)

if err := markFlagRequired(cmd, f, isUpdate); err != nil {
panic(unexpectedError(err)) // TODO: Handle
}
}

func shouldAsk(cmd *cobra.Command, f *Flag, isUpdate bool) bool {
if isUpdate {
return shouldPromptWhenFlagless(cmd, f.LongForm)
Expand Down
96 changes: 55 additions & 41 deletions internal/cli/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import (
"gopkg.in/auth0.v5/management"
)

var (
logsClientID = Flag{
Name: "Client ID",
LongForm: "client-id",
ShortForm: "c",
Help: "Client Id of an Auth0 application to filter the logs.",
}

logsNum = Flag{
Name: "Number of Entries",
LongForm: "number",
ShortForm: "n",
Help: "Number of log entries to show.",
}

logsNoColor = Flag{
Name: "Disable Color",
LongForm: "disable-color",
Help: "Disable colored log output.",
}
)

func logsCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Expand All @@ -24,30 +46,26 @@ func logsCmd(cli *cli) *cobra.Command {
}

func listLogsCmd(cli *cli) *cobra.Command {
var flags struct {
Num int
NoColor bool
}

var inputs struct {
ClientID string
Num int
NoColor bool
}

cmd := &cobra.Command{
Use: "list [client-id]",
Args: cobra.MaximumNArgs(1),
Short: "Show the tenant logs",
Long: `Show the tenant logs:
auth0 logs list [client-id]
`,
Use: "list",
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(1),
Short: "Show the application logs",
Long: "Show the tenant logs allowing to filter by Client Id.",
Example: `auth0 logs list
auth0 logs list --client-id <id>
auth0 logs ls -n 100`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
inputs.ClientID = ""
if len(args) == 1 {
inputs.ClientID = args[0]
}

list, err := getLatestLogs(cli, flags.Num, inputs.ClientID)
list, err := getLatestLogs(cli, inputs.Num, inputs.ClientID)
if err != nil {
return fmt.Errorf("An unexpected error occurred while getting logs: %v", err)
}
Expand All @@ -69,42 +87,38 @@ auth0 logs list [client-id]
cli.api.ActionExecution, time.Second,
)

cli.renderer.LogList(list, logsCh, actionExecutionAPI, flags.NoColor, !cli.debug)
cli.renderer.LogList(list, logsCh, actionExecutionAPI, inputs.NoColor, !cli.debug)
return nil
},
}

cmd.Flags().IntVarP(&flags.Num, "num-entries", "n", 100, "the number of log entries to print")
cmd.Flags().BoolVar(&flags.NoColor, "no-color", false, "turn off colored print")

logsClientID.RegisterString(cmd, &inputs.ClientID, "")
logsNum.RegisterInt(cmd, &inputs.Num, 100)
logsNoColor.RegisterBool(cmd, &inputs.NoColor, false)
return cmd
}

func tailLogsCmd(cli *cli) *cobra.Command {
var flags struct {
Num int
NoColor bool
}

var inputs struct {
ClientID string
Num int
NoColor bool
}

cmd := &cobra.Command{
Use: "tail [client-id]",
Use: "tail",
Args: cobra.MaximumNArgs(1),
Short: "Tail the tenant logs",
Long: `Tail the tenant logs:
auth0 logs tail [client-id]
`,
Long: "Tail the tenant logs allowing to filter by Client ID",
Example: `auth0 logs tail
auth0 logs tail --client-id <id>
auth0 logs tail -n 100`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
inputs.ClientID = ""
if len(args) == 1 {
inputs.ClientID = args[0]
}
lastLogID := ""
list, err := getLatestLogs(cli, flags.Num, inputs.ClientID)
list, err := getLatestLogs(cli, inputs.Num, inputs.ClientID)
if err != nil {
return fmt.Errorf("An unexpected error occurred while getting logs: %v", err)
}
Expand Down Expand Up @@ -166,14 +180,14 @@ auth0 logs tail [client-id]
cli.api.ActionExecution, time.Second,
)

cli.renderer.LogList(list, logsCh, actionExecutionAPI, flags.NoColor, !cli.debug)
cli.renderer.LogList(list, logsCh, actionExecutionAPI, inputs.NoColor, !cli.debug)
return nil
},
}

cmd.Flags().IntVarP(&flags.Num, "num-entries", "n", 100, "the number of log entries to print")
cmd.Flags().BoolVar(&flags.NoColor, "no-color", false, "turn off colored print")

logsClientID.RegisterString(cmd, &inputs.ClientID, "")
logsNum.RegisterInt(cmd, &inputs.Num, 100)
logsNoColor.RegisterBool(cmd, &inputs.NoColor, false)
return cmd
}

Expand Down

0 comments on commit 5cd6550

Please sign in to comment.