MCPcopy
hub / github.com/direnv/direnv / watchDirCommand

Function watchDirCommand

internal/cmd/cmd_watch_dir.go:18–65  ·  view source on GitHub ↗
(env Env, args []string)

Source from the content-addressed store, hash-verified

16}
17
18func watchDirCommand(env Env, args []string) (err error) {
19 if len(args) < 3 {
20 return fmt.Errorf("a directory is required to add to the list of watches")
21 }
22
23 shellName := args[1]
24 dir := args[2]
25
26 shell := DetectShell(shellName)
27
28 if shell == nil {
29 return fmt.Errorf("unknown target shell '%s'", shellName)
30 }
31
32 if _, err := os.Stat(dir); os.IsNotExist(err) {
33 return fmt.Errorf("dir '%s' does not exist", dir)
34 }
35
36 watches := NewFileTimes()
37 watchString, ok := env[DIRENV_WATCHES]
38 if ok {
39 err = watches.Unmarshal(watchString)
40 if err != nil {
41 return err
42 }
43 }
44
45 err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
46 if err != nil {
47 return err
48 }
49 return watches.NewTime(path, info.ModTime().Unix(), true)
50 })
51 if err != nil {
52 return fmt.Errorf("failed to recursively watch dir '%s': %w", dir, err)
53 }
54
55 e := make(ShellExport)
56 e.Add(DIRENV_WATCHES, watches.Marshal())
57
58 exportStr, err := shell.Export(e)
59 if err != nil {
60 return err
61 }
62 _, _ = os.Stdout.WriteString(exportStr)
63
64 return
65}

Callers

nothing calls this directly

Calls 7

UnmarshalMethod · 0.95
NewTimeMethod · 0.95
MarshalMethod · 0.95
DetectShellFunction · 0.85
NewFileTimesFunction · 0.85
AddMethod · 0.80
ExportMethod · 0.65

Tested by

no test coverage detected