Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: logs flags and examples #172

Merged
merged 6 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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