From a4ef99acdb2978d853a85923deacc9bc29e5b325 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 21 Jul 2022 09:51:35 +0200 Subject: [PATCH] feat(command): run script resolves #2556 --- src/segments/command.go | 26 +++++++++++++++--- src/segments/command_test.go | 44 ++++++++++++++++++++++++++++--- themes/schema.json | 8 +++++- website/docs/segments/command.mdx | 3 ++- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/src/segments/command.go b/src/segments/command.go index 66dc079e..c5ece77d 100644 --- a/src/segments/command.go +++ b/src/segments/command.go @@ -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 { @@ -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 } @@ -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) { diff --git a/src/segments/command_test.go b/src/segments/command_test.go index 16a3eed8..f7d3f7f8 100644 --- a/src/segments/command_test.go +++ b/src/segments/command_test.go @@ -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) { @@ -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)) + } + } +} diff --git a/themes/schema.json b/themes/schema.json index 7a81d6d2..2d4496b2 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -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": "" } } } diff --git a/website/docs/segments/command.mdx b/website/docs/segments/command.mdx index 4fcc2701..698c9933 100644 --- a/website/docs/segments/command.mdx +++ b/website/docs/segments/command.mdx @@ -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]) @@ -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