Skip to content

Commit

Permalink
add fixes to setting custom browser (#760)
Browse files Browse the repository at this point in the history
* add fixes to setting custom browser

* if custom browser is set setup template in config
  • Loading branch information
meyerjrr authored Sep 26, 2024
1 parent 8a621b5 commit ba8a146
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/assume/assume.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func AssumeCommand(c *cli.Context) error {
}

browserPath := cfg.CustomBrowserPath
if browserPath == "" {
if browserPath == "" && cfg.AWSConsoleBrowserLaunchTemplate == nil {
return errors.New("default browser not configured. run `granted browser set` to configure")
}

Expand Down
28 changes: 20 additions & 8 deletions pkg/browser/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func UserHasDefaultBrowser(ctx *cli.Context) (bool, error) {
return false, err
}
}
return conf.DefaultBrowser != "" && conf.CustomBrowserPath != "", nil
return conf.DefaultBrowser != "" && conf.CustomBrowserPath != "" || conf.AWSConsoleBrowserLaunchTemplate != nil, nil
}

func HandleManualBrowserSelection() (string, error) {
Expand All @@ -50,7 +50,7 @@ func HandleManualBrowserSelection() (string, error) {
withStdio := survey.WithStdio(os.Stdin, os.Stderr, os.Stderr)
in := survey.Select{
Message: "Select one of the browsers from the list",
Options: []string{"Chrome", "Brave", "Edge", "Firefox", "Waterfox", "Chromium", "Safari", "Stdout", "FirefoxStdout", "Firefox Developer Edition", "Firefox Nightly", "Arc"},
Options: []string{"Chrome", "Brave", "Edge", "Firefox", "Waterfox", "Chromium", "Safari", "Stdout", "FirefoxStdout", "Firefox Developer Edition", "Firefox Nightly", "Arc", "Custom"},
}
var selection string
clio.NewLine()
Expand Down Expand Up @@ -136,6 +136,9 @@ func GetBrowserKey(b string) string {
if strings.Contains(strings.ToLower(b), "arc") {
return ArcKey
}
if strings.Contains(strings.ToLower(b), "custom") {
return CustomKey
}

return StdoutKey
}
Expand Down Expand Up @@ -215,8 +218,14 @@ func ConfigureBrowserSelection(browserName string, path string) error {
browserTitle := title.String(strings.ToLower(browserKey))
// We allow users to configure a custom install path if we cannot detect the installation
browserPath := path

//load config
conf, err := config.Load()
if err != nil {
return err
}
// detect installation
if browserKey != FirefoxStdoutKey && browserKey != StdoutKey {
if browserKey != FirefoxStdoutKey && browserKey != StdoutKey && browserKey != CustomKey {

if browserPath != "" {
_, err := os.Stat(browserPath)
Expand Down Expand Up @@ -254,12 +263,15 @@ func ConfigureBrowserSelection(browserName string, path string) error {
}
}
}
// save the detected browser as the default
conf, err := config.Load()
if err != nil {
return err

//if browser set to default but config does not include browser launch tempalate. add it.
if browserKey == CustomKey && conf.AWSConsoleBrowserLaunchTemplate == nil {
conf.AWSConsoleBrowserLaunchTemplate = &config.BrowserLaunchTemplate{
Command: "",
}
}

// save the detected browser as the default
conf.DefaultBrowser = browserKey
conf.CustomBrowserPath = browserPath
err = conf.Save()
Expand Down Expand Up @@ -385,7 +397,7 @@ func AskAndGetBrowserPath() (string, error) {
// We allow users to configure a custom install path is we cannot detect the installation
browserPath := ""
// detect installation
if browserKey != FirefoxStdoutKey && browserKey != StdoutKey {
if browserKey != FirefoxStdoutKey && browserKey != StdoutKey && browserKey != CustomKey {

customBrowserPath, detected := DetectInstallation(browserKey)
if !detected {
Expand Down
2 changes: 2 additions & 0 deletions pkg/granted/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ var DefaultBrowserCommand = cli.Command{
Usage: "View the web browser that Granted uses to open cloud consoles",
Subcommands: []*cli.Command{&SetBrowserCommand, &SetSSOBrowserCommand},
Action: func(c *cli.Context) error {

// return the default browser that is set
conf, err := config.Load()
if err != nil {
return err
}

clio.Infof("Granted is using %s. To change this run `granted browser set`", conf.DefaultBrowser)

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/granted/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var ConsoleCommand = cli.Command{
var l assume.Launcher
if cfg.CustomBrowserPath == "" && cfg.DefaultBrowser != "" {
l = launcher.Open{}
} else if cfg.CustomBrowserPath == "" {
} else if cfg.CustomBrowserPath == "" && cfg.AWSConsoleBrowserLaunchTemplate == nil {
return errors.New("default browser not configured. run `granted browser set` to configure")
} else {
switch cfg.DefaultBrowser {
Expand Down

0 comments on commit ba8a146

Please sign in to comment.