From d31520261f0ffc4cb93e29476e39ace45e813fc8 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 1 Sep 2018 14:33:01 +1000 Subject: [PATCH] introduce platform specific defaults --- pkg/commands/os.go | 13 +----- pkg/commands/os_linux.go | 15 ------- pkg/commands/os_test.go | 58 +++++++-------------------- pkg/commands/os_windows.go | 4 +- pkg/config/app_config.go | 20 +++++---- pkg/config/config_default_platform.go | 10 +++++ pkg/config/config_linux.go | 8 ++++ pkg/config/config_windows.go | 8 ++++ 8 files changed, 53 insertions(+), 83 deletions(-) delete mode 100644 pkg/commands/os_linux.go create mode 100644 pkg/config/config_default_platform.go create mode 100644 pkg/config/config_linux.go create mode 100644 pkg/config/config_windows.go diff --git a/pkg/commands/os.go b/pkg/commands/os.go index 9fc3a2bc0..834c45376 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -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 } diff --git a/pkg/commands/os_linux.go b/pkg/commands/os_linux.go deleted file mode 100644 index c17ebc98f..000000000 --- a/pkg/commands/os_linux.go +++ /dev/null @@ -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 &\"", - } -} diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go index 21d72dc67..1eab1580d 100644 --- a/pkg/commands/os_test.go +++ b/pkg/commands/os_test.go @@ -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)) } diff --git a/pkg/commands/os_windows.go b/pkg/commands/os_windows.go index 51ad401cd..1658e5f36 100644 --- a/pkg/commands/os_windows.go +++ b/pkg/commands/os_windows.go @@ -6,5 +6,5 @@ func getPlatform() *Platform { shell: "cmd", shellArg: "/c", escapedQuote: `\"`, - openCommand: `cmd /c "start "" {{filename}}"`, -}} + } +} diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index b5503d6fd..3e384d80f 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -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}}' `) } diff --git a/pkg/config/config_default_platform.go b/pkg/config/config_default_platform.go new file mode 100644 index 000000000..a87d5a7f6 --- /dev/null +++ b/pkg/config/config_default_platform.go @@ -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}}'`) +} diff --git a/pkg/config/config_linux.go b/pkg/config/config_linux.go new file mode 100644 index 000000000..ef30ac7d2 --- /dev/null +++ b/pkg/config/config_linux.go @@ -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 &\"'`) +} diff --git a/pkg/config/config_windows.go b/pkg/config/config_windows.go new file mode 100644 index 000000000..b81a5fdb5 --- /dev/null +++ b/pkg/config/config_windows.go @@ -0,0 +1,8 @@ +package config + +// GetPlatformDefaultConfig gets the defaults for the platform +func GetPlatformDefaultConfig() []byte { + return []byte( + `os: + openCommand: 'cmd /c "start "" {{filename}}"'`) +}