mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-03 13:21:56 +02:00
introduce platform specific defaults
This commit is contained in:
parent
ad880e2d56
commit
d31520261f
@ -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}}"`,
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
@ -40,8 +40,7 @@ type AppConfigurer interface {
|
||||
|
||||
// NewAppConfig makes a new app config
|
||||
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag *bool) (*AppConfig, error) {
|
||||
defaultConfig := GetDefaultConfig()
|
||||
userConfig, err := LoadConfig("config", defaultConfig)
|
||||
userConfig, err := LoadConfig("config", true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -113,13 +112,16 @@ func newViper(filename string) (*viper.Viper, error) {
|
||||
}
|
||||
|
||||
// LoadConfig gets the user's config
|
||||
func LoadConfig(filename string, defaults []byte) (*viper.Viper, error) {
|
||||
func LoadConfig(filename string, withDefaults bool) (*viper.Viper, error) {
|
||||
v, err := newViper(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if defaults != nil {
|
||||
if err = LoadDefaults(v, defaults); err != nil {
|
||||
if withDefaults {
|
||||
if err = LoadDefaults(v, GetDefaultConfig()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = LoadDefaults(v, GetPlatformDefaultConfig()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@ -131,7 +133,7 @@ func LoadConfig(filename string, defaults []byte) (*viper.Viper, error) {
|
||||
|
||||
// LoadDefaults loads in the defaults defined in this file
|
||||
func LoadDefaults(v *viper.Viper, defaults []byte) error {
|
||||
return v.ReadConfig(bytes.NewBuffer(defaults))
|
||||
return v.MergeConfig(bytes.NewBuffer(defaults))
|
||||
}
|
||||
|
||||
func prepareConfigFile(filename string) (string, error) {
|
||||
@ -166,7 +168,7 @@ func LoadAndMergeFile(v *viper.Viper, filename string) error {
|
||||
func (c *AppConfig) WriteToUserConfig(key, value string) error {
|
||||
// reloading the user config directly (without defaults) so that we're not
|
||||
// writing any defaults back to the user's config
|
||||
v, err := LoadConfig("config", nil)
|
||||
v, err := LoadConfig("config", false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -222,10 +224,6 @@ update:
|
||||
method: prompt # can be: prompt | background | never
|
||||
days: 14 # how often a update is checked for
|
||||
reporting: 'undetermined' # one of: 'on' | 'off' | 'undetermined'
|
||||
# git:
|
||||
# stuff relating to git
|
||||
# os:
|
||||
# openCommand: 'code -r {{filename}}'
|
||||
`)
|
||||
}
|
||||
|
||||
|
10
pkg/config/config_default_platform.go
Normal file
10
pkg/config/config_default_platform.go
Normal file
@ -0,0 +1,10 @@
|
||||
// +build !windows !linux
|
||||
|
||||
package config
|
||||
|
||||
// GetPlatformDefaultConfig gets the defaults for the platform
|
||||
func GetPlatformDefaultConfig() []byte {
|
||||
return []byte(
|
||||
`os:
|
||||
openCommand: 'open {{filename}}'`)
|
||||
}
|
8
pkg/config/config_linux.go
Normal file
8
pkg/config/config_linux.go
Normal file
@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
// GetPlatformDefaultConfig gets the defaults for the platform
|
||||
func GetPlatformDefaultConfig() []byte {
|
||||
return []byte(
|
||||
`os:
|
||||
openCommand: 'bash -c \"xdg-open {{filename}} &>/dev/null &\"'`)
|
||||
}
|
8
pkg/config/config_windows.go
Normal file
8
pkg/config/config_windows.go
Normal file
@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
// GetPlatformDefaultConfig gets the defaults for the platform
|
||||
func GetPlatformDefaultConfig() []byte {
|
||||
return []byte(
|
||||
`os:
|
||||
openCommand: 'cmd /c "start "" {{filename}}"'`)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user