-
Notifications
You must be signed in to change notification settings - Fork 8
Add accounts command and improve account selection UX #261
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/basecamp/basecamp-cli/internal/appctx" | ||
| "github.com/basecamp/basecamp-cli/internal/output" | ||
| "github.com/basecamp/basecamp-cli/internal/tui/resolve" | ||
| ) | ||
|
|
||
| // NewAccountsCmd creates the accounts command group. | ||
| func NewAccountsCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "accounts", | ||
| Aliases: []string{"account"}, | ||
| Short: "Manage accounts", | ||
| Long: "List authorized Basecamp accounts and set the default.", | ||
| } | ||
|
|
||
| cmd.AddCommand( | ||
| newAccountsListCmd(), | ||
| newAccountsUseCmd(), | ||
| ) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func newAccountsListCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List authorized accounts", | ||
| Long: "List all Basecamp accounts you have access to.", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| app := appctx.FromContext(cmd.Context()) | ||
| if app == nil { | ||
| return fmt.Errorf("app not initialized") | ||
| } | ||
|
|
||
| accounts, err := app.Resolve().ListAccounts(cmd.Context()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Convert to a serializable format | ||
| type accountRow struct { | ||
| ID int64 `json:"id"` | ||
| Name string `json:"name"` | ||
| Href string `json:"href"` | ||
| } | ||
| rows := make([]accountRow, len(accounts)) | ||
| for i, acct := range accounts { | ||
| rows[i] = accountRow{ | ||
| ID: acct.ID, | ||
| Name: acct.Name, | ||
| Href: acct.HREF, | ||
| } | ||
| } | ||
|
|
||
|
jeremy marked this conversation as resolved.
|
||
| count := len(rows) | ||
| label := "accounts" | ||
| if count == 1 { | ||
| label = "account" | ||
| } | ||
|
|
||
| return app.OK(rows, | ||
| output.WithSummary(fmt.Sprintf("%d %s", count, label)), | ||
| output.WithBreadcrumbs( | ||
| output.Breadcrumb{ | ||
| Action: "use", | ||
| Cmd: "basecamp accounts use <id>", | ||
| Description: "Set default account", | ||
| }, | ||
| ), | ||
| ) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func newAccountsUseCmd() *cobra.Command { | ||
| var scope string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "use <id>", | ||
| Short: "Set default account", | ||
| Long: "Set the default Basecamp account for CLI commands.", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| app := appctx.FromContext(cmd.Context()) | ||
| if app == nil { | ||
| return fmt.Errorf("app not initialized") | ||
| } | ||
|
|
||
| // Validate scope | ||
| if scope != "global" && scope != "local" { | ||
| return output.ErrUsage("--scope must be \"global\" or \"local\"") | ||
| } | ||
|
|
||
| accountIDStr := args[0] | ||
|
|
||
| // Validate it's a number | ||
| accountID, err := strconv.ParseInt(accountIDStr, 10, 64) | ||
| if err != nil { | ||
| return output.ErrUsage("Invalid account ID") | ||
| } | ||
|
|
||
| // Validate account exists | ||
| accounts, err := app.Resolve().ListAccounts(cmd.Context()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var found bool | ||
| var accountName string | ||
| for _, acct := range accounts { | ||
| if acct.ID == accountID { | ||
| found = true | ||
| accountName = acct.Name | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| return output.ErrNotFound("account", accountIDStr) | ||
| } | ||
|
|
||
| // Persist the canonical account ID (e.g. "007" → "7") | ||
| canonicalID := strconv.FormatInt(accountID, 10) | ||
| if err := resolve.PersistValue("account_id", canonicalID, scope); err != nil { | ||
| return fmt.Errorf("failed to save account: %w", err) | ||
| } | ||
|
|
||
| summary := fmt.Sprintf("Default account set to %s (#%s, %s)", accountName, canonicalID, scope) | ||
|
|
||
| return app.OK(map[string]any{ | ||
| "id": accountID, | ||
| "name": accountName, | ||
| "scope": scope, | ||
| }, | ||
| output.WithSummary(summary), | ||
| output.WithBreadcrumbs( | ||
| output.Breadcrumb{ | ||
| Action: "list", | ||
| Cmd: "basecamp accounts list", | ||
| Description: "List accounts", | ||
| }, | ||
| output.Breadcrumb{ | ||
| Action: "projects", | ||
| Cmd: "basecamp projects list", | ||
| Description: "List projects", | ||
| }, | ||
| ), | ||
| ) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&scope, "scope", "global", "Config scope (global or local)") | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.