-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from leandroncbrito/create-command
kool create command
- Loading branch information
Showing
21 changed files
with
419 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"kool-dev/kool/cmd/builder" | ||
"kool-dev/kool/cmd/presets" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// KoolCreate holds handlers and functions to implement the preset command logic | ||
type KoolCreate struct { | ||
DefaultKoolService | ||
parser presets.Parser | ||
createCommand builder.Command | ||
KoolPreset | ||
} | ||
|
||
func init() { | ||
var ( | ||
create = NewKoolCreate() | ||
createCmd = NewCreateCommand(create) | ||
) | ||
|
||
rootCmd.AddCommand(createCmd) | ||
} | ||
|
||
// NewKoolCreate creates a new handler for create logic | ||
func NewKoolCreate() *KoolCreate { | ||
return &KoolCreate{ | ||
*newDefaultKoolService(), | ||
&presets.DefaultParser{}, | ||
&builder.DefaultCommand{}, | ||
*NewKoolPreset(), | ||
} | ||
} | ||
|
||
// Execute runs the create logic with incoming arguments. | ||
func (c *KoolCreate) Execute(originalArgs []string) (err error) { | ||
preset := originalArgs[0] | ||
dir := originalArgs[1] | ||
|
||
c.parser.LoadPresets(presets.GetAll()) | ||
|
||
if !c.parser.Exists(preset) { | ||
err = fmt.Errorf("Unknown preset %s", preset) | ||
return | ||
} | ||
|
||
createCmd, err := c.parser.GetCreateCommand(preset) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
err = c.createCommand.Parse(createCmd) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
err = c.createCommand.Interactive(dir) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
_ = os.Chdir(dir) | ||
|
||
err = c.KoolPreset.Execute([]string{preset}) | ||
|
||
return | ||
} | ||
|
||
// NewCreateCommand initializes new kool create command | ||
func NewCreateCommand(create *KoolCreate) (createCmd *cobra.Command) { | ||
createCmd = &cobra.Command{ | ||
Use: "create [preset] [project]", | ||
Short: "Create a new project using preset", | ||
Args: cobra.ExactArgs(2), | ||
Run: DefaultCommandRunFunction(create), | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"kool-dev/kool/cmd/builder" | ||
"kool-dev/kool/cmd/presets" | ||
"kool-dev/kool/cmd/shell" | ||
"testing" | ||
) | ||
|
||
func newFakeKoolCreate() *KoolCreate { | ||
return &KoolCreate{ | ||
*newFakeKoolService(), | ||
&presets.FakeParser{}, | ||
&builder.FakeCommand{}, | ||
*newFakeKoolPreset(), | ||
} | ||
} | ||
|
||
func TestNewKoolCreate(t *testing.T) { | ||
k := NewKoolCreate() | ||
|
||
if _, ok := k.DefaultKoolService.out.(*shell.DefaultOutputWriter); !ok { | ||
t.Errorf("unexpected shell.OutputWriter on default KoolCreate instance") | ||
} | ||
|
||
if _, ok := k.DefaultKoolService.exiter.(*shell.DefaultExiter); !ok { | ||
t.Errorf("unexpected shell.Exiter on default KoolCreate instance") | ||
} | ||
|
||
if _, ok := k.DefaultKoolService.in.(*shell.DefaultInputReader); !ok { | ||
t.Errorf("unexpected shell.InputReader on default KoolCreate instance") | ||
} | ||
|
||
if _, ok := k.createCommand.(*builder.DefaultCommand); !ok { | ||
t.Errorf("unexpected builder.Command on default KoolCreate instance") | ||
} | ||
|
||
if _, ok := k.parser.(*presets.DefaultParser); !ok { | ||
t.Errorf("unexpected presets.Parser on default KoolCreate instance") | ||
} | ||
} | ||
|
||
func TestNewKoolCreateCommand(t *testing.T) { | ||
f := newFakeKoolCreate() | ||
|
||
f.parser.(*presets.FakeParser).MockExists = true | ||
f.KoolPreset.parser.(*presets.FakeParser).MockExists = true | ||
f.parser.(*presets.FakeParser).MockCreateCommand = "kool docker create command" | ||
|
||
cmd := NewCreateCommand(f) | ||
cmd.SetArgs([]string{"laravel", "my-app"}) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
t.Errorf("unexpected error executing create command; error: %v", err) | ||
} | ||
|
||
if !f.parser.(*presets.FakeParser).CalledLoadPresets { | ||
t.Error("did not call parser.LoadPresets") | ||
} | ||
|
||
if !f.parser.(*presets.FakeParser).CalledExists { | ||
t.Error("did not call parser.Exists") | ||
} | ||
|
||
if !f.parser.(*presets.FakeParser).CalledGetCreateCommand { | ||
t.Error("did not call parser.GetCreateCommand") | ||
} | ||
|
||
if !f.createCommand.(*builder.FakeCommand).CalledParseCommand { | ||
t.Error("did not call Parse on KoolCreate.createCommand Command") | ||
} | ||
|
||
if !f.createCommand.(*builder.FakeCommand).CalledInteractive { | ||
t.Error("did not call Interactive on KoolCreate.createCommand Command") | ||
} | ||
|
||
if !f.out.(*shell.FakeOutputWriter).CalledSetWriter { | ||
t.Error("did not call SetWriter") | ||
} | ||
} | ||
|
||
func TestInvalidPresetCreateCommand(t *testing.T) { | ||
f := newFakeKoolCreate() | ||
cmd := NewCreateCommand(f) | ||
|
||
cmd.SetArgs([]string{"invalid", "my-app"}) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
t.Errorf("unexpected error executing preset command; error: %v", err) | ||
} | ||
|
||
if !f.parser.(*presets.FakeParser).CalledLoadPresets { | ||
t.Error("did not call parser.LoadPresets") | ||
} | ||
|
||
if !f.parser.(*presets.FakeParser).CalledExists { | ||
t.Error("did not call parser.Exists") | ||
} | ||
|
||
if !f.out.(*shell.FakeOutputWriter).CalledError { | ||
t.Error("did not call Error") | ||
} | ||
|
||
expected := "Unknown preset invalid" | ||
output := f.out.(*shell.FakeOutputWriter).Err.Error() | ||
|
||
if expected != output { | ||
t.Errorf("expecting error '%s', got '%s'", expected, output) | ||
} | ||
|
||
if !f.exiter.(*shell.FakeExiter).Exited() { | ||
t.Error("did not call Exit") | ||
} | ||
} | ||
|
||
func TestNoArgsNewCreateCommand(t *testing.T) { | ||
f := newFakeKoolCreate() | ||
|
||
cmd := NewCreateCommand(f) | ||
cmd.SetOut(bytes.NewBufferString("")) | ||
|
||
if err := cmd.Execute(); err == nil { | ||
t.Error("expecting no arguments error executing create command") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.