Skip to content

Commit

Permalink
Add support for shorthand run expression
Browse files Browse the repository at this point in the history
  • Loading branch information
evilmarty committed May 24, 2022
1 parent cbf142d commit 9f91380
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ one command.

Use `commands.<command_name>` to give your command a unique name. The key
`command_name` is a string and its value is a map of the command's configuration
data.
data. A string value can be used as a shorthand for the `run` attribute.

#### Example

```yaml
commands:
calendar: cal
```
### `commands.<command_name>.description`

Expand Down
25 changes: 16 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,27 @@ func (x *ConfigCommands) UnmarshalYAML(value *yaml.Node) error {
keyNode := content[0]
valueNode := content[1]

if keyNode.Kind != yaml.ScalarNode || valueNode.Kind != yaml.MappingNode {
if keyNode.Kind != yaml.ScalarNode || !(valueNode.Kind == yaml.MappingNode || valueNode.Kind == yaml.ScalarNode) {
return fmt.Errorf("line %d: unexpected node type", keyNode.Line)
}

var command ConfigCommand
if err := valueNode.Decode(&command); err != nil {
return err
}
if numCommands := len(command.Commands); command.Run == "" && numCommands == 0 {
return fmt.Errorf("line %d: '%s' command missing run or commands attribute", keyNode.Line, keyNode.Value)
} else if command.Run != "" && numCommands > 0 {
return fmt.Errorf("line %d: '%s' command cannot have both run and commands attribute", keyNode.Line, keyNode.Value)
if valueNode.Kind == yaml.ScalarNode {
command = ConfigCommand{
Name: keyNode.Value,
Run: valueNode.Value,
}
} else {
if err := valueNode.Decode(&command); err != nil {
return err
}
if numCommands := len(command.Commands); command.Run == "" && numCommands == 0 {
return fmt.Errorf("line %d: '%s' command missing run or commands attribute", keyNode.Line, keyNode.Value)
} else if command.Run != "" && numCommands > 0 {
return fmt.Errorf("line %d: '%s' command cannot have both run and commands attribute", keyNode.Line, keyNode.Value)
}
command.Name = keyNode.Value
}
command.Name = keyNode.Value
commands = append(commands, command)

content = content[2:]
Expand Down
27 changes: 27 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,33 @@ punish:
}
}

func TestConfigCommandsUnmarshalYAML_Shorthand(t *testing.T) {
var actual ConfigCommands
content := `
protect: echo Protect all sentient life forms
punish: echo Punish and enslave
`
expected := ConfigCommands{
ConfigCommand{
Name: "protect",
Run: "echo Protect all sentient life forms",
},
ConfigCommand{
Name: "punish",
Run: "echo Punish and enslave",
},
}
err := yaml.Unmarshal([]byte(content), &actual)

if err != nil {
t.Errorf("Received error from parser: %s", err)
}

if !reflect.DeepEqual(actual, expected) {
fatalDiff(t, expected, actual)
}
}

func TestConfigCommandsGet(t *testing.T) {
commands := ConfigCommands{
ConfigCommand{
Expand Down

0 comments on commit 9f91380

Please sign in to comment.