|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/SAP-samples/sap-devs-cli/internal/config" |
| 10 | + "github.com/SAP-samples/sap-devs-cli/internal/credentials" |
| 11 | + "github.com/SAP-samples/sap-devs-cli/internal/trayctl" |
| 12 | + "github.com/SAP-samples/sap-devs-cli/internal/xdg" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var trayCmd = &cobra.Command{ |
| 17 | + Use: "tray", |
| 18 | + Short: "Manage the optional GUI tray companion", |
| 19 | + Long: `Manage the sap-devs system tray companion — an optional graphical dashboard |
| 20 | +that shows sync status, active profile, and injected tools at a glance. |
| 21 | +
|
| 22 | +Note: The tray companion uses Wails v3 (currently in alpha). |
| 23 | +This is an optional enhancement — all CLI features work without it.`, |
| 24 | +} |
| 25 | + |
| 26 | +var trayInstallCmd = &cobra.Command{ |
| 27 | + Use: "install", |
| 28 | + Short: "Download and install the tray companion binary", |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + paths, err := xdg.New() |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + mgr := &trayctl.Manager{ |
| 36 | + CacheDir: paths.CacheDir, |
| 37 | + Token: credentials.Resolve(paths.ConfigDir), |
| 38 | + Version: Version, |
| 39 | + RepoURL: repoURL, |
| 40 | + } |
| 41 | + |
| 42 | + out := cmd.OutOrStdout() |
| 43 | + fmt.Fprintln(out, "Downloading sap-devs-tray...") |
| 44 | + if err := mgr.Install(); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + fmt.Fprintln(out, "Verifying...") |
| 49 | + if err := mgr.Verify(); err != nil { |
| 50 | + return fmt.Errorf("verification failed: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + fmt.Fprintln(out, "Tray companion installed successfully.") |
| 54 | + fmt.Fprintln(out) |
| 55 | + fmt.Fprintln(out, "Note: The sap-devs tray companion uses Wails v3 (currently in alpha).") |
| 56 | + fmt.Fprintln(out, "This is an optional enhancement — all CLI features work without it.") |
| 57 | + fmt.Fprintln(out, "If you encounter issues, run `sap-devs tray uninstall` to remove it.") |
| 58 | + fmt.Fprintln(out) |
| 59 | + |
| 60 | + fmt.Fprint(out, "Start tray automatically on login? [Y/n] ") |
| 61 | + reader := bufio.NewReader(os.Stdin) |
| 62 | + answer, _ := reader.ReadString('\n') |
| 63 | + answer = strings.TrimSpace(strings.ToLower(answer)) |
| 64 | + if answer == "" || answer == "y" || answer == "yes" { |
| 65 | + if err := mgr.RegisterAutostart(); err != nil { |
| 66 | + fmt.Fprintf(out, "Warning: could not register autostart: %v\n", err) |
| 67 | + } else { |
| 68 | + fmt.Fprintln(out, "Autostart registered.") |
| 69 | + cfg, _ := config.Load(paths.ConfigDir) |
| 70 | + cfg.Tray.Autostart = true |
| 71 | + _ = cfg.Save(paths.ConfigDir) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +var trayUninstallCmd = &cobra.Command{ |
| 80 | + Use: "uninstall", |
| 81 | + Short: "Remove the tray companion", |
| 82 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 83 | + paths, err := xdg.New() |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + mgr := &trayctl.Manager{CacheDir: paths.CacheDir, Version: Version, RepoURL: repoURL} |
| 88 | + |
| 89 | + if err := mgr.UnregisterAutostart(); err != nil { |
| 90 | + fmt.Fprintf(cmd.ErrOrStderr(), "Warning: could not remove autostart entry: %v\n", err) |
| 91 | + } |
| 92 | + if err := mgr.Uninstall(); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + cfg, _ := config.Load(paths.ConfigDir) |
| 97 | + cfg.Tray.Autostart = false |
| 98 | + _ = cfg.Save(paths.ConfigDir) |
| 99 | + |
| 100 | + fmt.Fprintln(cmd.OutOrStdout(), "Tray companion uninstalled.") |
| 101 | + return nil |
| 102 | + }, |
| 103 | +} |
| 104 | + |
| 105 | +var trayStartCmd = &cobra.Command{ |
| 106 | + Use: "start", |
| 107 | + Short: "Launch the tray companion", |
| 108 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 109 | + paths, err := xdg.New() |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + mgr := &trayctl.Manager{CacheDir: paths.CacheDir} |
| 114 | + if err := mgr.Start(); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + fmt.Fprintln(cmd.OutOrStdout(), "Tray companion started.") |
| 118 | + return nil |
| 119 | + }, |
| 120 | +} |
| 121 | + |
| 122 | +var trayStopCmd = &cobra.Command{ |
| 123 | + Use: "stop", |
| 124 | + Short: "Stop the running tray companion", |
| 125 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 126 | + paths, err := xdg.New() |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + mgr := &trayctl.Manager{CacheDir: paths.CacheDir} |
| 131 | + if err := mgr.Stop(); err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + fmt.Fprintln(cmd.OutOrStdout(), "Tray companion stopped.") |
| 135 | + return nil |
| 136 | + }, |
| 137 | +} |
| 138 | + |
| 139 | +var trayStatusCmd = &cobra.Command{ |
| 140 | + Use: "status", |
| 141 | + Short: "Show tray companion status", |
| 142 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 143 | + paths, err := xdg.New() |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + mgr := &trayctl.Manager{CacheDir: paths.CacheDir, Version: Version} |
| 148 | + out := cmd.OutOrStdout() |
| 149 | + |
| 150 | + if !mgr.IsInstalled() { |
| 151 | + fmt.Fprintln(out, "Tray: not installed") |
| 152 | + fmt.Fprintln(out, "Run `sap-devs tray install` to download the tray companion.") |
| 153 | + return nil |
| 154 | + } |
| 155 | + |
| 156 | + running := "stopped" |
| 157 | + if mgr.IsRunning() { |
| 158 | + running = "running" |
| 159 | + } |
| 160 | + |
| 161 | + cfg, _ := config.Load(paths.ConfigDir) |
| 162 | + autostart := "disabled" |
| 163 | + if cfg.Tray.Autostart { |
| 164 | + autostart = "enabled" |
| 165 | + } |
| 166 | + |
| 167 | + fmt.Fprintf(out, "Tray: installed (%s)\n", running) |
| 168 | + fmt.Fprintf(out, "Autostart: %s\n", autostart) |
| 169 | + fmt.Fprintf(out, "Binary: %s\n", mgr.BinaryPath()) |
| 170 | + return nil |
| 171 | + }, |
| 172 | +} |
| 173 | + |
| 174 | +func init() { |
| 175 | + trayCmd.AddCommand(trayInstallCmd) |
| 176 | + trayCmd.AddCommand(trayUninstallCmd) |
| 177 | + trayCmd.AddCommand(trayStartCmd) |
| 178 | + trayCmd.AddCommand(trayStopCmd) |
| 179 | + trayCmd.AddCommand(trayStatusCmd) |
| 180 | + rootCmd.AddCommand(trayCmd) |
| 181 | +} |
0 commit comments