summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2017-09-28 23:05:02 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2017-09-28 23:05:02 +0900
commitee40212e9772941b49a4bdb1e95f99913cdea469 (patch)
tree799667bda43ca07dbb33b3992f10ba6ea608062e /src
parent7f5f6efbac677603181434193abbb2a8d550b8b5 (diff)
Update FZF_DEFAULT_COMMAND
- Use bash for `set -o pipefail` - Fall back to simpler find command when the original command failed Related: #1061
Diffstat (limited to 'src')
-rw-r--r--src/constants.go2
-rw-r--r--src/reader.go10
-rw-r--r--src/reader_test.go4
-rw-r--r--src/util/util_unix.go5
-rw-r--r--src/util/util_windows.go8
5 files changed, 21 insertions, 8 deletions
diff --git a/src/constants.go b/src/constants.go
index cfd3a3bc..d5445299 100644
--- a/src/constants.go
+++ b/src/constants.go
@@ -55,7 +55,7 @@ var defaultCommand string
func init() {
if !util.IsWindows() {
- defaultCommand = `command find -L . -mindepth 1 \( -path '*/\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \) -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-`
+ defaultCommand = `set -o pipefail; (command find -L . -mindepth 1 \( -path '*/\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \) -prune -o -type f -print -o -type l -print || command find -L . -mindepth 1 -path '*/\.*' -prune -o -type f -print -o -type l -print) 2> /dev/null | cut -b3-`
} else if os.Getenv("TERM") == "cygwin" {
defaultCommand = `sh -c "command find -L . -mindepth 1 -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-"`
} else {
diff --git a/src/reader.go b/src/reader.go
index 22ce4ba0..5fd6d876 100644
--- a/src/reader.go
+++ b/src/reader.go
@@ -56,9 +56,11 @@ func (r *Reader) ReadSource() {
if util.IsTty() {
cmd := os.Getenv("FZF_DEFAULT_COMMAND")
if len(cmd) == 0 {
- cmd = defaultCommand
+ // The default command for *nix requires bash
+ success = r.readFromCommand("bash", defaultCommand)
+ } else {
+ success = r.readFromCommand("sh", cmd)
}
- success = r.readFromCommand(cmd)
} else {
success = r.readFromStdin()
}
@@ -100,8 +102,8 @@ func (r *Reader) readFromStdin() bool {
return true
}
-func (r *Reader) readFromCommand(cmd string) bool {
- listCommand := util.ExecCommand(cmd)
+func (r *Reader) readFromCommand(shell string, cmd string) bool {
+ listCommand := util.ExecCommandWith(shell, cmd)
out, err := listCommand.StdoutPipe()
if err != nil {
return false
diff --git a/src/reader_test.go b/src/reader_test.go
index 82ca6b7b..c29936ce 100644
--- a/src/reader_test.go
+++ b/src/reader_test.go
@@ -23,7 +23,7 @@ func TestReadFromCommand(t *testing.T) {
}
// Normal command
- reader.fin(reader.readFromCommand(`echo abc && echo def`))
+ reader.fin(reader.readFromCommand("sh", `echo abc && echo def`))
if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
t.Errorf("%s", strs)
}
@@ -48,7 +48,7 @@ func TestReadFromCommand(t *testing.T) {
reader.startEventPoller()
// Failing command
- reader.fin(reader.readFromCommand(`no-such-command`))
+ reader.fin(reader.readFromCommand("sh", `no-such-command`))
strs = []string{}
if len(strs) > 0 {
t.Errorf("%s", strs)
diff --git a/src/util/util_unix.go b/src/util/util_unix.go
index d538ee00..fc63c027 100644
--- a/src/util/util_unix.go
+++ b/src/util/util_unix.go
@@ -14,6 +14,11 @@ func ExecCommand(command string) *exec.Cmd {
if len(shell) == 0 {
shell = "sh"
}
+ return ExecCommandWith(shell, command)
+}
+
+// ExecCommandWith executes the given command with the specified shell
+func ExecCommandWith(shell string, command string) *exec.Cmd {
return exec.Command(shell, "-c", command)
}
diff --git a/src/util/util_windows.go b/src/util/util_windows.go
index efd19a2a..493f4d7f 100644
--- a/src/util/util_windows.go
+++ b/src/util/util_windows.go
@@ -10,8 +10,14 @@ import (
"github.com/mattn/go-shellwords"
)
-// ExecCommand executes the given command with $SHELL
+// ExecCommand executes the given command with cmd
func ExecCommand(command string) *exec.Cmd {
+ return ExecCommandWith("cmd", command)
+}
+
+// ExecCommandWith executes the given command with cmd. _shell parameter is
+// ignored on Windows.
+func ExecCommandWith(_shell string, command string) *exec.Cmd {
args, _ := shellwords.Parse(command)
allArgs := make([]string, len(args)+1)
allArgs[0] = "/c"