From b4cb362a7cd819ac292ccea586d811b7040b1a54 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 26 Jun 2024 12:38:27 +0100 Subject: [PATCH] fix: use browser setting in config for URLs Use the browser specified in the configuration file to open URLs. If the browser is not specified the link will not be opened and an error is returned to the user in most cases. This replaces the use of the BROWSER environment variable and the xdg-open command (on linux). --- internal/executor/login.go | 22 +++++++++++----------- internal/executor/show.go | 8 ++++++-- internal/utilities/browser.go | 30 ------------------------------ internal/utilities/utilities.go | 23 +++++++++++++++++++++++ 4 files changed, 40 insertions(+), 43 deletions(-) delete mode 100644 internal/utilities/browser.go diff --git a/internal/executor/login.go b/internal/executor/login.go index 975ed22..c84f3ea 100644 --- a/internal/executor/login.go +++ b/internal/executor/login.go @@ -24,7 +24,7 @@ type LoginExecutor struct { } func NewLoginExecutor(printer *printer.Printer, config *config.Config, name, summary string) *LoginExecutor { - command := LoginExecutor{ + loginExe := LoginExecutor{ FlagSet: flag.NewFlagSet(name, flag.ExitOnError), printer: printer, @@ -32,21 +32,21 @@ func NewLoginExecutor(printer *printer.Printer, config *config.Config, name, sum instance: "", } - command.StringVar(&command.instance, flagInstance, "", "Specify the instance that you want to login to.") + loginExe.StringVar(&loginExe.instance, flagInstance, "", "Specify the instance that you want to login to.") - command.Usage = commandUsageFunc(name, summary, command.FlagSet) + loginExe.Usage = commandUsageFunc(name, summary, loginExe.FlagSet) - return &command + return &loginExe } -func (c *LoginExecutor) Execute() error { +func (l *LoginExecutor) Execute() error { var err error - if c.instance == "" { + if l.instance == "" { return FlagNotSetError{flagText: flagInstance} } - instance := c.instance + instance := l.instance if !strings.HasPrefix(instance, "https") || !strings.HasPrefix(instance, "http") { instance = "https://" + instance @@ -68,7 +68,7 @@ func (c *LoginExecutor) Execute() error { consentPageURL := gtsClient.AuthCodeURL() - utilities.OpenLink(consentPageURL) + _ = utilities.OpenLink(l.config.Integrations.Browser, consentPageURL) var builder strings.Builder @@ -78,7 +78,7 @@ func (c *LoginExecutor) Execute() error { builder.WriteString("\n\n" + "Once you have the code please copy and paste it below.") builder.WriteString("\n" + "Out-of-band token: ") - c.printer.PrintInfo(builder.String()) + l.printer.PrintInfo(builder.String()) var code string @@ -95,12 +95,12 @@ func (c *LoginExecutor) Execute() error { return fmt.Errorf("unable to verify the credentials: %w", err) } - loginName, err := config.SaveCredentials(c.config.CredentialsFile, account.Username, gtsClient.Authentication) + loginName, err := config.SaveCredentials(l.config.CredentialsFile, account.Username, gtsClient.Authentication) if err != nil { return fmt.Errorf("unable to save the authentication details: %w", err) } - c.printer.PrintSuccess("Successfully logged into " + loginName + ".") + l.printer.PrintSuccess("Successfully logged into " + loginName + ".") return nil } diff --git a/internal/executor/show.go b/internal/executor/show.go index f8b47a1..2926c77 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -137,7 +137,9 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error { } if s.showInBrowser { - utilities.OpenLink(account.URL) + if err := utilities.OpenLink(s.config.Integrations.Browser, account.URL); err != nil { + return fmt.Errorf("unable to open link: %w", err) + } return nil } @@ -177,7 +179,9 @@ func (s *ShowExecutor) showStatus(gtsClient *client.Client) error { } if s.showInBrowser { - utilities.OpenLink(status.URL) + if err := utilities.OpenLink(s.config.Integrations.Browser, status.URL); err != nil { + return fmt.Errorf("unable to open link: %w", err) + } return nil } diff --git a/internal/utilities/browser.go b/internal/utilities/browser.go deleted file mode 100644 index 7e251ba..0000000 --- a/internal/utilities/browser.go +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Dan Anglin -// -// SPDX-License-Identifier: GPL-3.0-or-later - -package utilities - -import ( - "os" - "os/exec" - "runtime" -) - -func OpenLink(url string) { - var open string - - envBrower := os.Getenv("BROWSER") - - switch { - case len(envBrower) > 0: - open = envBrower - case runtime.GOOS == "linux": - open = "xdg-open" - default: - return - } - - command := exec.Command(open, url) - - _ = command.Start() -} diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index c36120e..141eb91 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -39,3 +39,26 @@ func OpenMedia(viewer string, paths []string) error { return nil } + +type UnspecifiedBrowserError struct{} + +func (e UnspecifiedBrowserError) Error() string { + return "the browser to view this link is not specified" +} + +func OpenLink(browser, url string) error { + if browser == "" { + return UnspecifiedBrowserError{} + } + + cmd := strings.Split(browser, " ") + cmd = append(cmd, url) + + command := exec.Command(cmd[0], cmd[1:]...) + + if err := command.Start(); err != nil { + return fmt.Errorf("received an error after starting the program to view the link: %w", err) + } + + return nil +}