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: add env create #201

Merged
merged 13 commits into from
Oct 22, 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
4 changes: 1 addition & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ linters-settings:
- hugeParam
gocyclo:
min-complexity: 15
golint:
min-confidence: 0
gomnd:
settings:
mnd:
Expand Down Expand Up @@ -69,7 +67,6 @@ linters:
- godot
- gofmt
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
Expand All @@ -81,6 +78,7 @@ linters:
- nakedret
- noctx
- nolintlint
- revive
- rowserrcheck
- staticcheck
- structcheck
Expand Down
71 changes: 60 additions & 11 deletions cmd/meroxa/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
"time"

"github.com/cased/cased-go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/meroxa/cli/cmd/meroxa/global"
"github.com/meroxa/cli/config"
"github.com/meroxa/cli/log"
"github.com/meroxa/meroxa-go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

type Command interface {
Expand Down Expand Up @@ -77,11 +77,23 @@ type CommandWithNoHeaders interface {
HideHeaders(hide bool)
}

type CommandWithConfirm interface {
type CommandWithConfirmWithValue interface {
Command
// Confirm adds a prompt before the command is executed where the user is asked to write the exact value as
// ValueToConfirm adds a prompt before the command is executed where the user is asked to write the exact value as
// wantInput. If the user input matches the command will be executed, otherwise processing will be stopped.
Confirm(ctx context.Context) (wantInput string)
ValueToConfirm(ctx context.Context) (wantInput string)
}

type CommandWithPrompt interface {
Command
// Prompt adds a prompt before the command is executed where the user is asked to answer y/N to proceed
Prompt() error

// SkipPrompt will return logic around when to skip prompt (e.g.: when all flags and arguments are specified)
SkipPrompt() bool

// NotConfirmed indicates what to show in case user declines the answer
NotConfirmed() (prompt string)
}

type CommandWithDocs interface {
Expand Down Expand Up @@ -179,9 +191,10 @@ func BuildCobraCommand(c Command) *cobra.Command {
buildCommandWithClient(cmd, c)
buildCommandWithConfig(cmd, c)

// buildCommandWithConfirm needs to go before buildCommandWithExecute to make sure there's a confirmation prompt
// buildCommandWithConfirmWithValue needs to go before buildCommandWithExecute to make sure there's a confirmation prompt
// prior to execution.
buildCommandWithConfirm(cmd, c)
buildCommandWithConfirmWithValue(cmd, c)
buildCommandWithConfirmWithoutValue(cmd, c)
buildCommandWithFeatureFlag(cmd, c)
buildCommandWithExecute(cmd, c)

Expand Down Expand Up @@ -319,8 +332,8 @@ func buildCommandWithNoHeaders(cmd *cobra.Command, c Command) {
}
}

func buildCommandWithConfirm(cmd *cobra.Command, c Command) {
v, ok := c.(CommandWithConfirm)
func buildCommandWithConfirmWithValue(cmd *cobra.Command, c Command) {
v, ok := c.(CommandWithConfirmWithValue)
if !ok {
return
}
Expand Down Expand Up @@ -350,7 +363,7 @@ func buildCommandWithConfirm(cmd *cobra.Command, c Command) {
return nil
}

wantInput := v.Confirm(cmd.Context())
wantInput := v.ValueToConfirm(cmd.Context())

reader := bufio.NewReader(os.Stdin)
fmt.Printf("To proceed, type %q or re-run this command with --force\n▸ ", wantInput)
Expand All @@ -367,6 +380,42 @@ func buildCommandWithConfirm(cmd *cobra.Command, c Command) {
}
}

func buildCommandWithConfirmWithoutValue(cmd *cobra.Command, c Command) {
v, ok := c.(CommandWithPrompt)
if !ok {
return
}

var (
skip bool
)
cmd.Flags().BoolVarP(&skip, "yes", "y", false, "skip confirmation prompt")

old := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if old != nil {
err := old(cmd, args)
if err != nil {
return err
}
}

// do not prompt for confirmation when --yes or when we explicitly want to skip prompt
if skip || v.SkipPrompt() {
return nil
}

e := v.Prompt()

if e != nil {
fmt.Println(v.NotConfirmed())
os.Exit(1)
}

return nil
}
}

func buildCommandWithDocs(cmd *cobra.Command, c Command) {
v, ok := c.(CommandWithDocs)
if !ok {
Expand Down
16 changes: 8 additions & 8 deletions cmd/meroxa/root/connectors/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *Remove) Docs() builder.Docs {
}
}

func (r *Remove) Confirm(_ context.Context) (wantInput string) {
func (r *Remove) ValueToConfirm(_ context.Context) (wantInput string) {
return r.args.Name
}

Expand Down Expand Up @@ -97,11 +97,11 @@ func (r *Remove) Aliases() []string {
}

var (
_ builder.CommandWithDocs = (*Remove)(nil)
_ builder.CommandWithAliases = (*Remove)(nil)
_ builder.CommandWithArgs = (*Remove)(nil)
_ builder.CommandWithClient = (*Remove)(nil)
_ builder.CommandWithLogger = (*Remove)(nil)
_ builder.CommandWithExecute = (*Remove)(nil)
_ builder.CommandWithConfirm = (*Remove)(nil)
_ builder.CommandWithDocs = (*Remove)(nil)
_ builder.CommandWithAliases = (*Remove)(nil)
_ builder.CommandWithArgs = (*Remove)(nil)
_ builder.CommandWithClient = (*Remove)(nil)
_ builder.CommandWithLogger = (*Remove)(nil)
_ builder.CommandWithExecute = (*Remove)(nil)
_ builder.CommandWithConfirmWithValue = (*Remove)(nil)
)
16 changes: 8 additions & 8 deletions cmd/meroxa/root/endpoints/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (r *Remove) Docs() builder.Docs {
}
}

func (r *Remove) Confirm(_ context.Context) (wantInput string) {
func (r *Remove) ValueToConfirm(_ context.Context) (wantInput string) {
return r.args.Name
}

Expand Down Expand Up @@ -89,11 +89,11 @@ func (r *Remove) Aliases() []string {
}

var (
_ builder.CommandWithDocs = (*Remove)(nil)
_ builder.CommandWithAliases = (*Remove)(nil)
_ builder.CommandWithArgs = (*Remove)(nil)
_ builder.CommandWithClient = (*Remove)(nil)
_ builder.CommandWithLogger = (*Remove)(nil)
_ builder.CommandWithExecute = (*Remove)(nil)
_ builder.CommandWithConfirm = (*Remove)(nil)
_ builder.CommandWithDocs = (*Remove)(nil)
_ builder.CommandWithAliases = (*Remove)(nil)
_ builder.CommandWithArgs = (*Remove)(nil)
_ builder.CommandWithClient = (*Remove)(nil)
_ builder.CommandWithLogger = (*Remove)(nil)
_ builder.CommandWithExecute = (*Remove)(nil)
_ builder.CommandWithConfirmWithValue = (*Remove)(nil)
)
Loading