mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
introduce platform specific defaults
This commit is contained in:
@ -51,7 +51,6 @@ func (c *OSCommand) RunCommandWithOutput(command string) (string, error) {
|
||||
c.Log.WithField("command", command).Info("RunCommand")
|
||||
splitCmd := str.ToArgv(command)
|
||||
c.Log.Info(splitCmd)
|
||||
|
||||
return sanitisedCommandOutput(
|
||||
c.command(splitCmd[0], splitCmd[1:]...).CombinedOutput(),
|
||||
)
|
||||
@ -98,23 +97,15 @@ func sanitisedCommandOutput(output []byte, err error) (string, error) {
|
||||
return outputString, nil
|
||||
}
|
||||
|
||||
// getOpenCommand get open command
|
||||
func (c *OSCommand) getOpenCommand() string {
|
||||
if c.Config.GetUserConfig().IsSet("os.openCommand") {
|
||||
return c.Config.GetUserConfig().GetString("os.openCommand")
|
||||
}
|
||||
return c.Platform.openCommand
|
||||
}
|
||||
|
||||
// OpenFile opens a file with the given
|
||||
func (c *OSCommand) OpenFile(filename string) error {
|
||||
commandTemplate := c.getOpenCommand()
|
||||
commandTemplate := c.Config.GetUserConfig().GetString("os.openCommand")
|
||||
templateValues := map[string]string{
|
||||
"filename": c.Quote(filename),
|
||||
}
|
||||
|
||||
command := utils.ResolvePlaceholderString(commandTemplate, templateValues)
|
||||
_, err := c.RunCommandWithOutput(command)
|
||||
err := c.RunCommand(command)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -1,15 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func getPlatform() *Platform {
|
||||
return &Platform{
|
||||
os: runtime.GOOS,
|
||||
shell: "bash",
|
||||
shellArg: "-c",
|
||||
escapedQuote: "\"",
|
||||
openCommand: "bash -c \"xdg-open {{filename}} &>/dev/null &\"",
|
||||
}
|
||||
}
|
@ -76,48 +76,6 @@ func TestOSCommandRunCommand(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOSCommandGetOpenCommand(t *testing.T) {
|
||||
// two scenarios to test. One with a config set, the other with the platform default
|
||||
|
||||
type scenario struct {
|
||||
before func(*OSCommand)
|
||||
test func(string)
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
{
|
||||
func(OSCmd *OSCommand) {},
|
||||
func(command string) {
|
||||
assert.Equal(t, command, "open {{filename}}")
|
||||
},
|
||||
},
|
||||
{
|
||||
func(OSCmd *OSCommand) {
|
||||
OSCmd.Config.GetUserConfig().Set("os.openCommand", "test {{filename}}")
|
||||
},
|
||||
func(command string) {
|
||||
assert.Equal(t, command, "test {{filename}}")
|
||||
},
|
||||
},
|
||||
{
|
||||
func(OSCmd *OSCommand) {
|
||||
OSCmd.Platform = &Platform{
|
||||
openCommand: "platform specific open {{filename}}",
|
||||
}
|
||||
},
|
||||
func(command string) {
|
||||
assert.Equal(t, command, "platform specific open {{filename}}")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
OSCmd := newDummyOSCommand()
|
||||
s.before(OSCmd)
|
||||
s.test(OSCmd.getOpenCommand())
|
||||
}
|
||||
}
|
||||
|
||||
func TestOSCommandOpenFile(t *testing.T) {
|
||||
type scenario struct {
|
||||
filename string
|
||||
@ -138,8 +96,19 @@ func TestOSCommandOpenFile(t *testing.T) {
|
||||
{
|
||||
"test",
|
||||
func(name string, arg ...string) *exec.Cmd {
|
||||
assert.Equal(t, name, "bash")
|
||||
assert.Equal(t, arg, []string{"-c", "open \"test\""})
|
||||
assert.Equal(t, "open", name)
|
||||
assert.Equal(t, []string{"test"}, arg)
|
||||
return exec.Command("echo")
|
||||
},
|
||||
func(err error) {
|
||||
assert.NoError(t, err)
|
||||
},
|
||||
},
|
||||
{
|
||||
"filename with spaces",
|
||||
func(name string, arg ...string) *exec.Cmd {
|
||||
assert.Equal(t, "open", name)
|
||||
assert.Equal(t, []string{"filename with spaces"}, arg)
|
||||
return exec.Command("echo")
|
||||
},
|
||||
func(err error) {
|
||||
@ -151,6 +120,7 @@ func TestOSCommandOpenFile(t *testing.T) {
|
||||
for _, s := range scenarios {
|
||||
OSCmd := newDummyOSCommand()
|
||||
OSCmd.command = s.command
|
||||
OSCmd.Config.GetUserConfig().Set("os.openCommand", "open {{filename}}")
|
||||
|
||||
s.test(OSCmd.OpenFile(s.filename))
|
||||
}
|
||||
|
@ -6,5 +6,5 @@ func getPlatform() *Platform {
|
||||
shell: "cmd",
|
||||
shellArg: "/c",
|
||||
escapedQuote: `\"`,
|
||||
openCommand: `cmd /c "start "" {{filename}}"`,
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user