MCPcopy
hub / github.com/direnv/direnv / watchListCommand

Function watchListCommand

internal/cmd/cmd_watch_list.go:22–86  ·  view source on GitHub ↗
(env Env, args []string)

Source from the content-addressed store, hash-verified

20}
21
22func watchListCommand(env Env, args []string) (err error) {
23 var shellName string
24
25 if len(args) >= 2 {
26 shellName = args[1]
27 } else {
28 shellName = "bash"
29 }
30
31 shell := DetectShell(shellName)
32
33 if shell == nil {
34 return fmt.Errorf("unknown target shell '%s'", shellName)
35 }
36
37 watches := NewFileTimes()
38 watchString, ok := env[DIRENV_WATCHES]
39 if ok {
40 err = watches.Unmarshal(watchString)
41 if err != nil {
42 return err
43 }
44 }
45
46 // Read `mtime path` lines from stdin
47 reader := bufio.NewReader(os.Stdin)
48
49 i := 1
50 for {
51 line, err := reader.ReadString('\n')
52 if err == nil {
53 elems := strings.SplitN(line, " ", 2)
54 if len(elems) != 2 {
55 return fmt.Errorf("line %d: expected to contain two elements", i)
56 }
57 mtime, err := strconv.Atoi(elems[0])
58 if err != nil {
59 return fmt.Errorf("line %d: %w", i, err)
60 }
61 path := elems[1][:len(elems[1])-1]
62
63 // add to watches
64 err = watches.NewTime(path, int64(mtime), true)
65 if err != nil {
66 return err
67 }
68 } else if errors.Is(err, io.EOF) {
69 break
70 } else {
71 return fmt.Errorf("line %d: %w", i, err)
72 }
73 i++
74 }
75
76 e := make(ShellExport)
77 e.Add(DIRENV_WATCHES, watches.Marshal())
78
79 exportStr, err := shell.Export(e)

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