summaryrefslogtreecommitdiffstats
path: root/pkg/commands/os.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-08-31 18:43:54 +1000
committerJesse Duffield <jessedduffield@gmail.com>2018-09-01 11:38:32 +1000
commitae0d88f855773dd76432487509f9af13f987a812 (patch)
tree849249573b2b809a0b9e406cd15ee000cc92ec76 /pkg/commands/os.go
parentcff1dee6dcb303e789b804bbe270a0032f0b76a7 (diff)
WIP using runDirectCommand with xdg-open
Diffstat (limited to 'pkg/commands/os.go')
-rw-r--r--pkg/commands/os.go54
1 files changed, 17 insertions, 37 deletions
diff --git a/pkg/commands/os.go b/pkg/commands/os.go
index 0fa92f724..fd27d39da 100644
--- a/pkg/commands/os.go
+++ b/pkg/commands/os.go
@@ -6,7 +6,8 @@ import (
"os/exec"
"strings"
- "github.com/davecgh/go-spew/spew"
+ "github.com/jesseduffield/lazygit/pkg/config"
+ "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/mgutz/str"
@@ -20,22 +21,25 @@ type Platform struct {
shell string
shellArg string
escapedQuote string
+ openCommand string
}
// OSCommand holds all the os commands
type OSCommand struct {
Log *logrus.Entry
Platform *Platform
+ Config config.AppConfigurer
command func(string, ...string) *exec.Cmd
getGlobalGitConfig func(string) (string, error)
getenv func(string) string
}
// NewOSCommand os command runner
-func NewOSCommand(log *logrus.Entry) *OSCommand {
+func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand {
return &OSCommand{
Log: log,
Platform: getPlatform(),
+ Config: config,
command: exec.Command,
getGlobalGitConfig: gitconfig.Global,
getenv: os.Getenv,
@@ -74,12 +78,10 @@ func (c *OSCommand) FileType(path string) string {
// RunDirectCommand wrapper around direct commands
func (c *OSCommand) RunDirectCommand(command string) (string, error) {
c.Log.WithField("command", command).Info("RunDirectCommand")
- args := str.ToArgv(c.Platform.shellArg + " " + command)
- c.Log.Info(spew.Sdump(args))
return sanitisedCommandOutput(
exec.
- Command(c.Platform.shell, args...).
+ Command(c.Platform.shell, c.Platform.shellArg, command).
CombinedOutput(),
)
}
@@ -95,45 +97,23 @@ func sanitisedCommandOutput(output []byte, err error) (string, error) {
}
// getOpenCommand get open command
-func (c *OSCommand) getOpenCommand() (string, string, error) {
- //NextStep open equivalents: xdg-open (linux), cygstart (cygwin), open (OSX)
- trailMap := map[string]string{
- "xdg-open": " &>/dev/null &",
- "cygstart": "",
- "open": "",
+func (c *OSCommand) getOpenCommand() string {
+ if c.Config.GetUserConfig().IsSet("os.openCommand") {
+ return c.Config.GetUserConfig().GetString("os.openCommand")
}
-
- for name, trail := range trailMap {
- if err := c.RunCommand("which " + name); err == nil {
- return name, trail, nil
- }
- }
- return "", "", errors.New("Unsure what command to use to open this file")
-}
-
-// VsCodeOpenFile opens the file in code, with the -r flag to open in the
-// current window
-// each of these open files needs to have the same function signature because
-// they're being passed as arguments into another function,
-// but only editFile actually returns a *exec.Cmd
-func (c *OSCommand) VsCodeOpenFile(filename string) (*exec.Cmd, error) {
- return nil, c.RunCommand("code -r " + filename)
-}
-
-// SublimeOpenFile opens the filein sublime
-// may be deprecated in the future
-func (c *OSCommand) SublimeOpenFile(filename string) (*exec.Cmd, error) {
- return nil, c.RunCommand("subl " + filename)
+ return c.Platform.openCommand
}
// OpenFile opens a file with the given
func (c *OSCommand) OpenFile(filename string) error {
- cmdName, cmdTrail, err := c.getOpenCommand()
- if err != nil {
- return err
+ commandTemplate := c.getOpenCommand()
+ templateValues := map[string]string{
+ "filename": c.Quote(filename),
}
- return c.RunCommand(cmdName + " " + c.Quote(filename) + cmdTrail) // TODO: test on linux
+ command := utils.ResolvePlaceholderString(commandTemplate, templateValues)
+ _, err := c.RunDirectCommand(command)
+ return err
}
// EditFile opens a file in a subprocess using whatever editor is available,