summaryrefslogtreecommitdiffstats
path: root/pkg/commands/oscommands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2020-11-25 08:52:00 +1100
committerJesse Duffield <jessedduffield@gmail.com>2020-11-28 10:45:30 +1100
commit999e170f1d17b652dad231f1cbde6ab4bbeae8c7 (patch)
tree3a7ea186c3b868661242731a029dce3df396de1e /pkg/commands/oscommands
parent7513bfb13a48c566e9ab0c31e259ea73d562c292 (diff)
standardise how we read from the config
Diffstat (limited to 'pkg/commands/oscommands')
-rw-r--r--pkg/commands/oscommands/os.go52
-rw-r--r--pkg/commands/oscommands/os_test.go152
2 files changed, 12 insertions, 192 deletions
diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go
index da9fb311f..bbfcb8095 100644
--- a/pkg/commands/oscommands/os.go
+++ b/pkg/commands/oscommands/os.go
@@ -19,7 +19,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/mgutz/str"
"github.com/sirupsen/logrus"
- gitconfig "github.com/tcnksm/go-gitconfig"
)
// Platform stores the os state
@@ -36,25 +35,23 @@ type Platform struct {
// OSCommand holds all the os commands
type OSCommand struct {
- Log *logrus.Entry
- Platform *Platform
- Config config.AppConfigurer
- Command func(string, ...string) *exec.Cmd
- BeforeExecuteCmd func(*exec.Cmd)
- GetGlobalGitConfig func(string) (string, error)
- Getenv func(string) string
+ Log *logrus.Entry
+ Platform *Platform
+ Config config.AppConfigurer
+ Command func(string, ...string) *exec.Cmd
+ BeforeExecuteCmd func(*exec.Cmd)
+ Getenv func(string) string
}
// NewOSCommand os command runner
func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand {
return &OSCommand{
- Log: log,
- Platform: getPlatform(),
- Config: config,
- Command: exec.Command,
- BeforeExecuteCmd: func(*exec.Cmd) {},
- GetGlobalGitConfig: gitconfig.Global,
- Getenv: os.Getenv,
+ Log: log,
+ Platform: getPlatform(),
+ Config: config,
+ Command: exec.Command,
+ BeforeExecuteCmd: func(*exec.Cmd) {},
+ Getenv: os.Getenv,
}
}
@@ -235,31 +232,6 @@ func (c *OSCommand) OpenLink(link string) error {
return err
}
-// EditFile opens a file in a subprocess using whatever editor is available,
-// falling back to core.editor, VISUAL, EDITOR, then vi
-func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) {
- editor, _ := c.GetGlobalGitConfig("core.editor")
-
- if editor == "" {
- editor = c.Getenv("VISUAL")
- }
- if editor == "" {
- editor = c.Getenv("EDITOR")
- }
- if editor == "" {
- if err := c.RunCommand("which vi"); err == nil {
- editor = "vi"
- }
- }
- if editor == "" {
- return nil, errors.New("No editor defined in $VISUAL, $EDITOR, or git config")
- }
-
- splitCmd := str.ToArgv(fmt.Sprintf("%s %s", editor, c.Quote(filename)))
-
- return c.PrepareSubProcess(splitCmd[0], splitCmd[1:]...), nil
-}
-
// PrepareSubProcess iniPrepareSubProcessrocess then tells the Gui to switch to it
// TODO: see if this needs to exist, given that ExecutableFromString does the same things
func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *exec.Cmd {
diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go
index e4859484c..f8b3767bf 100644
--- a/pkg/commands/oscommands/os_test.go
+++ b/pkg/commands/oscommands/os_test.go
@@ -109,158 +109,6 @@ func TestOSCommandOpenFile(t *testing.T) {
}
}
-// TestOSCommandEditFile is a function.
-func TestOSCommandEditFile(t *testing.T) {
- type scenario struct {
- filename string
- command func(string, ...string) *exec.Cmd
- getenv func(string) string
- getGlobalGitConfig func(string) (string, error)
- test func(*exec.Cmd, error)
- }
-
- scenarios := []scenario{
- {
- "test",
- func(name string, arg ...string) *exec.Cmd {
- return exec.Command("exit", "1")
- },
- func(env string) string {
- return ""
- },
- func(cf string) (string, error) {
- return "", nil
- },
- func(cmd *exec.Cmd, err error) {
- assert.EqualError(t, err, "No editor defined in $VISUAL, $EDITOR, or git config")
- },
- },
- {
- "test",
- func(name string, arg ...string) *exec.Cmd {
- if name == "which" {
- return exec.Command("exit", "1")
- }
-
- assert.EqualValues(t, "nano", name)
-
- return nil
- },
- func(env string) string {
- return ""
- },
- func(cf string) (string, error) {
- return "nano", nil
- },
- func(cmd *exec.Cmd, err error) {
- assert.NoError(t, err)
- },
- },
- {
- "test",
- func(name string, arg ...string) *exec.Cmd {
- if name == "which" {
- return exec.Command("exit", "1")
- }
-
- assert.EqualValues(t, "nano", name)
-
- return nil
- },
- func(env string) string {
- if env == "VISUAL" {
- return "nano"
- }
-
- return ""
- },
- func(cf string) (string, error) {
- return "", nil
- },
- func(cmd *exec.Cmd, err error) {
- assert.NoError(t, err)
- },
- },
- {
- "test",
- func(name string, arg ...string) *exec.Cmd {
- if name == "which" {
- return exec.Command("exit", "1")
- }
-
- assert.EqualValues(t, "emacs", name)
-
- return nil
- },
- func(env string) string {
- if env == "EDITOR" {
- return "emacs"
- }
-
- return ""
- },
- func(cf string) (string, error) {
- return "", nil
- },
- func(cmd *exec.Cmd, err error) {
- assert.NoError(t, err)
- },
- },
- {
- "test",
- func(name string, arg ...string) *exec.Cmd {
- if name == "which" {
- return exec.Command("echo")
- }
-
- assert.EqualValues(t, "vi", name)
-
- return nil
- },
- func(env string) string {
- return ""
- },
- func(cf string) (string, error) {
- return "", nil
- },
- func(cmd *exec.Cmd, err error) {
- assert.NoError(t, err)
- },
- },
- {
- "file/with space",
- func(name string, args ...string) *exec.Cmd {
- if name == "which" {
- return exec.Command("echo")
- }
-
- assert.EqualValues(t, "vi", name)
- assert.EqualValues(t, "file/with space", args[0])
-
- return nil
- },
- func(env string) string {
- return ""
- },
- func(cf string) (string, error) {
- return "", nil
- },
- func(cmd *exec.Cmd, err error) {
- assert.NoError(t, err)
- },
- },
- }
-
- for _, s := range scenarios {
- OSCmd := NewDummyOSCommand()
- OSCmd.Command = s.command
- OSCmd.GetGlobalGitConfig = s.getGlobalGitConfig
- OSCmd.Getenv = s.getenv
-
- s.test(OSCmd.EditFile(s.filename))
- }
-}
-
// TestOSCommandQuote is a function.
func TestOSCommandQuote(t *testing.T) {
osCommand := NewDummyOSCommand()