Skip to content

Commit

Permalink
Refactored nits
Browse files Browse the repository at this point in the history
Signed-off-by: t-kikuc <[email protected]>
  • Loading branch information
t-kikuc committed Jan 11, 2024
1 parent 77c8c92 commit 76f89d0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 39 deletions.
13 changes: 0 additions & 13 deletions hoge

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/app/pipectl/cmd/initialize/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/app/pipectl/cmd/initialize/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
)

func TestGenerateECSConfig(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
inputs string // mock for user's input
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 24 additions & 15 deletions pkg/app/pipectl/cmd/initialize/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
Expand All @@ -101,20 +102,19 @@ 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)
}

Check warning on line 108 in pkg/app/pipectl/cmd/initialize/initialize.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/initialize/initialize.go#L87-L108

Added lines #L87 - L108 were not covered by tests

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)

Check warning on line 117 in pkg/app/pipectl/cmd/initialize/initialize.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/initialize/initialize.go#L110-L117

Added lines #L110 - L117 were not covered by tests
}

if err != nil {
Expand All @@ -127,21 +127,30 @@ func generateConfig(ctx context.Context, input cli.Input, p prompt.Prompt) error
}

Check warning on line 127 in pkg/app/pipectl/cmd/initialize/initialize.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/initialize/initialize.go#L124-L127

Added lines #L124 - L127 were not covered by tests

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
}

Check warning on line 138 in pkg/app/pipectl/cmd/initialize/initialize.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/initialize/initialize.go#L129-L138

Added lines #L129 - L138 were not covered by tests

return nil

Check warning on line 140 in pkg/app/pipectl/cmd/initialize/initialize.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/initialize/initialize.go#L140

Added line #L140 was not covered by tests
}

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

Check warning on line 154 in pkg/app/pipectl/cmd/initialize/initialize.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/cmd/initialize/initialize.go#L143-L154

Added lines #L143 - L154 were not covered by tests
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/app/pipectl/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/app/pipectl/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Check warning on line 46 in pkg/app/pipectl/prompt/prompt.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/prompt/prompt.go#L42-L46

Added lines #L42 - L46 were not covered by tests
}
return nil

Check warning on line 48 in pkg/app/pipectl/prompt/prompt.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipectl/prompt/prompt.go#L48

Added line #L48 was not covered by tests
}

// 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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/pipectl/prompt/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down Expand Up @@ -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))
})
Expand Down Expand Up @@ -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))
})
Expand Down Expand Up @@ -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))
})
Expand Down

0 comments on commit 76f89d0

Please sign in to comment.