From 77b2a2a6e51d9595f057ed1f60f0861b540d6328 Mon Sep 17 00:00:00 2001 From: meyerjrr Date: Wed, 18 Sep 2024 17:34:25 +1000 Subject: [PATCH] update custom browser logic to handle custom cases for google chrome --- pkg/assume/entrypoint.go | 1 + pkg/config/config.go | 2 +- pkg/launcher/custom.go | 24 +++++++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/assume/entrypoint.go b/pkg/assume/entrypoint.go index 32f86660..c85c2377 100644 --- a/pkg/assume/entrypoint.go +++ b/pkg/assume/entrypoint.go @@ -116,6 +116,7 @@ func GetCliApp() *cli.App { if err != nil { return err } + if !hasSetup { browserName, err := browser.HandleBrowserWizard(c) if err != nil { diff --git a/pkg/config/config.go b/pkg/config/config.go index 934b9bbb..f53a529c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -285,7 +285,7 @@ func Load() (*Config, error) { _, err = toml.NewDecoder(file).Decode(&c) if err != nil { // if there is an error just reset the file - return &c, nil + return nil, err } return &c, nil } diff --git a/pkg/launcher/custom.go b/pkg/launcher/custom.go index 18c30ccb..67c2bbf6 100644 --- a/pkg/launcher/custom.go +++ b/pkg/launcher/custom.go @@ -3,6 +3,7 @@ package launcher import ( "errors" "fmt" + "regexp" "strings" "text/template" @@ -52,10 +53,31 @@ func (l Custom) LaunchCommand(url string, profile string) ([]string, error) { return nil, fmt.Errorf("executing command template (check that your browser launch template is valid in your Granted config): %w", err) } - commandParts := strings.Fields(renderedCommand.String()) + commandParts := splitCommand(renderedCommand.String()) return commandParts, nil } +// splits each component of the command. Anything within quotes will be handled as one component of the command +// eg open -a "Google Chrome" returns ["open", "-a", "Google Chrome", ""] +func splitCommand(command string) []string { + + re := regexp.MustCompile(`"([^"]+)"|(\S+)`) + matches := re.FindAllStringSubmatch(command, -1) + + var result []string + for _, match := range matches { + + if match[1] != "" { + result = append(result, match[1]) + } else { + + result = append(result, match[2]) + } + } + + return result +} + func (l Custom) UseForkProcess() bool { return l.ForkProcess } var ErrLaunchTemplateNotConfigured = errors.New("launch template is not configured")