forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_edit.go
61 lines (53 loc) · 1.17 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
// `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 foundRC *RC
if config, err = LoadConfig(env); err != nil {
return
}
if len(args) > 1 {
rcPath = args[1]
fi, _ := os.Stat(rcPath)
if fi != nil && fi.IsDir() {
rcPath = filepath.Join(rcPath, ".envrc")
}
} else {
foundRC = config.FindRC()
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 == "" {
err = fmt.Errorf("$EDITOR not found.")
return
}
cmd := exec.Command(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.Allow()
}
return
},
}