MCPcopy
hub / github.com/direnv/direnv / cmdEditAction

Function cmdEditAction

internal/cmd/cmd_edit.go:21–83  ·  view source on GitHub ↗
(env Env, args []string, config *Config)

Source from the content-addressed store, hash-verified

19}
20
21func cmdEditAction(env Env, args []string, config *Config) (err error) {
22 var rcPath string
23 var times *FileTimes
24 var foundRC *RC
25
26 defer log.SetPrefix(log.Prefix())
27 log.SetPrefix(log.Prefix() + "cmd_edit: ")
28
29 foundRC, err = config.FindRC()
30 if err != nil {
31 return err
32 }
33 if foundRC != nil {
34 times = &foundRC.times
35 }
36
37 if len(args) > 1 {
38 rcPath = args[1]
39 fi, _ := os.Stat(rcPath)
40 if fi != nil && fi.IsDir() {
41 rcPath = filepath.Join(rcPath, ".envrc")
42 }
43 } else {
44 if foundRC == nil {
45 return fmt.Errorf(".envrc or .env not found. Use `direnv edit .` to create a new .envrc in the current directory")
46 }
47 rcPath = foundRC.path
48 }
49
50 editor := env["EDITOR"]
51 if editor == "" {
52 logError(config, "$EDITOR not found.")
53 editor = detectEditor(env["PATH"])
54 if editor == "" {
55 err = fmt.Errorf("could not find a default editor in the PATH")
56 return
57 }
58 }
59
60 run := fmt.Sprintf("%s %s", editor, BashEscape(rcPath))
61
62 // G204: Subprocess launched with function call as argument or cmd arguments
63 // #nosec
64 cmd := exec.Command(config.BashPath, "-c", run)
65 cmd.Stdin = os.Stdin
66 cmd.Stdout = os.Stdout
67 cmd.Stderr = os.Stderr
68 if err = cmd.Run(); err != nil {
69 return
70 }
71
72 foundRC, err = FindRC(rcPath, config)
73 logDebug("foundRC: %#v", foundRC)
74 logDebug("times: %#v", times)
75 if times != nil {
76 logDebug("times.Check(): %#v", times.Check())
77 }
78 if err == nil && foundRC != nil && (times == nil || times.Check() != nil) {

Callers

nothing calls this directly

Calls 8

CheckMethod · 0.95
AllowMethod · 0.95
logErrorFunction · 0.85
detectEditorFunction · 0.85
BashEscapeFunction · 0.85
FindRCFunction · 0.85
logDebugFunction · 0.85
FindRCMethod · 0.80

Tested by

no test coverage detected