From 195944a9938fc483f4861f3bece0a190de35e9ca Mon Sep 17 00:00:00 2001 From: Jason Bao Date: Tue, 4 Nov 2025 14:29:10 -0800 Subject: [PATCH] Add open editor helper --- cmd/app/app.go | 1 + cmd/app/editor.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/user/login.go | 23 ++--------------------- configs/local.json | 2 +- configs/prod.json | 2 +- utils/browser.go | 25 +++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 cmd/app/editor.go create mode 100644 utils/browser.go diff --git a/cmd/app/app.go b/cmd/app/app.go index 3eed9ed..059c68c 100644 --- a/cmd/app/app.go +++ b/cmd/app/app.go @@ -19,4 +19,5 @@ func init() { Cmd.AddCommand(generateResourcesCmd) Cmd.AddCommand(startCmd) Cmd.AddCommand(deployCmd) + Cmd.AddCommand(editCmd) } diff --git a/cmd/app/editor.go b/cmd/app/editor.go new file mode 100644 index 0000000..a866059 --- /dev/null +++ b/cmd/app/editor.go @@ -0,0 +1,46 @@ +package app + +import ( + "fmt" + + "github.com/major-technology/cli/singletons" + "github.com/major-technology/cli/utils" + "github.com/spf13/cobra" +) + +// editCmd represents the editor command +var editCmd = &cobra.Command{ + Use: "editor", + Short: "Open the application editor in your browser", + Long: `Open the application editor in your default browser for the current application.`, + Run: func(cmd *cobra.Command, args []string) { + cobra.CheckErr(runEdit(cmd)) + }, +} + +func runEdit(cmd *cobra.Command) error { + // Get application ID + applicationID, err := getApplicationID() + if err != nil { + return err + } + + // Get config to access frontend URI + cfg := singletons.GetConfig() + if cfg == nil { + return fmt.Errorf("configuration not initialized") + } + + // Construct the editor URL + editorURL := fmt.Sprintf("%s/apps/%s/edit", cfg.FrontendURI, applicationID) + + // Open the URL in the browser + if err := utils.OpenBrowser(editorURL); err != nil { + // If browser fails to open, still show the URL + cmd.Printf("Failed to open browser automatically. Please visit:\n%s\n", editorURL) + return nil + } + + cmd.Printf("Opening application editor in your browser:\n%s\n", editorURL) + return nil +} diff --git a/cmd/user/login.go b/cmd/user/login.go index f393717..dca6d94 100644 --- a/cmd/user/login.go +++ b/cmd/user/login.go @@ -2,14 +2,13 @@ package user import ( "fmt" - "os/exec" - "runtime" "time" "github.com/charmbracelet/huh" apiClient "github.com/major-technology/cli/clients/api" mjrToken "github.com/major-technology/cli/clients/token" "github.com/major-technology/cli/singletons" + "github.com/major-technology/cli/utils" "github.com/spf13/cobra" ) @@ -31,7 +30,7 @@ func runLogin(cobraCmd *cobra.Command) error { return fmt.Errorf("failed to start login: %w", err) } - if err := openBrowser(startResp.VerificationURI); err != nil { + if err := utils.OpenBrowser(startResp.VerificationURI); err != nil { // ignore, failed to open browser } cobraCmd.Println("Attempting to automatically open the SSO authorization page in your default browser.") @@ -71,24 +70,6 @@ func runLogin(cobraCmd *cobra.Command) error { return nil } -// openBrowser opens the specified URL in the default browser -func openBrowser(url string) error { - var execCmd *exec.Cmd - - switch runtime.GOOS { - case "linux": - execCmd = exec.Command("xdg-open", url) - case "windows": - execCmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url) - case "darwin": - execCmd = exec.Command("open", url) - default: - return fmt.Errorf("unsupported platform") - } - - return execCmd.Start() -} - // pollForToken polls POST /cli/login/poll until authenticated or timeout func pollForToken(cobraCmd *cobra.Command, client *apiClient.Client, deviceCode string, interval int, expiresIn int) (string, error) { ticker := time.NewTicker(time.Duration(interval) * time.Second) diff --git a/configs/local.json b/configs/local.json index f9f6daf..86e41b6 100644 --- a/configs/local.json +++ b/configs/local.json @@ -1,4 +1,4 @@ { "api_url": "http://localhost:3001/cli", - "frontend_uri": "http://localhost:3000/device" + "frontend_uri": "http://localhost:3000" } \ No newline at end of file diff --git a/configs/prod.json b/configs/prod.json index 2c66e0d..af07d0a 100644 --- a/configs/prod.json +++ b/configs/prod.json @@ -1,4 +1,4 @@ { "api_url": "https://api.prod.major.build/cli", - "frontend_uri": "https://app.major.build/device" + "frontend_uri": "https://app.major.build" } \ No newline at end of file diff --git a/utils/browser.go b/utils/browser.go new file mode 100644 index 0000000..e9424a3 --- /dev/null +++ b/utils/browser.go @@ -0,0 +1,25 @@ +package utils + +import ( + "fmt" + "os/exec" + "runtime" +) + +// OpenBrowser opens the specified URL in the default browser +func OpenBrowser(url string) error { + var execCmd *exec.Cmd + + switch runtime.GOOS { + case "linux": + execCmd = exec.Command("xdg-open", url) + case "windows": + execCmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url) + case "darwin": + execCmd = exec.Command("open", url) + default: + return fmt.Errorf("unsupported platform") + } + + return execCmd.Start() +}