feat(cli): add remotes set-token command
- commit
- 3819799dfbb321ada1dd17fcf9a00bb721c50f39
- author
- bnema · · 2026-01-18 03:17 UTC
- parent
- db5f449
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
1 file · +44 -0
M internal/adapters/in/cli/remotes.go +44 -0
| @@ -28,6 +28,7 @@ Configuration is stored in ~/.config/gordon/remotes.toml`, | |||
| 28 | 28 | cmd.AddCommand(newRemotesAddCmd()) | |
| 29 | 29 | cmd.AddCommand(newRemotesRemoveCmd()) | |
| 30 | 30 | cmd.AddCommand(newRemotesUseCmd()) | |
| 31 | + | cmd.AddCommand(newRemotesSetTokenCmd()) | |
| 31 | 32 | ||
| 32 | 33 | return cmd | |
| 33 | 34 | } | |
| @@ -246,6 +247,49 @@ Examples: | |||
| 246 | 247 | } | |
| 247 | 248 | } | |
| 248 | 249 | ||
| 250 | + | // newRemotesSetTokenCmd creates the remotes set-token command. | |
| 251 | + | func newRemotesSetTokenCmd() *cobra.Command { | |
| 252 | + | return &cobra.Command{ | |
| 253 | + | Use: "set-token <name> <token>", | |
| 254 | + | Short: "Set the token for a remote", | |
| 255 | + | Long: `Set or update the authentication token for a saved remote. | |
| 256 | + | ||
| 257 | + | This is useful when: | |
| 258 | + | - The server uses token-based authentication (not password auth) | |
| 259 | + | - You have a pre-generated token from 'gordon auth token generate' | |
| 260 | + | - You want to update an expired token | |
| 261 | + | ||
| 262 | + | For servers with password authentication, use 'gordon auth login' instead. | |
| 263 | + | ||
| 264 | + | Examples: | |
| 265 | + | gordon remotes set-token prod eyJhbGciOiJIUzI1NiIs... | |
| 266 | + | gordon remotes set-token staging $(cat token.txt)`, | |
| 267 | + | Args: cobra.ExactArgs(2), | |
| 268 | + | RunE: func(cmd *cobra.Command, args []string) error { | |
| 269 | + | name := args[0] | |
| 270 | + | token := args[1] | |
| 271 | + | ||
| 272 | + | // Verify remote exists | |
| 273 | + | remotes, _, err := remote.ListRemotes() | |
| 274 | + | if err != nil { | |
| 275 | + | return fmt.Errorf("failed to list remotes: %w", err) | |
| 276 | + | } | |
| 277 | + | ||
| 278 | + | if _, exists := remotes[name]; !exists { | |
| 279 | + | fmt.Println(styles.RenderError(fmt.Sprintf("Remote '%s' not found", name))) | |
| 280 | + | return nil | |
| 281 | + | } | |
| 282 | + | ||
| 283 | + | if err := remote.UpdateRemoteToken(name, token); err != nil { | |
| 284 | + | return fmt.Errorf("failed to update token: %w", err) | |
| 285 | + | } | |
| 286 | + | ||
| 287 | + | fmt.Println(styles.RenderSuccess(fmt.Sprintf("Token updated for remote '%s'", name))) | |
| 288 | + | return nil | |
| 289 | + | }, | |
| 290 | + | } | |
| 291 | + | } | |
| 292 | + | ||
| 249 | 293 | // normalizeURL ensures the URL has a protocol scheme, defaulting to https. | |
| 250 | 294 | func normalizeURL(url string) string { | |
| 251 | 295 | if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { | |
| @@ -28,6 +28,7 @@ Configuration is stored in ~/.config/gordon/remotes.toml`, | |||||
| 28 | cmd.AddCommand(newRemotesAddCmd()) | 28 | cmd.AddCommand(newRemotesAddCmd()) | ||
| 29 | cmd.AddCommand(newRemotesRemoveCmd()) | 29 | cmd.AddCommand(newRemotesRemoveCmd()) | ||
| 30 | cmd.AddCommand(newRemotesUseCmd()) | 30 | cmd.AddCommand(newRemotesUseCmd()) | ||
| 31 | + | cmd.AddCommand(newRemotesSetTokenCmd()) | |||
| 31 | 32 | ||||
| 32 | return cmd | 33 | return cmd | ||
| 33 | } | 34 | } | ||
| @@ -246,6 +247,49 @@ Examples: | |||||
| 246 | } | 247 | } | ||
| 247 | } | 248 | } | ||
| 248 | 249 | ||||
| 250 | + | // newRemotesSetTokenCmd creates the remotes set-token command. | |||
| 251 | + | func newRemotesSetTokenCmd() *cobra.Command { | |||
| 252 | + | return &cobra.Command{ | |||
| 253 | + | Use: "set-token <name> <token>", | |||
| 254 | + | Short: "Set the token for a remote", | |||
| 255 | + | Long: `Set or update the authentication token for a saved remote. | |||
| 256 | + | ||||
| 257 | + | This is useful when: | |||
| 258 | + | - The server uses token-based authentication (not password auth) | |||
| 259 | + | - You have a pre-generated token from 'gordon auth token generate' | |||
| 260 | + | - You want to update an expired token | |||
| 261 | + | ||||
| 262 | + | For servers with password authentication, use 'gordon auth login' instead. | |||
| 263 | + | ||||
| 264 | + | Examples: | |||
| 265 | + | gordon remotes set-token prod eyJhbGciOiJIUzI1NiIs... | |||
| 266 | + | gordon remotes set-token staging $(cat token.txt)`, | |||
| 267 | + | Args: cobra.ExactArgs(2), | |||
| 268 | + | RunE: func(cmd *cobra.Command, args []string) error { | |||
| 269 | + | name := args[0] | |||
| 270 | + | token := args[1] | |||
| 271 | + | ||||
| 272 | + | // Verify remote exists | |||
| 273 | + | remotes, _, err := remote.ListRemotes() | |||
| 274 | + | if err != nil { | |||
| 275 | + | return fmt.Errorf("failed to list remotes: %w", err) | |||
| 276 | + | } | |||
| 277 | + | ||||
| 278 | + | if _, exists := remotes[name]; !exists { | |||
| 279 | + | fmt.Println(styles.RenderError(fmt.Sprintf("Remote '%s' not found", name))) | |||
| 280 | + | return nil | |||
| 281 | + | } | |||
| 282 | + | ||||
| 283 | + | if err := remote.UpdateRemoteToken(name, token); err != nil { | |||
| 284 | + | return fmt.Errorf("failed to update token: %w", err) | |||
| 285 | + | } | |||
| 286 | + | ||||
| 287 | + | fmt.Println(styles.RenderSuccess(fmt.Sprintf("Token updated for remote '%s'", name))) | |||
| 288 | + | return nil | |||
| 289 | + | }, | |||
| 290 | + | } | |||
| 291 | + | } | |||
| 292 | + | ||||
| 249 | // normalizeURL ensures the URL has a protocol scheme, defaulting to https. | 293 | // normalizeURL ensures the URL has a protocol scheme, defaulting to https. | ||
| 250 | func normalizeURL(url string) string { | 294 | func normalizeURL(url string) string { | ||
| 251 | if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { | 295 | if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { | ||