Skip to content

Commit

Permalink
Add Firefox container config to guided setup
Browse files Browse the repository at this point in the history
Guided setup now prompts if you want to use Firefox
containers.  If you say no, then you'll get the older
workflow for UrlAction.

Non-Firefox container workflow now prompts if you want
to use a non-default browser.

Fixes: #345
  • Loading branch information
synfinatic committed May 1, 2022
1 parent 0deb416 commit dbc44b7
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 27 deletions.
156 changes: 131 additions & 25 deletions cmd/setup_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ package main
import (
"fmt"
"net"
"os"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/manifoldco/promptui"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/aws-sso-cli/utils"
)

// https://docs.aws.amazon.com/general/latest/gr/sso.html
Expand Down Expand Up @@ -57,13 +58,14 @@ const (
// SetupCmd defines the Kong args for the setup command (which currently doesn't exist)
type SetupCmd struct {
DefaultRegion string `kong:"help='Default AWS region for running commands (or \"None\")'"`
UrlAction string `kong:"name='default-url-action',help='How to handle URLs [open|print|clip]'"`
SSOStartHostname string `kong:"help='AWS SSO User Portal Hostname'"`
SSORegion string `kong:"help='AWS SSO Instance Region'"`
HistoryLimit int64 `kong:"help='Number of items to keep in History',default=-1"`
HistoryMinutes int64 `kong:"help='Number of minutes to keep items in History',default=-1"`
DefaultLevel string `kong:"help='Logging level [error|warn|info|debug|trace]'"`
Force bool `kong:"help='Force override of existing config file'"`
FirefoxPath string `kong:"help='Path to the Firefox web browser'"`
RanSetup bool `kong:"hidden"` // track if setup has already run
}

// Run executes the setup command
Expand All @@ -74,13 +76,16 @@ func (cc *SetupCmd) Run(ctx *RunContext) error {
func setupWizard(ctx *RunContext) error {
var err error
var instanceName, startHostname, ssoRegion, awsRegion, urlAction string
var historyLimit, historyMinutes, logLevel string
var historyLimit, historyMinutes, logLevel, useFirefox, firefoxBrowserPath, browser string
var hLimit, hMinutes int64
urlExecCommand := []string{}
firefoxOpenUrlInContainer := false

if ctx.Cli.Setup.UrlAction != "" {
utils.NewHandleUrl(ctx.Cli.Setup.UrlAction, "", "")
urlAction = ctx.Cli.Setup.UrlAction
// Don't run setup twice
if ctx.Cli.Setup.RanSetup {
return nil
}
ctx.Cli.Setup.RanSetup = true

if ctx.Cli.Setup.DefaultLevel != "" {
if err := logLevelValidate(ctx.Cli.Setup.DefaultLevel); err != nil {
Expand Down Expand Up @@ -168,22 +173,82 @@ func setupWizard(ctx *RunContext) error {
awsRegion = ""
}

// UrlAction
if len(urlAction) == 0 {
// How should we deal with URLs? Note we don't support `exec`
// here since that is an "advanced" feature
label = "Default action to take with URLs (UrlAction)"
sel = promptui.Select{
Label: label,
Items: []string{"open", "print", "printurl", "clip"},
Stdout: &bellSkipper{},
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf(`%s: {{ . | faint }}`, label),
},
// Firefox?
label = "Use Firefox containers to open URLs?"
sel = promptui.Select{
Label: label,
HideSelected: false,
Items: []string{"Yes", "No"},
Stdout: &bellSkipper{},
}
if _, useFirefox, err = sel.Run(); err != nil {
return err
}

if useFirefox == "Yes" {
fmt.Printf("Ensure that you have the 'Open external links in a container' plugin for Firefox.")
urlAction = "exec"
label = "Path to Firefox binary"
prompt = promptui.Prompt{
Label: label,
Stdout: &bellSkipper{},
Default: firefoxDefaultBrowserPath(ctx.Cli.Setup.FirefoxPath),
Pointer: promptui.PipeCursor,
Validate: validateBinary,
}
if _, urlAction, err = sel.Run(); err != nil {
if firefoxBrowserPath, err = prompt.Run(); err != nil {
return err
}

urlExecCommand = []string{
firefoxBrowserPath,
`%s`,
}
firefoxOpenUrlInContainer = true

} else {
// UrlAction
if len(urlAction) == 0 {
// How should we deal with URLs? Note we don't support `exec`
// here since that is an "advanced" feature
label = "Default action to take with URLs (UrlAction)"
sel = promptui.Select{
Label: label,
Items: []string{"open", "print", "printurl", "clip"},
Stdout: &bellSkipper{},
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf(`%s: {{ . | faint }}`, label),
},
}
if _, urlAction, err = sel.Run(); err != nil {
return err
}
}

if urlAction == "open" {
override := ""
sel = promptui.Select{
Label: "Override default browser?",
Items: []string{"No", "Yes"},
Stdout: &bellSkipper{},
}
if _, override, err = sel.Run(); err != nil {
return err
}

if override == "Yes" {
prompt = promptui.Prompt{
Label: "Specify path to browser to use",
Default: ctx.Cli.Browser,
Stdout: &bellSkipper{},
Pointer: promptui.PipeCursor,
Validate: validateBinary,
}
}
if browser, err = prompt.Run(); err != nil {
return err
}
}
}

// HistoryLimit
Expand Down Expand Up @@ -247,12 +312,15 @@ func setupWizard(ctx *RunContext) error {

// write config file
s := sso.Settings{
DefaultSSO: instanceName,
SSO: map[string]*sso.SSOConfig{},
UrlAction: urlAction,
HistoryLimit: hLimit,
HistoryMinutes: hMinutes,
LogLevel: logLevel,
DefaultSSO: instanceName,
SSO: map[string]*sso.SSOConfig{},
UrlAction: urlAction,
UrlExecCommand: urlExecCommand,
FirefoxOpenUrlInContainer: firefoxOpenUrlInContainer,
Browser: browser,
HistoryLimit: hLimit,
HistoryMinutes: hMinutes,
LogLevel: logLevel,
}
s.SSO[instanceName] = &sso.SSOConfig{
SSORegion: ssoRegion,
Expand Down Expand Up @@ -296,3 +364,41 @@ func validateInteger(input string) error {
}
return nil
}

// returns the default path to the firefox browser
func firefoxDefaultBrowserPath(path string) string {
if len(path) != 0 {
return path
}

switch runtime.GOOS {
case "darwin":
path = "/Applications/Firefox.app/Contents/MacOS/firefox"
case "linux":
path = "/usr/bin/firefox"
case "windows":
path = "\\Program Files\\Mozilla Firefox\\firefox.exe"
default:
}
return path
}

func validateBinary(input string) error {
s, err := os.Stat(input)
if err != nil {
return err
}
switch runtime.GOOS {
case "windows":
// Windows doesn't have file permissions
if s.Mode().IsRegular() {
return nil
}
default:
// must be a file and user execute bit set
if s.Mode().IsRegular() && s.Mode().Perm()&0100 > 0 {
return nil
}
}
return fmt.Errorf("not a valid valid")
}
4 changes: 2 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ UrlAction: open
```yaml
# Open the AWS Console using Brave on MacOS
UrlAction: exec
UrlActionExec:
UrlExecCommand:
- open
- -a
- /Applications/Brave Browser.app
Expand Down Expand Up @@ -257,7 +257,7 @@ Example:
```yaml
# Open the AWS Console in a Firefox container on MacOS
UrlAction: exec
UrlActionExec:
UrlExecCommand:
- /Applications/Firefox.app/Contents/MacOS/firefox
- "%s"
FirefoxOpenUrlInContainer: True
Expand Down

0 comments on commit dbc44b7

Please sign in to comment.