forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_edit.go
91 lines (79 loc) · 1.79 KB
/
cmd_edit.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// `direnv edit [PATH_TO_RC]`
var CmdEdit = &Cmd{
Name: "edit",
Desc: `Opens PATH_TO_RC or the current .envrc into an $EDITOR and allow
the file to be loaded afterwards.`,
Args: []string{"[PATH_TO_RC]"},
NoWait: true,
Fn: func(env Env, args []string) (err error) {
var config *Config
var rcPath string
var mtime int64
var foundRC *RC
if config, err = LoadConfig(env); err != nil {
return
}
foundRC = config.FindRC()
if foundRC != nil {
mtime = foundRC.mtime
}
if len(args) > 1 {
rcPath = args[1]
fi, _ := os.Stat(rcPath)
if fi != nil && fi.IsDir() {
rcPath = filepath.Join(rcPath, ".envrc")
}
} else {
if foundRC == nil {
return fmt.Errorf(".envrc not found. Use `direnv edit .` to create a new envrc in the current directory.")
}
rcPath = foundRC.path
}
editor := env["EDITOR"]
if editor == "" {
log_error("$EDITOR not found.")
editor = detectEditor(env["PATH"])
if editor == "" {
err = fmt.Errorf("Could not find a default editor in the PATH")
return
}
}
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("%s %s", editor, rcPath))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return
}
foundRC = FindRC(rcPath, config.AllowDir())
if foundRC != nil && foundRC.mtime > mtime {
foundRC.Allow()
}
return
},
}
// Utils
var EDITORS = [][]string{
{"subl", "-w"},
{"mate", "-w"},
{"open", "-t", "-W"}, // Opens with the default text editor on mac
{"nano"},
{"vim"},
{"emacs"},
}
func detectEditor(pathenv string) string {
for _, editor := range EDITORS {
if _, err := lookPath(editor[0], pathenv); err == nil {
return strings.Join(editor, " ")
}
}
return ""
}