Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the short syntax for custom command #339

Merged
merged 1 commit into from Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions dev.yml
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/commands.go
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
47 changes: 43 additions & 4 deletions pkg/manifest/manifest.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion pkg/manifest/manifest_test.go
Expand Up @@ -20,6 +20,9 @@ commands:
cmd1:
desc: description1
run: command1
cmd2:
run: command2
cmd3: command3

open:
app: http://localhost:5000
Expand All @@ -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)
}

Expand Down
9 changes: 9 additions & 0 deletions tests/test_custom.py
Expand Up @@ -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:
Expand Down