Skip to content

Commit

Permalink
feat(command): run script
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jul 21, 2022
1 parent b34f8bb commit a4ef99a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
26 changes: 22 additions & 4 deletions src/segments/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
ExecutableShell properties.Property = "shell"
// Command to execute
Command properties.Property = "command"
// Command to execute
Script properties.Property = "script"
)

func (c *Cmd) Template() string {
Expand All @@ -29,12 +31,23 @@ func (c *Cmd) Enabled() bool {
if !c.env.HasCommand(shell) {
return false
}
command := c.props.GetString(Command, "echo no command specified")
command := c.props.GetString(Command, "")
if len(command) != 0 {
return c.runCommand(shell, command)
}
script := c.props.GetString(Script, "")
if len(script) != 0 {
return c.runScript(shell, script)
}
return false
}

func (c *Cmd) runCommand(shell, command string) bool {
if strings.Contains(command, "||") {
commands := strings.Split(command, "||")
for _, cmd := range commands {
output := c.env.RunShellCommand(shell, strings.TrimSpace(cmd))
if output != "" {
if len(output) != 0 {
c.Output = output
return true
}
Expand All @@ -47,10 +60,15 @@ func (c *Cmd) Enabled() bool {
output += c.env.RunShellCommand(shell, strings.TrimSpace(cmd))
}
c.Output = output
return c.Output != ""
return len(c.Output) != 0
}
c.Output = c.env.RunShellCommand(shell, strings.TrimSpace(command))
return c.Output != ""
return len(c.Output) != 0
}

func (c *Cmd) runScript(shell, script string) bool {
c.Output = c.env.RunShellCommand(shell, script)
return len(c.Output) != 0
}

func (c *Cmd) Init(props properties.Properties, env environment.Environment) {
Expand Down
44 changes: 41 additions & 3 deletions src/segments/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ func TestExecuteSingleCommandEmpty(t *testing.T) {
func TestExecuteSingleCommandNoCommandProperty(t *testing.T) {
env := new(mock.MockedEnvironment)
env.On("HasCommand", "bash").Return(true)
env.On("RunShellCommand", "bash", "echo no command specified").Return("no command specified")
env.On("RunShellCommand", "bash", "").Return("")
var props properties.Map
c := &Cmd{
props: props,
env: env,
}
enabled := c.Enabled()
assert.True(t, enabled)
assert.Equal(t, "no command specified", c.Output)
assert.False(t, enabled)
}

func TestExecuteMultipleCommandsAndDisabled(t *testing.T) {
Expand Down Expand Up @@ -135,3 +134,42 @@ func TestExecuteMultipleCommandsOrDisabled(t *testing.T) {
enabled := c.Enabled()
assert.False(t, enabled)
}

func TestExecuteScript(t *testing.T) {
cases := []struct {
Case string
Output string
HasScript bool
ExpectedString string
ExpectedEnabled bool
}{
{
Case: "Output",
Output: "Hello World",
ExpectedString: "Hello World",
ExpectedEnabled: true,
},
{
Case: "No output",
ExpectedEnabled: false,
},
}
for _, tc := range cases {
script := "../test/script.sh"
env := new(mock.MockedEnvironment)
env.On("HasCommand", "bash").Return(true)
env.On("RunShellCommand", "bash", script).Return(tc.Output)
props := properties.Map{
Script: script,
}
c := &Cmd{
props: props,
env: env,
}
enabled := c.Enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, c.Template(), c))
}
}
}
8 changes: 7 additions & 1 deletion themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,13 @@
"type": "string",
"title": "Command",
"description": "the command(s) to run",
"default": "echo no command specified"
"default": ""
},
"script": {
"type": "string",
"title": "Script",
"description": "A script to run",
"default": ""
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion website/docs/segments/command.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ error). The `&&` functionality will join the output of the commands when success

- shell: `string` - the shell in which to run the command in. Uses `shell -c command` under the hood.
- command: `string` - the command(s) to run
- script: `string` - the path to a script to run

## Template ([info][templates])

Expand All @@ -57,7 +58,7 @@ error). The `&&` functionality will join the output of the commands when success

### Properties

- `.Output`: `string` - the output of the command.
- `.Output`: `string` - the output of the command or script.

[env]: /docs/configuration/templates#environment-variables
[templates]: /docs/configuration/templates

0 comments on commit a4ef99a

Please sign in to comment.