Debian Codegraph @

cmd/journalctl_mock/journalctl_mock.go

raw
1 package main
2
3 import (
4 "bufio"
5 "errors"
6 "fmt"
7 "os"
8 "os/signal"
9 "strings"
10 "syscall"
11 "time"
12
13 "github.com/spf13/pflag"
14 )
15
16 type LogEntry struct {
17 Timestamp time.Time
18 Text string
19 }
20
21 // Parses the timestamp from the start of the line and returns it along with the rest of the message.
22 func parseLogLine(line string) (*LogEntry, error) {
23 splitIndex := strings.Index(line, " ")
24 if splitIndex == -1 {
25 return nil, errors.New("invalid log line: no space found")
26 }
27
28 timestampStr := line[:splitIndex]
29 //rest := line[splitIndex+1:]
30
31 timestamp, err := time.Parse(time.RFC3339Nano, timestampStr)
32 if err != nil {
33 return nil, fmt.Errorf("invalid timestamp format: %w", err)
34 }
35
36 return &LogEntry{Timestamp: timestamp, Text: line}, nil
37 }
38
39 func loadLogEntries(path string) ([]LogEntry, error) {
40 file, err := os.Open(path)
41 if err != nil {
42 return nil, err
43 }
44 defer file.Close()
45
46 var entries []LogEntry
47 var current *LogEntry
48
49 scanner := bufio.NewScanner(file)
50 for scanner.Scan() {
51 line := scanner.Text()
52 if len(line) >= 20 && line[4] == '-' && line[7] == '-' && line[10] == 'T' {
53 // New entry
54 entry, err := parseLogLine(line)
55 if err != nil {
56 return nil, err
57 }
58 if current != nil {
59 entries = append(entries, *current)
60 }
61 current = entry
62 } else if current != nil {
63 // Multiline continuation
64 current.Text += "\n" + line
65 }
66 }
67 if current != nil {
68 entries = append(entries, *current)
69 }
70
71 if err := scanner.Err(); err != nil {
72 return nil, err
73 }
74
75 return entries, nil
76 }
77
78 func main() {
79 sigs := make(chan os.Signal, 1)
80
81 // Notify for SIGPIPE
82 signal.Notify(sigs, syscall.SIGPIPE)
83
84 go func() {
85 for sig := range sigs {
86 if sig == syscall.SIGPIPE {
87 // Exit silently with 141
88 os.Exit(128 + int(syscall.SIGPIPE))
89 }
90 }
91 }()
92
93 var (
94 output string
95 quiet bool
96 since string
97 until string
98 reverse bool
99 numLines int
100 )
101
102 pflag.StringVar(&output, "output", "", "Set output format")
103 pflag.BoolVar(&quiet, "quiet", false, "Suppress extra output")
104 pflag.StringVar(&since, "since", "", "Show entries not older than the specified time")
105 pflag.StringVar(&until, "until", "", "Show entries not newer than the specified time")
106 pflag.BoolVar(&reverse, "reverse", false, "Show newest entries first")
107 pflag.IntVarP(&numLines, "lines", "n", -1, "Max number of lines to print")
108 pflag.Parse()
109
110 if output != "short-iso-precise" {
111 fmt.Fprintln(os.Stderr, "Error: --output=short-iso-precise is required")
112 os.Exit(1)
113 }
114
115 if !quiet {
116 fmt.Fprintln(os.Stderr, "Error: --quiet is required")
117 os.Exit(1)
118 }
119
120 logPath := os.Getenv("NERDLOG_JOURNALCTL_MOCK_DATA")
121 if logPath == "" {
122 fmt.Fprintln(os.Stderr, "Error: NERDLOG_JOURNALCTL_MOCK_DATA not set")
123 os.Exit(1)
124 }
125
126 entries, err := loadLogEntries(logPath)
127 if err != nil {
128 fmt.Fprintf(os.Stderr, "Error loading log data: %v\n", err)
129 os.Exit(1)
130 }
131
132 var sinceTime, untilTime time.Time
133 if since != "" {
134 sinceTime, err = parseJournalctlTimeArg(since)
135 if err != nil {
136 fmt.Fprintf(os.Stderr, "Invalid --since time: %v\n", err)
137 os.Exit(1)
138 }
139 }
140 if until != "" {
141 untilTime, err = parseJournalctlTimeArg(until)
142 if err != nil {
143 fmt.Fprintf(os.Stderr, "Invalid --until time: %v\n", err)
144 os.Exit(1)
145 }
146 }
147
148 // Filter by time
149 var filtered []LogEntry
150 for _, e := range entries {
151 if !sinceTime.IsZero() && e.Timestamp.Before(sinceTime) {
152 continue
153 }
154 if !untilTime.IsZero() && e.Timestamp.After(untilTime) {
155 continue
156 }
157 filtered = append(filtered, e)
158 }
159
160 // Reverse if needed
161 if reverse {
162 for i, j := 0, len(filtered)-1; i < j; i, j = i+1, j-1 {
163 filtered[i], filtered[j] = filtered[j], filtered[i]
164 }
165 }
166
167 // Print
168 for i, e := range filtered {
169 if numLines >= 0 && i >= numLines {
170 break
171 }
172
173 fmt.Println(e.Text)
174 }
175 }
176
177 // parseJournalctlTimeArg parses one of the two possible time formats:
178 //
179 // "2006-01-02 15:04:00"
180 // "2006-01-02 15:04:00.000000"
181 func parseJournalctlTimeArg(s string) (time.Time, error) {
182 const layout1 = "2006-01-02 15:04:05"
183
184 if t, err := time.Parse(layout1, s); err == nil {
185 return t, nil
186 }
187
188 return time.Time{}, fmt.Errorf("invalid time format: %q", s)
189 }