feat(command): allow bypassing custom logic

resolves #4835
This commit is contained in:
Jan De Dobbeleer 2024-04-20 09:35:11 +02:00 committed by Jan De Dobbeleer
parent 9d1b9e2b7c
commit 6c44459b6f
3 changed files with 60 additions and 25 deletions

View file

@ -21,6 +21,8 @@ const (
Command properties.Property = "command"
// Command to execute
Script properties.Property = "script"
// Interpret execution, or not
Interpret properties.Property = "interpret"
)
func (c *Cmd) Template() string {
@ -32,18 +34,28 @@ func (c *Cmd) Enabled() bool {
if !c.env.HasCommand(shell) {
return false
}
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 {
interpret := c.props.GetBool(Interpret, true)
if !interpret {
c.Output = c.env.RunShellCommand(shell, command)
return len(c.Output) != 0
}
if strings.Contains(command, "||") {
commands := strings.Split(command, "||")
for _, cmd := range commands {
@ -54,6 +66,7 @@ func (c *Cmd) runCommand(shell, command string) bool {
}
}
}
if strings.Contains(command, "&&") {
var output string
commands := strings.Split(command, "&&")
@ -63,6 +76,7 @@ func (c *Cmd) runCommand(shell, command string) bool {
c.Output = output
return len(c.Output) != 0
}
c.Output = c.env.RunShellCommand(shell, strings.TrimSpace(command))
return len(c.Output) != 0
}

View file

@ -136,6 +136,23 @@ func TestExecuteMultipleCommandsOrDisabled(t *testing.T) {
assert.False(t, enabled)
}
func TestExecuteNonInterpretedCommand(t *testing.T) {
env := new(mock.MockedEnvironment)
env.On("HasCommand", "bash").Return(true)
env.On("RunShellCommand", "bash", "echo hello && echo world").Return("hello world")
props := properties.Map{
Command: "echo hello && echo world",
Interpret: false,
}
c := &Cmd{
props: props,
env: env,
}
enabled := c.Enabled()
assert.True(t, enabled)
assert.Equal(t, "hello world", renderTemplate(env, c.Template(), c))
}
func TestExecuteScript(t *testing.T) {
cases := []struct {
Case string

View file

@ -18,35 +18,39 @@ When the command errors or returns an empty string, this segment isn't rendered.
You have the ability to use `||` or `&&` to stitch commands together and achieve complex results. When using `||`
the first command that returns a string will be used (or none when they all fail to produce output that's not an
error). The `&&` functionality will join the output of the commands when successful.
error). The `&&` functionality will join the output of the commands when successful. If you want to run the command
as is, you can set `interpret` to `false`.
## Sample Configuration
import Config from '@site/src/components/Config.js';
import Config from "@site/src/components/Config.js";
<Config data={{
"type": "prompt",
"alignment": "right",
"segments": [
{
"type": "command",
"style": "plain",
"foreground": "#ffffff",
"properties": {
"shell": "bash",
"command": "git log --pretty=format:%cr -1 || date +%H:%M:%S"
}
}
]
}}/>
<Config
data={{
type: "prompt",
alignment: "right",
segments: [
{
type: "command",
style: "plain",
foreground: "#ffffff",
properties: {
shell: "bash",
command: "git log --pretty=format:%cr -1 || date +%H:%M:%S",
},
},
],
}}
/>
## Properties
| Name | Type | Default | Description |
| --------- | :------: | :-----: | --------------------------------------------------------------------------------- |
| `shell` | `string` | `bash` | 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 |
| Name | Type | Default | Description |
| ----------- | :-------: | :-----: | -------------------------------------------------------------------------------- |
| `shell` | `string` | `bash` | the shell in which to run the command in. Uses `shell -c command` under the hood |
| `interpret` | `boolean` | `true` | interpret the command or run as is |
| `command` | `string` | | the command(s) to run |
| `script` | `string` | | the path to a script to run |
## Template ([info][templates])
@ -60,9 +64,9 @@ import Config from '@site/src/components/Config.js';
### Properties
| Name | Type | Description |
| ------- | ------ | ---------------------------------- |
|`.Output`|`string`|the output of the command or script.|
| Name | Type | Description |
| --------- | -------- | ------------------------------------ |
| `.Output` | `string` | the output of the command or script. |
[env]: /docs/configuration/templates#environment-variables
[templates]: /docs/configuration/templates