|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/HenryOwenz/cloudgate/internal/cmd/commands" |
| 8 | + "github.com/HenryOwenz/cloudgate/internal/cmd/version" |
| 9 | + "github.com/HenryOwenz/cloudgate/internal/ui" |
| 10 | + tea "github.com/charmbracelet/bubbletea" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// rootCmd represents the base command when called without any subcommands |
| 15 | +var rootCmd = &cobra.Command{ |
| 16 | + Use: "cg", |
| 17 | + Short: "A terminal-based application that unifies multi-cloud operations", |
| 18 | + Long: `cloudgate is a terminal-based application that unifies multi-cloud operations |
| 19 | +across AWS, Azure, and GCP. |
| 20 | +
|
| 21 | +Where your clouds converge.`, |
| 22 | + Run: func(cmd *cobra.Command, args []string) { |
| 23 | + // Check if upgrade flag is set |
| 24 | + upgrade, _ := cmd.Flags().GetBool("upgrade") |
| 25 | + if upgrade { |
| 26 | + // Run the upgrade command |
| 27 | + upgradeCmd := commands.NewUpgradeCmd() |
| 28 | + upgradeCmd.Run(cmd, args) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + // Check if version flag is set |
| 33 | + versionFlag, _ := cmd.Flags().GetBool("version") |
| 34 | + if versionFlag { |
| 35 | + // Run the version command |
| 36 | + versionCmd := commands.NewVersionCmd() |
| 37 | + versionCmd.Run(cmd, args) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // Default behavior - run the UI |
| 42 | + // Clear the screen using ANSI escape codes (works cross-platform) |
| 43 | + fmt.Print("\033[H\033[2J") |
| 44 | + |
| 45 | + // Create and run the program |
| 46 | + p := tea.NewProgram(ui.New()) |
| 47 | + |
| 48 | + if _, err := p.Run(); err != nil { |
| 49 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + |
| 53 | + // After the UI exits, check for new version and display message if available |
| 54 | + fmt.Print(version.ColoredUpdateMessage()) |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 59 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 60 | +func Execute() { |
| 61 | + err := rootCmd.Execute() |
| 62 | + if err != nil { |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func init() { |
| 68 | + // Add the upgrade flag to the root command |
| 69 | + rootCmd.Flags().BoolP("upgrade", "u", false, "Upgrade cloudgate to the latest version") |
| 70 | + |
| 71 | + // Add the version flag to the root command |
| 72 | + rootCmd.Flags().BoolP("version", "v", false, "Display the current version of cloudgate") |
| 73 | + |
| 74 | + // Add commands |
| 75 | + rootCmd.AddCommand(commands.NewUpgradeCmd()) |
| 76 | + rootCmd.AddCommand(commands.NewVersionCmd()) |
| 77 | +} |
0 commit comments