|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + api "github.com/bootdotdev/bootdev/client" |
| 7 | + "github.com/bootdotdev/bootdev/version" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/viper" |
| 10 | +) |
| 11 | + |
| 12 | +var statusCmd = &cobra.Command{ |
| 13 | + Use: "status", |
| 14 | + Short: "Show authentication and CLI version status", |
| 15 | + Long: "Display whether you're logged in and whether the CLI is up to date", |
| 16 | + Run: func(cmd *cobra.Command, args []string) { |
| 17 | + checkAuthStatus() |
| 18 | + fmt.Println() // Blank line for readability |
| 19 | + checkVersionStatus(cmd) |
| 20 | + }, |
| 21 | +} |
| 22 | + |
| 23 | +func checkAuthStatus() { |
| 24 | + refreshToken := viper.GetString("refresh_token") |
| 25 | + if refreshToken == "" { |
| 26 | + fmt.Println("Not logged in") |
| 27 | + fmt.Println("Run 'bootdev login' to authenticate") |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + // Verify token is still valid by attempting to refresh |
| 32 | + _, err := api.FetchAccessToken() |
| 33 | + if err != nil { |
| 34 | + fmt.Println("Authentication expired") |
| 35 | + fmt.Println("Run 'bootdev login' to re-authenticate") |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + fmt.Println("Logged in") |
| 40 | + // TODO: Consider adding user data endpoint to show email/username |
| 41 | +} |
| 42 | + |
| 43 | +func checkVersionStatus(cmd *cobra.Command) { |
| 44 | + info := version.FromContext(cmd.Context()) |
| 45 | + if info == nil || info.FailedToFetch != nil { |
| 46 | + fmt.Println("Unable to check version status") |
| 47 | + if info != nil && info.FailedToFetch != nil { |
| 48 | + fmt.Printf("Error: %s\n", info.FailedToFetch.Error()) |
| 49 | + } |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if info.IsOutdated { |
| 54 | + fmt.Printf("CLI outdated: %s → %s available\n", info.CurrentVersion, info.LatestVersion) |
| 55 | + fmt.Println("Run 'bootdev upgrade' to update") |
| 56 | + } else { |
| 57 | + fmt.Printf("CLI up to date (%s)\n", info.CurrentVersion) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func init() { |
| 62 | + rootCmd.AddCommand(statusCmd) |
| 63 | +} |
0 commit comments