Skip to content

Commit 2dc8fd8

Browse files
authored
Add open editor helper (#18)
1 parent 29d8726 commit 2dc8fd8

6 files changed

Lines changed: 76 additions & 23 deletions

File tree

cmd/app/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ func init() {
1919
Cmd.AddCommand(generateResourcesCmd)
2020
Cmd.AddCommand(startCmd)
2121
Cmd.AddCommand(deployCmd)
22+
Cmd.AddCommand(editCmd)
2223
}

cmd/app/editor.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/major-technology/cli/singletons"
7+
"github.com/major-technology/cli/utils"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// editCmd represents the editor command
12+
var editCmd = &cobra.Command{
13+
Use: "editor",
14+
Short: "Open the application editor in your browser",
15+
Long: `Open the application editor in your default browser for the current application.`,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
cobra.CheckErr(runEdit(cmd))
18+
},
19+
}
20+
21+
func runEdit(cmd *cobra.Command) error {
22+
// Get application ID
23+
applicationID, err := getApplicationID()
24+
if err != nil {
25+
return err
26+
}
27+
28+
// Get config to access frontend URI
29+
cfg := singletons.GetConfig()
30+
if cfg == nil {
31+
return fmt.Errorf("configuration not initialized")
32+
}
33+
34+
// Construct the editor URL
35+
editorURL := fmt.Sprintf("%s/apps/%s/edit", cfg.FrontendURI, applicationID)
36+
37+
// Open the URL in the browser
38+
if err := utils.OpenBrowser(editorURL); err != nil {
39+
// If browser fails to open, still show the URL
40+
cmd.Printf("Failed to open browser automatically. Please visit:\n%s\n", editorURL)
41+
return nil
42+
}
43+
44+
cmd.Printf("Opening application editor in your browser:\n%s\n", editorURL)
45+
return nil
46+
}

cmd/user/login.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package user
22

33
import (
44
"fmt"
5-
"os/exec"
6-
"runtime"
75
"time"
86

97
"github.com/charmbracelet/huh"
108
apiClient "github.com/major-technology/cli/clients/api"
119
mjrToken "github.com/major-technology/cli/clients/token"
1210
"github.com/major-technology/cli/singletons"
11+
"github.com/major-technology/cli/utils"
1312
"github.com/spf13/cobra"
1413
)
1514

@@ -31,7 +30,7 @@ func runLogin(cobraCmd *cobra.Command) error {
3130
return fmt.Errorf("failed to start login: %w", err)
3231
}
3332

34-
if err := openBrowser(startResp.VerificationURI); err != nil {
33+
if err := utils.OpenBrowser(startResp.VerificationURI); err != nil {
3534
// ignore, failed to open browser
3635
}
3736
cobraCmd.Println("Attempting to automatically open the SSO authorization page in your default browser.")
@@ -71,24 +70,6 @@ func runLogin(cobraCmd *cobra.Command) error {
7170
return nil
7271
}
7372

74-
// openBrowser opens the specified URL in the default browser
75-
func openBrowser(url string) error {
76-
var execCmd *exec.Cmd
77-
78-
switch runtime.GOOS {
79-
case "linux":
80-
execCmd = exec.Command("xdg-open", url)
81-
case "windows":
82-
execCmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
83-
case "darwin":
84-
execCmd = exec.Command("open", url)
85-
default:
86-
return fmt.Errorf("unsupported platform")
87-
}
88-
89-
return execCmd.Start()
90-
}
91-
9273
// pollForToken polls POST /cli/login/poll until authenticated or timeout
9374
func pollForToken(cobraCmd *cobra.Command, client *apiClient.Client, deviceCode string, interval int, expiresIn int) (string, error) {
9475
ticker := time.NewTicker(time.Duration(interval) * time.Second)

configs/local.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"api_url": "http://localhost:3001/cli",
3-
"frontend_uri": "http://localhost:3000/device"
3+
"frontend_uri": "http://localhost:3000"
44
}

configs/prod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"api_url": "https://api.prod.major.build/cli",
3-
"frontend_uri": "https://app.major.build/device"
3+
"frontend_uri": "https://app.major.build"
44
}

utils/browser.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"runtime"
7+
)
8+
9+
// OpenBrowser opens the specified URL in the default browser
10+
func OpenBrowser(url string) error {
11+
var execCmd *exec.Cmd
12+
13+
switch runtime.GOOS {
14+
case "linux":
15+
execCmd = exec.Command("xdg-open", url)
16+
case "windows":
17+
execCmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
18+
case "darwin":
19+
execCmd = exec.Command("open", url)
20+
default:
21+
return fmt.Errorf("unsupported platform")
22+
}
23+
24+
return execCmd.Start()
25+
}

0 commit comments

Comments
 (0)