From ba8a14611eb742c0fa30184fa542b7d6370285e1 Mon Sep 17 00:00:00 2001 From: meyerjrr <33018450+meyerjrr@users.noreply.github.com> Date: Thu, 26 Sep 2024 23:35:23 +1000 Subject: [PATCH] add fixes to setting custom browser (#760) * add fixes to setting custom browser * if custom browser is set setup template in config --- pkg/assume/assume.go | 2 +- pkg/browser/detect.go | 28 ++++++++++++++++++++-------- pkg/granted/browser.go | 2 ++ pkg/granted/console.go | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/pkg/assume/assume.go b/pkg/assume/assume.go index b9bf7da4..0cacd12b 100644 --- a/pkg/assume/assume.go +++ b/pkg/assume/assume.go @@ -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") } diff --git a/pkg/browser/detect.go b/pkg/browser/detect.go index a779da51..a94b7cde 100644 --- a/pkg/browser/detect.go +++ b/pkg/browser/detect.go @@ -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) { @@ -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() @@ -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 } @@ -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) @@ -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() @@ -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 { diff --git a/pkg/granted/browser.go b/pkg/granted/browser.go index c301b9bf..8d7791e3 100644 --- a/pkg/granted/browser.go +++ b/pkg/granted/browser.go @@ -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 diff --git a/pkg/granted/console.go b/pkg/granted/console.go index 9fb89953..535788b5 100644 --- a/pkg/granted/console.go +++ b/pkg/granted/console.go @@ -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 {