|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + adapterconfig "github.com/nylas/cli/internal/adapters/config" |
| 9 | + "github.com/nylas/cli/internal/adapters/keyring" |
| 10 | + authapp "github.com/nylas/cli/internal/app/auth" |
| 11 | + "github.com/nylas/cli/internal/cli/common" |
| 12 | + "github.com/nylas/cli/internal/domain" |
| 13 | + "github.com/nylas/cli/internal/ports" |
| 14 | +) |
| 15 | + |
| 16 | +func newResetCmd() *cobra.Command { |
| 17 | + var force bool |
| 18 | + |
| 19 | + cmd := &cobra.Command{ |
| 20 | + Use: "reset", |
| 21 | + Short: "Reset all CLI configuration and credentials", |
| 22 | + Long: `Reset the Nylas CLI to a clean state by clearing all stored data: |
| 23 | +
|
| 24 | + - API credentials (API key, client ID, client secret) |
| 25 | + - Dashboard session (login tokens, selected app) |
| 26 | + - Grants (authenticated email accounts) |
| 27 | + - Config file (reset to defaults) |
| 28 | +
|
| 29 | +After reset, run 'nylas init' to set up again. |
| 30 | +
|
| 31 | +To reset only part of the CLI: |
| 32 | + nylas auth config --reset Reset API credentials only |
| 33 | + nylas dashboard logout Log out of Dashboard only`, |
| 34 | + Example: ` # Reset with confirmation prompt |
| 35 | + nylas config reset |
| 36 | +
|
| 37 | + # Reset without confirmation |
| 38 | + nylas config reset --force`, |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + if !force { |
| 41 | + fmt.Println("This will remove all stored credentials, grants, and configuration.") |
| 42 | + fmt.Println() |
| 43 | + if !common.Confirm("Are you sure you want to reset the CLI?", false) { |
| 44 | + fmt.Println("Reset cancelled.") |
| 45 | + return nil |
| 46 | + } |
| 47 | + fmt.Println() |
| 48 | + } |
| 49 | + |
| 50 | + secretStore, err := keyring.NewSecretStore(adapterconfig.DefaultConfigDir()) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("access secret store: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + // 1. Clear API credentials |
| 56 | + configSvc := authapp.NewConfigService(configStore, secretStore) |
| 57 | + if err := configSvc.ResetConfig(); err != nil { |
| 58 | + return fmt.Errorf("reset API config: %w", err) |
| 59 | + } |
| 60 | + _, _ = common.Green.Println(" ✓ API credentials cleared") |
| 61 | + |
| 62 | + // 2. Clear dashboard credentials |
| 63 | + clearDashboardCredentials(secretStore) |
| 64 | + _, _ = common.Green.Println(" ✓ Dashboard session cleared") |
| 65 | + |
| 66 | + // 3. Clear grants |
| 67 | + grantStore := keyring.NewGrantStore(secretStore) |
| 68 | + if err := grantStore.ClearGrants(); err != nil { |
| 69 | + return fmt.Errorf("clear grants: %w", err) |
| 70 | + } |
| 71 | + _, _ = common.Green.Println(" ✓ Grants cleared") |
| 72 | + |
| 73 | + // 4. Reset config file to defaults |
| 74 | + if err := configStore.Save(domain.DefaultConfig()); err != nil { |
| 75 | + return fmt.Errorf("reset config file: %w", err) |
| 76 | + } |
| 77 | + _, _ = common.Green.Println(" ✓ Config file reset") |
| 78 | + |
| 79 | + fmt.Println() |
| 80 | + _, _ = common.Green.Println("CLI has been reset.") |
| 81 | + fmt.Println() |
| 82 | + fmt.Println("Run 'nylas init' to set up again.") |
| 83 | + |
| 84 | + return nil |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + cmd.Flags().BoolVar(&force, "force", false, "Skip confirmation prompt") |
| 89 | + |
| 90 | + return cmd |
| 91 | +} |
| 92 | + |
| 93 | +// clearDashboardCredentials removes all dashboard-related keys from the secret store. |
| 94 | +func clearDashboardCredentials(secrets ports.SecretStore) { |
| 95 | + _ = secrets.Delete(ports.KeyDashboardUserToken) |
| 96 | + _ = secrets.Delete(ports.KeyDashboardOrgToken) |
| 97 | + _ = secrets.Delete(ports.KeyDashboardUserPublicID) |
| 98 | + _ = secrets.Delete(ports.KeyDashboardOrgPublicID) |
| 99 | + _ = secrets.Delete(ports.KeyDashboardDPoPKey) |
| 100 | + _ = secrets.Delete(ports.KeyDashboardAppID) |
| 101 | + _ = secrets.Delete(ports.KeyDashboardAppRegion) |
| 102 | +} |
0 commit comments