Skip to content

Commit

Permalink
feat(operator config): Add --yes flag to skip prompt (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntiD2ta authored Mar 28, 2024
1 parent d949f82 commit 5ed7eb3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pkg/operator/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,28 @@ func CreateCmd(p utils.Prompter) *cli.Command {
Both of these are needed for operator registration
`,
Flags: []cli.Flag{
&YesFlag,
},
Action: func(ctx *cli.Context) error {
op := types.OperatorConfigNew{}
skipPrompt := ctx.Bool(YesFlag.Name)

// Prompt user to generate empty or non-empty files
populate, err := p.Confirm("Would you like to populate the operator config file?")
if err != nil {
return err
}
op := types.OperatorConfigNew{}

if populate {
op, err = promptOperatorInfo(&op, p)
if !skipPrompt {
// Prompt user to generate empty or non-empty files
populate, err := p.Confirm("Would you like to populate the operator config file?")
if err != nil {
return err
}
}

if populate {
op, err = promptOperatorInfo(&op, p)
if err != nil {
return err
}
}
}
yamlData, err := yaml.Marshal(&op)
if err != nil {
return err
Expand Down
42 changes: 42 additions & 0 deletions pkg/operator/config/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"context"
"testing"

prompterMock "github.com/Layr-Labs/eigenlayer-cli/pkg/utils/mocks"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
"go.uber.org/mock/gomock"
)

func TestCreateCmd_WithYesFlag(t *testing.T) {
// Arrange
controller := gomock.NewController(t)
prompter := prompterMock.NewMockPrompter(controller)

cmd := CreateCmd(prompter)
app := cli.NewApp()
flags := []string{"--yes"}

// Expect that the prompter will not be called
prompter.EXPECT().Confirm(gomock.Any()).Times(0)

// // We do this because the in the parsing of arguments it ignores the first argument
// // for commands, so we add a blank string as the first argument
// // I suspect it does this because it is expecting the first argument to be the name of the command
// // But when we are testing the command, we don't want to have to specify the name of the command
// // since we are creating the command ourselves
// // https://github.com/urfave/cli/blob/c023d9bc5a3122830c9355a0a8c17137e0c8556f/command.go#L323
args := append([]string{""}, flags...)

cCtx := cli.NewContext(app, nil, &cli.Context{Context: context.Background()})

// Act
err := cmd.Run(cCtx, args...)

// Assert
assert.NoError(t, err)
assert.FileExists(t, "operator.yaml")
assert.FileExists(t, "metadata.json")
}
12 changes: 12 additions & 0 deletions pkg/operator/config/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import "github.com/urfave/cli/v2"

var (
YesFlag = cli.BoolFlag{
Name: "yes",
Aliases: []string{"y"},
Usage: "Use this flag to skip confirmation prompts. When used the operator config file and metadata file will be created with default values.",
EnvVars: []string{"YES"},
}
)

0 comments on commit 5ed7eb3

Please sign in to comment.