Skip to content

Commit

Permalink
Lots of setup improvements
Browse files Browse the repository at this point in the history
Fixes: #178
  • Loading branch information
synfinatic committed Dec 16, 2021
1 parent 84f4396 commit 86c6b6b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### New Features
* Setup now prompts for `HistoryMinutes` and `HistoryLimit`

### Bug Fixes
* Setup now uses a smaller cursor which doesn't hide the character
* Fix setup bug where the SSO Instance was always called `Default`
* Setup no longer accepts invalid characters for strings #178

## [v1.5.0] - 2021-12-14

### New Features
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PROJECT_VERSION := 1.5.0
PROJECT_VERSION := 1.5.1
DOCKER_REPO := synfinatic
PROJECT_NAME := aws-sso

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,15 @@ for this to take effect.

## Configuration

Anytime you you run `aws-sso` and it does not find it's configuration file, it
will prompt you via a series of questions to do a basic configuration to get
started.

By default, `aws-sso` stores it's configuration file in `~/.aws-sso/config.yaml`,
but this can be overridden by setting `$AWS_SSO_CONFIG` in your shell or via the
`--config` flag.


```yaml
SSOConfig:
<Name of AWS SSO>:
Expand Down
91 changes: 76 additions & 15 deletions cmd/setup_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"net"
"net/url"
"regexp"
"strconv"
"strings"

"github.com/manifoldco/promptui"
Expand Down Expand Up @@ -50,11 +52,13 @@ var AvailableAwsSSORegions []string = []string{

// 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]'"`
SSOStartUrl string `kong:"help='AWS SSO User Portal URL'"`
SSORegion string `kong:"help='AWS SSO Instance Region'"`
Force bool `kong:"help='Force override of existing config file'"`
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]'"`
SSOStartUrl string `kong:"help='AWS SSO User Portal URL'"`
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"`
Force bool `kong:"help='Force override of existing config file'"`
}

// Run executes the setup command
Expand All @@ -64,13 +68,15 @@ func (cc *SetupCmd) Run(ctx *RunContext) error {

func setupWizard(ctx *RunContext) error {
var err error
var instanceName, startURL, ssoRegion, awsRegion, urlAction string
var instanceName, startURL, ssoRegion, awsRegion, urlAction, historyLimit, historyMinutes string
var hLimit, hMinutes int64

// Name our SSO instance
prompt := promptui.Prompt{
Label: "SSO Instance Name",
Label: "SSO Instance Name (DefaultSSO)",
Validate: validateSSOName,
Default: ctx.Cli.SSO,
Pointer: promptui.PipeCursor,
}
if instanceName, err = prompt.Run(); err != nil {
return err
Expand All @@ -81,16 +87,21 @@ func setupWizard(ctx *RunContext) error {
Label: "SSO Start URL (StartUrl)",
Validate: validateSSOUrl,
Default: ctx.Cli.Setup.SSOStartUrl,
Pointer: promptui.PipeCursor,
}
if startURL, err = prompt.Run(); err != nil {
return err
}

// Pick our AWS SSO region
label := "AWS SSO Region (SSORegion)"
sel := promptui.Select{
Label: "AWS SSO Region (SSORegion)",
Label: label,
Items: AvailableAwsSSORegions,
HideSelected: false,
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf(`%s: {{ . | faint }}`, label),
},
}
if _, ssoRegion, err = sel.Run(); err != nil {
return err
Expand All @@ -108,10 +119,14 @@ func setupWizard(ctx *RunContext) error {
}

if len(awsRegion) == 0 {
label = "Default region for connecting to AWS (DefaultRegion)"
sel = promptui.Select{
Label: "Default region for connecting to AWS (DefaultRegion)",
Label: label,
Items: defaultRegions,
HideSelected: false,
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf(`%s: {{ . | faint }}`, label),
},
}
if _, awsRegion, err = sel.Run(); err != nil {
return err
Expand All @@ -131,22 +146,59 @@ func setupWizard(ctx *RunContext) error {

if len(urlAction) == 0 {
// How should we deal with URLs?
label = "Default action to take with URLs (UrlAction)"
sel = promptui.Select{
Label: "Default action to take with URLs (UrlAction)",
Label: label,
Items: []string{"open", "print", "clip"},
Templates: &promptui.SelectTemplates{
Selected: fmt.Sprintf(`%s: {{ . | faint }}`, label),
},
}
if _, urlAction, err = sel.Run(); err != nil {
return err
}
}

// HistoryLimit
if ctx.Cli.Setup.HistoryLimit < 0 {
prompt = promptui.Prompt{
Label: "Maximum number of History items to keep",
Validate: validateInteger,
Default: "10",
Pointer: promptui.PipeCursor,
}
if historyLimit, err = prompt.Run(); err != nil {
return err
}
hLimit, _ = strconv.ParseInt(historyLimit, 10, 64)
} else {
hLimit = ctx.Cli.Setup.HistoryLimit
}

if ctx.Cli.Setup.HistoryMinutes < 0 {
prompt = promptui.Prompt{
Label: "Number of minutes to keep items in History",
Validate: validateInteger,
Default: "1440",
Pointer: promptui.PipeCursor,
}
if historyMinutes, err = prompt.Run(); err != nil {
return err
}
hMinutes, _ = strconv.ParseInt(historyMinutes, 10, 64)
} else {
hMinutes = ctx.Cli.Setup.HistoryMinutes
}

// write config file
s := sso.Settings{
DefaultSSO: instanceName,
SSO: map[string]*sso.SSOConfig{},
UrlAction: urlAction,
DefaultSSO: instanceName,
SSO: map[string]*sso.SSOConfig{},
UrlAction: urlAction,
HistoryLimit: hLimit,
HistoryMinutes: hMinutes,
}
s.SSO[ctx.Cli.SSO] = &sso.SSOConfig{
s.SSO[instanceName] = &sso.SSOConfig{
SSORegion: ssoRegion,
StartUrl: startURL,
DefaultRegion: awsRegion,
Expand Down Expand Up @@ -189,8 +241,17 @@ func validateSSOUrl(input string) error {

// validateSSOName just makes sure we have some text
func validateSSOName(input string) error {
if len(input) > 0 {
r, _ := regexp.Compile(`^[a-zA-Z0-9]+$`)
if len(input) > 0 && r.Match([]byte(input)) {
return nil
}
return fmt.Errorf("SSO Name must be a valid string")
}

func validateInteger(input string) error {
_, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return fmt.Errorf("Value must be a valid integer")
}
return nil
}
2 changes: 1 addition & 1 deletion sso/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *Cache) AddHistory(item string) {
}

c.History = append([]string{item}, c.History...) // push on top
for len(c.History) > c.settings.HistoryLimit {
for int64(len(c.History)) > c.settings.HistoryLimit {
// remove the oldest entry
c.History = c.History[:len(c.History)-1]
}
Expand Down
2 changes: 1 addition & 1 deletion sso/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Settings struct {
PromptColors PromptColors `koanf:"PromptColors" yaml:"PromptColors,omitempty"` // go-prompt colors
LogLevel string `koanf:"LogLevel" yaml:"LogLevel,omitempty"`
LogLines bool `koanf:"LogLines" yaml:"LogLines,omitempty"`
HistoryLimit int `koanf:"HistoryLimit" yaml:"HistoryLimit,omitempty"`
HistoryLimit int64 `koanf:"HistoryLimit" yaml:"HistoryLimit,omitempty"`
HistoryMinutes int64 `koanf:"HistoryMinutes" yaml:"HistoryMinutes,omitempty"`
ListFields []string `koanf:"ListFields" yaml:"ListFields,omitempty"`
}
Expand Down

0 comments on commit 86c6b6b

Please sign in to comment.