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).
This commit is contained in:
parent
42251f6df8
commit
b4cb362a7c
4 changed files with 40 additions and 43 deletions
|
@ -24,7 +24,7 @@ type LoginExecutor struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoginExecutor(printer *printer.Printer, config *config.Config, name, summary string) *LoginExecutor {
|
func NewLoginExecutor(printer *printer.Printer, config *config.Config, name, summary string) *LoginExecutor {
|
||||||
command := LoginExecutor{
|
loginExe := LoginExecutor{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
|
||||||
printer: printer,
|
printer: printer,
|
||||||
|
@ -32,21 +32,21 @@ func NewLoginExecutor(printer *printer.Printer, config *config.Config, name, sum
|
||||||
instance: "",
|
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
|
var err error
|
||||||
|
|
||||||
if c.instance == "" {
|
if l.instance == "" {
|
||||||
return FlagNotSetError{flagText: flagInstance}
|
return FlagNotSetError{flagText: flagInstance}
|
||||||
}
|
}
|
||||||
|
|
||||||
instance := c.instance
|
instance := l.instance
|
||||||
|
|
||||||
if !strings.HasPrefix(instance, "https") || !strings.HasPrefix(instance, "http") {
|
if !strings.HasPrefix(instance, "https") || !strings.HasPrefix(instance, "http") {
|
||||||
instance = "https://" + instance
|
instance = "https://" + instance
|
||||||
|
@ -68,7 +68,7 @@ func (c *LoginExecutor) Execute() error {
|
||||||
|
|
||||||
consentPageURL := gtsClient.AuthCodeURL()
|
consentPageURL := gtsClient.AuthCodeURL()
|
||||||
|
|
||||||
utilities.OpenLink(consentPageURL)
|
_ = utilities.OpenLink(l.config.Integrations.Browser, consentPageURL)
|
||||||
|
|
||||||
var builder strings.Builder
|
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\n" + "Once you have the code please copy and paste it below.")
|
||||||
builder.WriteString("\n" + "Out-of-band token: ")
|
builder.WriteString("\n" + "Out-of-band token: ")
|
||||||
|
|
||||||
c.printer.PrintInfo(builder.String())
|
l.printer.PrintInfo(builder.String())
|
||||||
|
|
||||||
var code string
|
var code string
|
||||||
|
|
||||||
|
@ -95,12 +95,12 @@ func (c *LoginExecutor) Execute() error {
|
||||||
return fmt.Errorf("unable to verify the credentials: %w", err)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to save the authentication details: %w", err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,7 +137,9 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.showInBrowser {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -177,7 +179,9 @@ func (s *ShowExecutor) showStatus(gtsClient *client.Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.showInBrowser {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
||||||
//
|
|
||||||
// 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()
|
|
||||||
}
|
|
|
@ -39,3 +39,26 @@ func OpenMedia(viewer string, paths []string) error {
|
||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue