-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cli): migrate to Cobra and add token get command #21
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,18 +18,58 @@ import ( | |
|
|
||
| retry "github.com/appleboy/go-httpretry" | ||
| "github.com/go-authgate/cli/tui" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // exitCodeError carries a non-zero exit code through Cobra's error chain | ||
| // without printing a redundant message (the command already wrote to stderr). | ||
| type exitCodeError int | ||
|
|
||
| func (e exitCodeError) Error() string { return "" } | ||
|
|
||
| func main() { | ||
| initConfig() | ||
| if err := buildRootCmd().Execute(); err != nil { | ||
| var exitErr exitCodeError | ||
| if errors.As(err, &exitErr) { | ||
| os.Exit(int(exitErr)) | ||
| } | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| // Select UI manager based on environment detection | ||
| uiManager := tui.SelectManager() | ||
| func buildRootCmd() *cobra.Command { | ||
| rootCmd := &cobra.Command{ | ||
| Use: "authgate-cli", | ||
| Short: "OAuth 2.0 authentication CLI", | ||
| Version: getVersion(), | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| initConfig() | ||
| uiManager := tui.SelectManager() | ||
| ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
| defer stop() | ||
| if code := run(ctx, uiManager); code != 0 { | ||
| return exitCodeError(code) | ||
| } | ||
| return nil | ||
| }, | ||
|
Comment on lines
+48
to
+57
|
||
| } | ||
| registerFlags(rootCmd) | ||
| rootCmd.AddCommand(buildVersionCmd()) | ||
| rootCmd.AddCommand(buildTokenCmd()) | ||
| return rootCmd | ||
| } | ||
|
|
||
| ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
| exitCode := run(ctx, uiManager) | ||
| stop() | ||
| os.Exit(exitCode) | ||
| func buildVersionCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "version", | ||
| Short: "Print version", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| fmt.Println(getVersion()) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func run(ctx context.Context, ui tui.Manager) int { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SilenceErrors: truecombined withmain()exiting onExecute()error without printing it will cause user-facing failures (e.g., unknown flag/invalid args) to exit with code 1 and no message. Either print the returned error inmain()(to stderr) or stop silencing errors and let Cobra print them (keepingSilenceUsage: trueis fine).