-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
187 lines (155 loc) · 4.55 KB
/
main.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
package main
import (
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"github.com/oliverisaac/iterm-cmd/generate"
"github.com/oliverisaac/shellescape"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
var version string = "unset-version"
var commit string = "unset-commit"
func main() {
workDir := path.Join(os.TempDir(), "it2cmd")
if dir, isSet := os.LookupEnv("ITERM_CMD_DIR"); isSet {
workDir = dir
}
if len(os.Args) <= 1 {
log.Fatal("You must pass in at least one argument")
}
firstArg := os.Args[1]
// Check if user is asking for help
for _, h := range []string{"-h", "--help", "help", "--h"} {
if strings.EqualFold(firstArg, h) {
printHelp()
return
}
}
var err error
if strings.EqualFold(firstArg, "handle") {
err = handleClick(workDir)
} else if strings.EqualFold(firstArg, "version") {
fmt.Printf("iterm-cmd version: %s (%s)\n", version, commit)
return
} else {
err = handleCreate(workDir)
}
if err != nil {
log.Fatal(err)
}
}
func handleClick(workDir string) error {
if len(os.Args) < 3 {
return errors.New("You must pass a filename when calling with `handle`")
}
// "handle" is arg 1
pathArg := os.Args[2]
var lineNumber int
var err error
if len(os.Args) > 3 {
lineNumberString := os.Args[3]
if lineNumberString != "" {
lineNumber, err = strconv.Atoi(os.Args[3])
if err != nil {
return errors.Wrap(err, "Second argument should be line number")
}
}
}
isDir, err := pathIsDirectory(pathArg)
if err != nil {
return errors.Wrap(err, "Checking if path is directory")
}
// Handle the scenario where we passed af file in the iterm_cmd dir
if !isDir && strings.HasPrefix(pathArg, workDir) {
logrus.Trace("fileArg starts with Workdir and is not a directory, going to treate as iterm-cmd")
src, err := os.Open(pathArg)
if err != nil {
return errors.Wrap(err, "Failed to open file for reading")
}
defer src.Close()
io.Copy(os.Stdout, src)
if lookupBoolEnv("ITERM_CMD_RUN_ON_CLICK", true) {
fmt.Printf("\n")
}
return nil
} else if isDir {
logrus.Trace("fileArg is a directory, going to CD to it")
cmd := []string{}
if lookupBoolEnv("ITERM_CMD_PRINT_EASY_NAV", true) {
easyNav := fmt.Sprintf(`echo "< $PWD ^ $( dirname %s )"`, shellescape.Escape(pathArg))
cmd = append(cmd, easyNav)
}
cmd = append(cmd, fmt.Sprintf("cd %s", shellescape.Escape(pathArg)))
if lookupBoolEnv("ITERM_CMD_LS_AFTER_CD", true) {
cmd = append(cmd, "ls")
}
fmt.Println(strings.Join(cmd, "; "))
return nil
} else {
logrus.Trace("fileArg is a file, going to open it in EDITOR")
editor, editorSet := os.LookupEnv("EDITOR")
if !editorSet {
logrus.Trace("EDITOR is not set, defaulting to vim")
editor = "vim"
}
cmd := []string{editor}
if lineNumber > 0 {
if strings.HasSuffix(editor, "vim") {
cmd = append(cmd, fmt.Sprintf("+%d", lineNumber))
} else if strings.HasSuffix(editor, "code") {
cmd = append(cmd, "--goto", fmt.Sprintf("%s:%d", pathArg, lineNumber))
}
}
cmd = append(cmd, pathArg)
fmt.Println(strings.Join(shellescape.EscapeArgs(cmd), " "))
}
return nil
}
func lookupBoolEnv(envVar string, defaultValue bool) bool {
retVal := defaultValue
if val, ok := os.LookupEnv(envVar); ok {
var err error
retVal, err = strconv.ParseBool(val)
if err != nil {
log.Error(errors.Wrapf(err, "Parsing boolean option %s", envVar))
}
}
return retVal
}
// isDirectory determines if a file represented
// by `path` is a directory or not
// https://freshman.tech/snippets/go/check-if-file-is-dir/
func pathIsDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, errors.Wrap(err, "Getting stat of possible directory")
}
return fileInfo.IsDir(), nil
}
func handleCreate(workDir string) error {
exe := os.Args[1]
var args []string
if len(os.Args) > 2 {
args = os.Args[2:]
}
file, err := generate.NewCommandGenerator(nil, workDir).CommandFile(exe, args...)
if err != nil {
return errors.Wrap(err, "Creating command file")
}
fmt.Printf("%s", file)
return nil
}
func printHelp() {
help := `
iterm-cmd takes an arbitrary command as an argument and then generates a file in ITERM_CMD_DIR named after the hash of that command. The hash is truncated to create unique filenames but also be as short as possible.
This is meant to be used in conjunction with the iTerm2 cmd-click functionality.
More details are here: github.com/oliverisaac/iterm-cmd/
If this command is called with 'handle' as its first argument, then it will do the handle portion of handling clicks.
`
fmt.Println(help)
}