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

Tweak environment flags #226

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 21 additions & 15 deletions cmd/meroxa/root/environments/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package environments

import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/manifoldco/promptui"
"github.com/meroxa/cli/cmd/meroxa/builder"
Expand Down Expand Up @@ -51,10 +51,10 @@ type Create struct {
}

flags struct {
Type string `long:"type" usage:"environment type, when not specified"`
Provider string `long:"provider" usage:"environment cloud provider to use"`
Region string `long:"region" usage:"environment region"`
Config string `short:"c" long:"config" usage:"environment configuration based on type and provider (e.g.: --config '{\"aws_access_key_id\":\"my_access_key\", \"aws_secret_access_key\":\"my_secret_access_key\"}')"` // nolint:lll
Type string `long:"type" usage:"environment type, when not specified"`
Provider string `long:"provider" usage:"environment cloud provider to use"`
Region string `long:"region" usage:"environment region"`
Config []string `short:"c" long:"config" usage:"environment configuration based on type and provider (e.g.: --config aws_access_key_id=my_access_key --config aws_access_secret=my_access_secret)"` // nolint:lll
}

envCfg map[string]interface{}
Expand All @@ -80,7 +80,7 @@ func (c *Create) ParseArgs(args []string) error {
}

func (c *Create) SkipPrompt() bool {
return c.args.Name != "" && c.flags.Type != "" && c.flags.Provider != "" && c.flags.Region != "" && c.flags.Config != ""
return c.args.Name != "" && c.flags.Type != "" && c.flags.Provider != "" && c.flags.Region != "" && len(c.flags.Config) != 0
}

func (c *Create) setUserValues(e *meroxa.CreateEnvironmentInput) {
Expand All @@ -105,19 +105,25 @@ func (c *Create) setUserValues(e *meroxa.CreateEnvironmentInput) {
}
}

func stringSliceToMap(input []string) map[string]interface{} {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be extracted in some point as part of builder, once we change how config flags are used in other commands, but I think it's fine we leave it here for now.

const pair = 2
m := make(map[string]interface{})
for _, config := range input {
parts := strings.Split(config, "=")
if len(parts) >= pair {
m[parts[0]] = parts[1]
}
}
return m
}

func (c *Create) Execute(ctx context.Context) error {
e := &meroxa.CreateEnvironmentInput{}
c.setUserValues(e)

// In case user skipped prompt and configuration was specified via flags
if c.flags.Config != "" {
var config map[string]interface{}
err := json.Unmarshal([]byte(c.flags.Config), &config)
if err != nil {
return fmt.Errorf("could not parse configuration: %w", err)
}

e.Configuration = config
if len(c.flags.Config) != 0 {
e.Configuration = stringSliceToMap(c.flags.Config)
}

c.logger.Infof(ctx, "Provisioning environment...")
Expand Down Expand Up @@ -208,7 +214,7 @@ func (c *Create) Prompt() error {
c.flags.Region, _ = p.Run()
}

if c.flags.Config == "" {
if len(c.flags.Config) != 0 {
c.envCfg = make(map[string]interface{})

p := promptui.Prompt{
Expand Down
11 changes: 3 additions & 8 deletions cmd/meroxa/root/environments/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,9 @@ func TestCreateEnvironmentExecution(t *testing.T) {
c.flags.Type = "dedicated"
c.flags.Provider = "aws"
c.flags.Region = "aws"
c.flags.Config = `{"aws_access_key_id":"my_access_key", "aws_access_secret":"my_access_secret"}`
c.flags.Config = []string{"aws_access_key_id=my_access_key", "aws_access_secret=my_access_secret"}

cfg := map[string]interface{}{}
err := json.Unmarshal([]byte(c.flags.Config), &cfg)

if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}
cfg := stringSliceToMap(c.flags.Config)

e := &meroxa.CreateEnvironmentInput{
Type: meroxa.EnvironmentType(c.flags.Type),
Expand Down Expand Up @@ -150,7 +145,7 @@ func TestCreateEnvironmentExecution(t *testing.T) {
).
Return(rE, nil)

err = c.Execute(ctx)
err := c.Execute(ctx)

if err != nil {
t.Fatalf("not expected error, got \"%s\"", err.Error())
Expand Down