mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
parent
b34f8bbbf3
commit
a4ef99acdb
|
@ -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) {
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue