From 6c44459b6f1b3c5d72ae56e048a27acdf468a575 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 20 Apr 2024 09:35:11 +0200 Subject: [PATCH] feat(command): allow bypassing custom logic resolves #4835 --- src/segments/command.go | 14 ++++++++ src/segments/command_test.go | 17 ++++++++++ website/docs/segments/command.mdx | 54 +++++++++++++++++-------------- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/segments/command.go b/src/segments/command.go index 9bd3c7e9..bb6f2782 100644 --- a/src/segments/command.go +++ b/src/segments/command.go @@ -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 } diff --git a/src/segments/command_test.go b/src/segments/command_test.go index 66f75a0a..c2cb0d4a 100644 --- a/src/segments/command_test.go +++ b/src/segments/command_test.go @@ -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 diff --git a/website/docs/segments/command.mdx b/website/docs/segments/command.mdx index 2f149e8d..548156ad 100644 --- a/website/docs/segments/command.mdx +++ b/website/docs/segments/command.mdx @@ -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"; - + ## 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