-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatuscmd.go
166 lines (136 loc) · 3.56 KB
/
statuscmd.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"github.com/fatih/color"
"golang.org/x/term"
"dbohdan.com/regular/envfile"
)
func (s *StatusCmd) Run(config Config) error {
width := getTermWidth()
separator := strings.Repeat("-", width)
jobs := newJobScheduler()
err := filepath.Walk(config.ConfigRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Base(path) == jobConfigFileName {
_, _, err := jobs.update(config.ConfigRoot, path)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return fmt.Errorf("error looking for jobs in config dir: %v", err)
}
db, err := openAppDB(config.StateRoot)
if err != nil {
return err
}
defer db.close()
secret := regexp.MustCompile(secretRegexp)
seenNames := make(map[string]struct{})
// We iterate over a copy of selectedNames instead of the keys of jobs.byName to preserve order.
selectedNames := s.JobNames[:]
if len(selectedNames) == 0 {
for name := range jobs.byName {
selectedNames = append(selectedNames, name)
}
slices.Sort(selectedNames)
}
for i, name := range selectedNames {
job, ok := jobs.byName[name]
if !ok {
continue
}
_, seen := seenNames[name]
if seen {
continue
}
seenNames[name] = struct{}{}
for key, value := range envfile.OS() {
if osValue, ok := job.Env[key]; ok && value == osValue {
delete(job.Env, key)
continue
}
if secret.MatchString(key) {
job.Env[key] = redactedValue
}
}
color.Set(color.Bold)
fmt.Println(name)
color.Unset()
fmt.Println(" duplicate:", boolYesNo(job.Duplicate))
fmt.Println(" enable:", boolYesNo(job.Enable))
if len(job.Env) == 0 {
fmt.Println(" env: none")
} else {
fmt.Println(" env:")
for _, k := range job.Env.Keys() {
fmt.Printf(" %v: %v\n", k, job.Env[k])
}
}
fmt.Println(" jitter:", formatDuration(job.Jitter))
fmt.Println(" log:", boolYesNo(job.Log))
fmt.Println(" queue:", job.QueueName())
fmt.Println()
completed, err := db.getLastCompleted(job.Name)
if err != nil {
return fmt.Errorf("error getting last completed job %q: %w", name, err)
}
if completed == nil {
fmt.Println(" last started: unknown")
fmt.Println(" last finished: unknown")
fmt.Println(" exit status: unknown")
} else {
fmt.Println(" last started: ", completed.Started.Format(timestampFormat))
fmt.Println(" last finished:", completed.Finished.Format(timestampFormat))
fmt.Println(" exit status:", completed.ExitStatus)
}
fmt.Println(" logs:")
stdoutLines, err := db.getJobLogs(name, "stdout", s.LogLines)
if err != nil {
return fmt.Errorf("error loading stdout for job %q: %w", name, err)
}
if len(stdoutLines) == 0 {
fmt.Println(" stdout: empty")
} else {
fmt.Println(" stdout:")
fmt.Println(separator)
for _, line := range stdoutLines {
fmt.Println(line)
}
fmt.Println(separator)
}
stderrLines, err := db.getJobLogs(name, "stderr", s.LogLines)
if err != nil {
return fmt.Errorf("error loading stderr for job %q: %w", name, err)
}
if len(stderrLines) == 0 {
fmt.Println(" stderr: empty")
} else {
fmt.Println(" stderr:")
fmt.Println(separator)
for _, line := range stderrLines {
fmt.Println(line)
}
fmt.Println(separator)
}
if i != len(selectedNames)-1 {
fmt.Println()
}
}
return nil
}
func getTermWidth() int {
if w, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil {
return w
}
return 80
}