-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcli.go
203 lines (164 loc) · 4.52 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"os/exec"
)
func firstNonEmpty(arr []string) string {
for _, s := range arr {
if len(s) > 0 {
return s
}
}
return ""
}
func isVolumeRoot(path string) bool {
return os.IsPathSeparator(path[len(path)-1])
}
func isGitRoot(path string) bool {
gitpath := filepath.Join(path, ".git")
fi, err := os.Stat(gitpath)
if err != nil {
return false
}
return fi.IsDir()
}
func findGitRoot(path string) (bool, string) {
if absRoot, err := filepath.Abs(path); err == nil {
path = absRoot
}
if withoutLinks, err := filepath.EvalSymlinks(path); err == nil {
path = withoutLinks
}
path = filepath.Clean(path)
for isVolumeRoot(path) == false {
if isGitRoot(path) {
return true, path
} else {
if path == filepath.Dir(path) {
panic("findGitRoot will loop")
}
path = filepath.Dir(path)
}
}
return false, ""
}
const usage string = `The basic command is:
hf [path] [command]
The default path is the current folder.
The default command is the first valid of $GIT_EDITOR, $EDITOR, or vim
(subl on Windows).
To find in your git project, use:
hf -git [command]
(if -git is provided, then next argument (optional) is assumed to be a command)
If the binary name is 'hfg', the -git option is assumed.
This was done because Windows users have no easy way of creating command aliases.
A -cmd=<yourcmd> option is provided to simplify aliases. For example, I defined
iga (interactive git add) like this:
alias iga='hf -cmd="git add"'
Examples:
hf
hf -git
hf -git vim
hf ~/go/src/
hfg rm
hf . rm
Inside the app:
a-z0-9 Edit input string (those also work: backspace/C-a/C-e/C-k/Home/End)
Up/down Move cursor to next file
Space Toggle mark for current file and move to next
C-t Toggle mark for all files
C-s Toggle "run command in shell"
TAB Jump to edit command (and back)
RET Run command
ESC Quit
When editing the command, the string $FILES is special and will
replaced by the select (or marked) files, properly quoted.
`
type Options struct {
debug bool // debug mode (print stats when closing, etc)
fakefiles int // generate fake filenames for testing performance
git bool // user wants to search in git project
rootDir string // path to recursively search
runCmd string // initial command to run
folderDisplay string // string to display in modeline
}
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func ParseArgs() (opts *Options, err error) {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage)
}
git := flag.Bool("git", false, "Find in current git project instead of folder")
cmd := flag.String("cmd", "", "Command to run (alternate syntax to simplify alias)")
debug := flag.Bool("debug", false, "Print stats in the end (debug only)")
fakefiles := flag.Int("fakefiles", 0, "Generate N fake file names for testing")
flag.Parse()
opts = &Options{
debug: *debug,
fakefiles: *fakefiles,
git: *git,
runCmd: "",
rootDir: ".",
}
// this is hacky :(
command := os.Args[0]
if strings.HasSuffix(command, "hfg") || strings.HasSuffix(command, "hfg.exe") {
opts.git = true
}
if opts.git {
foundGit, gitFolder := findGitRoot(".")
if !foundGit {
err = errors.New("Git project not found")
return
} else {
opts.rootDir = gitFolder
}
}
var defaultEditor string
if runtime.GOOS == "windows" {
if commandExists("subl") {
defaultEditor = "subl"
} else {
defaultEditor = "notepad"
}
} else {
defaultEditor = "vim"
}
defaultCmd := firstNonEmpty([]string{os.Getenv("GIT_EDITOR"), os.Getenv("EDITOR"), defaultEditor})
hasCmd := (*cmd != "")
if *cmd != "" {
opts.runCmd = *cmd
} else {
opts.runCmd = defaultCmd
}
argLen := len(flag.Args())
if argLen >= 3 || (argLen == 2 && (opts.git || hasCmd)) || (argLen == 1 && opts.git && hasCmd) {
err = errors.New("Could not parse options")
} else if opts.git && !hasCmd && argLen == 1 {
opts.runCmd = flag.Arg(0)
} else if !opts.git && argLen == 1 {
opts.rootDir = flag.Arg(0)
} else if !opts.git && !hasCmd && argLen == 2 {
opts.rootDir = flag.Arg(0)
opts.runCmd = flag.Arg(1)
}
opts.rootDir = filepath.Clean(opts.rootDir)
if withoutLinks, err := filepath.EvalSymlinks(opts.rootDir); err == nil {
opts.rootDir = withoutLinks
}
if opts.git {
opts.folderDisplay += "git:"
} else {
opts.folderDisplay += "cwd:"
}
opts.folderDisplay += opts.rootDir
return
}