From bd071759b99553a98a80049aa317458c392eadeb Mon Sep 17 00:00:00 2001 From: Pior Bastida Date: Tue, 27 Aug 2019 21:32:06 -0400 Subject: [PATCH] Implement the short syntax for custom command --- dev.yml | 4 +-- pkg/cmd/commands.go | 4 +-- pkg/manifest/manifest.go | 47 ++++++++++++++++++++++++++++++++--- pkg/manifest/manifest_test.go | 12 ++++++++- tests/test_custom.py | 9 +++++++ 5 files changed, 66 insertions(+), 10 deletions(-) diff --git a/dev.yml b/dev.yml index ca4cd712..e8fb0a84 100644 --- a/dev.yml +++ b/dev.yml @@ -61,9 +61,7 @@ commands: desc: Show the commits since the last tag run: git log `git describe --tags --abbrev=0`..HEAD --pretty=format:'%s' - godoc: - desc: Starting GoDoc server on http://0.0.0.0:6060 - run: (sleep 1; open http://0.0.0.0:6060/pkg/github.com/devbuddy/devbuddy/pkg/)& godoc -http=:6060 + godoc: (sleep 1; open http://0.0.0.0:6060/pkg/github.com/devbuddy/devbuddy/pkg/)& godoc -http=:6060 install-dev: desc: Install bud in the GOROOT diff --git a/pkg/cmd/commands.go b/pkg/cmd/commands.go index 8ada90a2..392765cf 100644 --- a/pkg/cmd/commands.go +++ b/pkg/cmd/commands.go @@ -32,7 +32,7 @@ func customCommandRun(cmd *cobra.Command, args []string) error { } name := cmd.Annotations["name"] - spec, ok := man.Commands[name] + spec, ok := man.GetCommands()[name] if !ok { return fmt.Errorf("custom command is not found: %s", name) } @@ -57,7 +57,7 @@ func buildCustomCommands() { var cmd *cobra.Command - for name, spec := range man.Commands { + for name, spec := range man.GetCommands() { desc := "Custom" if spec.Description != "" { desc = fmt.Sprintf("Custom: %s", spec.Description) diff --git a/pkg/manifest/manifest.go b/pkg/manifest/manifest.go index 7711ec96..3224e3c7 100644 --- a/pkg/manifest/manifest.go +++ b/pkg/manifest/manifest.go @@ -14,10 +14,44 @@ var manifestFilename = "dev.yml" // Manifest is a representation of the project manifest type Manifest struct { - Env map[string]string `yaml:"env"` - Up []interface{} `yaml:"up"` - Commands map[string]*Command `yaml:"commands"` - Open map[string]string `yaml:"open"` + Env map[string]string `yaml:"env"` + Up []interface{} `yaml:"up"` + Commands map[string]interface{} `yaml:"commands"` + Open map[string]string `yaml:"open"` +} + +func (m *Manifest) commands() (map[string]*Command, error) { + cmds := map[string]*Command{} + + for name, payload := range m.Commands { + if p, ok := payload.(string); ok { + cmds[name] = &Command{Run: p} + continue + } + + if p, ok := payload.(map[interface{}]interface{}); ok { + if run, ok := p["run"]; ok { + if runS, ok := run.(string); ok { + cmds[name] = &Command{Run: runS} + if desc, ok := p["desc"]; ok { + if descS, ok := desc.(string); ok { + cmds[name].Description = descS + } + } + continue + } + } + } + + return nil, fmt.Errorf("malformed manifest: invalid command %s", name) + } + + return cmds, nil +} + +func (m *Manifest) GetCommands() map[string]*Command { + cmds, _ := m.commands() + return cmds } // Command is a representation of the `command` section of a manifest @@ -49,5 +83,10 @@ func Load(path string) (*Manifest, error) { return nil, err } + _, err = manifest.commands() + if err != nil { + return nil, err + } + return manifest, nil } diff --git a/pkg/manifest/manifest_test.go b/pkg/manifest/manifest_test.go index d734859a..3807c65a 100644 --- a/pkg/manifest/manifest_test.go +++ b/pkg/manifest/manifest_test.go @@ -20,6 +20,9 @@ commands: cmd1: desc: description1 run: command1 + cmd2: + run: command2 + cmd3: command3 open: app: http://localhost:5000 @@ -37,7 +40,14 @@ func TestLoad(t *testing.T) { require.Equal(t, map[string]string{"TESTENV": "TESTVALUE"}, man.Env) require.Equal(t, []interface{}{"task1", "task2"}, man.Up) - require.Equal(t, map[string]*Command{"cmd1": {Run: "command1", Description: "description1"}}, man.Commands) + + commands := map[string]*Command{ + "cmd1": {Run: "command1", Description: "description1"}, + "cmd2": {Run: "command2"}, + "cmd3": {Run: "command3"}, + } + require.Equal(t, commands, man.GetCommands()) + require.Equal(t, map[string]string{"app": "http://localhost:5000"}, man.Open) } diff --git a/tests/test_custom.py b/tests/test_custom.py index da8f4a27..a56b1f80 100644 --- a/tests/test_custom.py +++ b/tests/test_custom.py @@ -9,6 +9,15 @@ def test_command(cmd, project): project.assert_file('somefile') +def test_command_short_syntax(cmd, project): + project.write_devyml(""" + commands: + mycmd: touch somefile + """) + cmd.run("bud mycmd") + project.assert_file('somefile') + + def test_run_in_project_dir(cmd, project): project.write_devyml(""" commands: