From 76f89d08ea4d3dbf868e72a2730b1bbafc61ecc4 Mon Sep 17 00:00:00 2001 From: t-kikuc Date: Thu, 11 Jan 2024 17:18:31 +0900 Subject: [PATCH] Refactored nits Signed-off-by: t-kikuc --- hoge | 13 ------- pkg/app/pipectl/cmd/initialize/ecs.go | 2 +- pkg/app/pipectl/cmd/initialize/ecs_test.go | 3 ++ pkg/app/pipectl/cmd/initialize/initialize.go | 39 ++++++++++++-------- pkg/app/pipectl/exporter/exporter.go | 2 +- pkg/app/pipectl/prompt/prompt.go | 10 ++--- pkg/app/pipectl/prompt/prompt_test.go | 8 ++-- 7 files changed, 38 insertions(+), 39 deletions(-) delete mode 100644 hoge diff --git a/hoge b/hoge deleted file mode 100644 index c38981f583..0000000000 --- a/hoge +++ /dev/null @@ -1,13 +0,0 @@ -apiVersion: pipecd.dev/v1beta1 -kind: ECSApp -spec: - name: a - input: - serviceDefinitionFile: b - taskDefinitionFile: c - targetGroups: - primary: - targetGroupArn: d - containerName: e - containerPort: 1 - description: Generated by `pipectl init`. See https://pipecd.dev/docs/user-guide/configuration-reference/ for more. diff --git a/pkg/app/pipectl/cmd/initialize/ecs.go b/pkg/app/pipectl/cmd/initialize/ecs.go index d7e4c75076..b26fa2ace5 100644 --- a/pkg/app/pipectl/cmd/initialize/ecs.go +++ b/pkg/app/pipectl/cmd/initialize/ecs.go @@ -70,7 +70,7 @@ func generateECSConfig(p prompt.Prompt) (*genericConfig, error) { }, } - err := p.Run(inputs) + err := p.RunSlice(inputs) if err != nil { return nil, err } diff --git a/pkg/app/pipectl/cmd/initialize/ecs_test.go b/pkg/app/pipectl/cmd/initialize/ecs_test.go index c110928460..ab34936445 100644 --- a/pkg/app/pipectl/cmd/initialize/ecs_test.go +++ b/pkg/app/pipectl/cmd/initialize/ecs_test.go @@ -27,6 +27,8 @@ import ( ) func TestGenerateECSConfig(t *testing.T) { + t.Parallel() + testcases := []struct { name string inputs string // mock for user's input @@ -56,6 +58,7 @@ func TestGenerateECSConfig(t *testing.T) { } for _, tc := range testcases { + tc := tc t.Run(tc.name, func(t *testing.T) { reader := strings.NewReader(tc.inputs) prompt := prompt.NewPrompt(reader) diff --git a/pkg/app/pipectl/cmd/initialize/initialize.go b/pkg/app/pipectl/cmd/initialize/initialize.go index 02b4a70998..8e7c8a9c91 100644 --- a/pkg/app/pipectl/cmd/initialize/initialize.go +++ b/pkg/app/pipectl/cmd/initialize/initialize.go @@ -34,14 +34,9 @@ type command struct { // Add flags if needed. } -var ( - platform int - exportPath string -) - const ( - platformKubernetes = 0 - platformECS = 1 + platformKubernetes string = "0" + platformECS string = "1" ) // Use genericConfigs in order to simplify using the spec. @@ -90,8 +85,14 @@ func (c *command) run(ctx context.Context, input cli.Input) error { } func generateConfig(ctx context.Context, input cli.Input, p prompt.Prompt) error { + // user's inputs + var ( + platform string + exportPath string + ) + platformInput := prompt.Input{ - Message: fmt.Sprintf("Which platform? Enter the number [%d]Kubernetes [%d]ECS", platformKubernetes, platformECS), + Message: fmt.Sprintf("Which platform? Enter the number [%s]Kubernetes [%s]ECS", platformKubernetes, platformECS), TargetPointer: &platform, Required: true, } @@ -101,7 +102,7 @@ func generateConfig(ctx context.Context, input cli.Input, p prompt.Prompt) error Required: false, } - err := p.Run([]prompt.Input{platformInput}) + err := p.Run(platformInput) if err != nil { return fmt.Errorf("invalid platform number: %v", err) } @@ -109,12 +110,11 @@ func generateConfig(ctx context.Context, input cli.Input, p prompt.Prompt) error var cfg *genericConfig switch platform { case platformKubernetes: - // cfg, err = generateKubernetesConfig(...) panic("not implemented") case platformECS: cfg, err = generateECSConfig(p) default: - return fmt.Errorf("invalid platform number: %d", platform) + return fmt.Errorf("invalid platform number: %s", platform) } if err != nil { @@ -127,21 +127,30 @@ func generateConfig(ctx context.Context, input cli.Input, p prompt.Prompt) error } fmt.Println("### The config model was successfully prepared. Move on to exporting. ###") - err = p.RunOne(exportPathInput) + err = p.Run(exportPathInput) if err != nil { + printConfig(cfgBytes) return err } + err = export(cfgBytes, exportPath) + if err != nil { + return nil + } + + return nil +} + +func export(cfgBytes []byte, exportPath string) error { if len(exportPath) == 0 { + // if the path is not specified, print to stdout printConfig(cfgBytes) return nil } - err = exporter.Export(cfgBytes, exportPath) + err := exporter.Export(cfgBytes, exportPath) if err != nil { - // fmt.Printf("Failed to export to %s: %v\n", exportPath, err) printConfig(cfgBytes) return err } - return nil } diff --git a/pkg/app/pipectl/exporter/exporter.go b/pkg/app/pipectl/exporter/exporter.go index 8ceef9315a..8718e9aa2a 100644 --- a/pkg/app/pipectl/exporter/exporter.go +++ b/pkg/app/pipectl/exporter/exporter.go @@ -50,7 +50,7 @@ func askOverwrite() (overwrite bool, err error) { Required: false, } p := prompt.NewPrompt(os.Stdin) - err = p.RunOne(overwriteInput) + err = p.Run(overwriteInput) if err != nil { return false, err } diff --git a/pkg/app/pipectl/prompt/prompt.go b/pkg/app/pipectl/prompt/prompt.go index e6507626c8..e7ed883f97 100644 --- a/pkg/app/pipectl/prompt/prompt.go +++ b/pkg/app/pipectl/prompt/prompt.go @@ -38,18 +38,18 @@ func NewPrompt(in io.Reader) Prompt { } } -// Run sequentially asks for inputs and set the values to the target pointers. -func (prompt *Prompt) Run(inputs []Input) error { +// RunSlice sequentially asks for inputs and set the values to the target pointers. +func (prompt *Prompt) RunSlice(inputs []Input) error { for _, in := range inputs { - if e := prompt.RunOne(in); e != nil { + if e := prompt.Run(in); e != nil { return e } } return nil } -// RunOne asks for an input and set the value to the target pointer. -func (prompt *Prompt) RunOne(input Input) error { +// Run asks for an input and set the value to the target pointer. +func (prompt *Prompt) Run(input Input) error { fmt.Printf("%s: ", input.Message) line, e := prompt.bufReader.ReadString('\n') if e != nil { diff --git a/pkg/app/pipectl/prompt/prompt_test.go b/pkg/app/pipectl/prompt/prompt_test.go index 10a03245c3..005c42e629 100644 --- a/pkg/app/pipectl/prompt/prompt_test.go +++ b/pkg/app/pipectl/prompt/prompt_test.go @@ -83,7 +83,7 @@ func TestRunString(t *testing.T) { t.Run(tc.name, func(t *testing.T) { strReader := strings.NewReader(tc.str) p := NewPrompt(strReader) - err := p.RunOne(tc.in) + err := p.Run(tc.in) assert.Equal(t, tc.expectedErr, err) assert.Equal(t, tc.expectedValue, *tc.in.TargetPointer.(*string)) }) @@ -140,7 +140,7 @@ func TestRunStringSlice(t *testing.T) { t.Run(tc.name, func(t *testing.T) { strReader := strings.NewReader(tc.str) p := NewPrompt(strReader) - err := p.RunOne(tc.in) + err := p.Run(tc.in) assert.Equal(t, tc.expectedErr, err) assert.Equal(t, tc.expectedValue, *tc.in.TargetPointer.(*[]string)) }) @@ -219,7 +219,7 @@ func TestRunInt(t *testing.T) { t.Run(tc.name, func(t *testing.T) { strReader := strings.NewReader(tc.str) p := NewPrompt(strReader) - err := p.RunOne(tc.in) + err := p.Run(tc.in) assert.Equal(t, tc.expectedErr, err) assert.Equal(t, tc.expectedValue, *tc.in.TargetPointer.(*int)) }) @@ -320,7 +320,7 @@ func TestRunBool(t *testing.T) { t.Run(tc.name, func(t *testing.T) { strReader := strings.NewReader(tc.str) p := NewPrompt(strReader) - err := p.RunOne(tc.in) + err := p.Run(tc.in) assert.Equal(t, tc.expectedErr, err) assert.Equal(t, tc.expectedValue, *tc.in.TargetPointer.(*bool)) })