Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): configure in non-interactive mode #158

Merged
merged 3 commits into from
Jul 6, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions cli/cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ type credsDetails struct {
ApiSecret string `toml:"api_secret" json:"api_secret" survey:"api_secret"`
}

func (c *credsDetails) Verify() error {
if c.Account == "" {
return errors.New("account missing")
}
if c.ApiKey == "" {
return errors.New("api_key missing")
}
if c.ApiSecret == "" {
return errors.New("api_secret missing")
}
return nil
}

// apiKeyDetails represents the details of an API key, we use this struct
// internally to unmarshal the JSON file provided by the Lacework WebUI
type apiKeyDetails struct {
Expand Down Expand Up @@ -106,6 +119,9 @@ func init() {
func promptConfigureSetup() error {
cli.Log.Debugw("configuring cli", "profile", cli.Profile)

// make sure that the state is loaded to use during configuration
cli.loadStateFromViper()

// if the Lacework account is empty, and the profile that is being configured is
// not the 'default' profile, auto-populate the account with the provided profile
if cli.Account == "" && cli.Profile != "default" {
Expand Down Expand Up @@ -167,17 +183,28 @@ func promptConfigureSetup() error {
}

newCreds := credsDetails{}
err := survey.Ask(append(questions, secretQuest), &newCreds,
survey.WithIcons(promptIconsFunc),
)
if err != nil {
return err
}
if cli.InteractiveMode() {
err := survey.Ask(append(questions, secretQuest), &newCreds,
survey.WithIcons(promptIconsFunc),
)
if err != nil {
return err
}

if len(newCreds.ApiSecret) == 0 {
if len(newCreds.ApiSecret) == 0 {
newCreds.ApiSecret = cli.Secret
}
cli.OutputHuman("\n")
} else {
newCreds.Account = cli.Account
newCreds.ApiKey = cli.KeyID
newCreds.ApiSecret = cli.Secret
}

if err := newCreds.Verify(); err != nil {
return errors.Wrap(err, "unable to configure the command-line")
}

var (
profiles = Profiles{}
buf = new(bytes.Buffer)
Expand Down Expand Up @@ -217,12 +244,12 @@ func promptConfigureSetup() error {
return err
}

err = ioutil.WriteFile(confPath, buf.Bytes(), 0600)
err := ioutil.WriteFile(confPath, buf.Bytes(), 0600)
if err != nil {
return err
}

cli.OutputHuman("\nYou are all set!\n")
cli.OutputHuman("You are all set!\n")
return nil
}

Expand Down