Skip to content

Commit e688455

Browse files
authored
fix: refactor log processing for improved clarity (#1003)
1 parent 93c4daa commit e688455

File tree

1 file changed

+55
-43
lines changed

1 file changed

+55
-43
lines changed

internal/agent/TUIinstallProcessPage.go

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -72,54 +72,21 @@ func (p *installProcessPage) Init() tea.Cmd {
7272
time.Sleep(100 * time.Millisecond)
7373
buf := logBuffer.Bytes()
7474
if len(buf) > lastLen {
75-
newLogs := buf[lastLen:]
76-
lines := bytes.Split(newLogs, []byte("\n"))
77-
for _, line := range lines {
78-
strLine := string(line)
79-
if len(strLine) == 0 {
80-
continue
81-
}
82-
oldLog.Print(strLine)
83-
var logEntry map[string]interface{}
84-
msg := strLine
85-
if err := json.Unmarshal([]byte(strLine), &logEntry); err == nil {
86-
if m, ok := logEntry["message"].(string); ok {
87-
msg = m
88-
}
89-
if level, ok := logEntry["level"].(string); ok && (level == "error" || level == "fatal") {
90-
if !errorSent {
91-
p.errorMsg = msg
92-
errorSent = true
93-
}
94-
continue
95-
}
96-
}
97-
if strings.Contains(msg, AgentPartitionLog) {
98-
p.output <- StepPrefix + InstallPartitionStep
99-
} else if strings.Contains(msg, AgentBeforeInstallLog) {
100-
p.output <- StepPrefix + InstallBeforeInstallStep
101-
} else if strings.Contains(msg, AgentActiveLog) {
102-
p.output <- StepPrefix + InstallActiveStep
103-
} else if strings.Contains(msg, AgentBootloaderLog) {
104-
p.output <- StepPrefix + InstallBootloaderStep
105-
} else if strings.Contains(msg, AgentRecoveryLog) {
106-
p.output <- StepPrefix + InstallRecoveryStep
107-
} else if strings.Contains(msg, AgentPassiveLog) {
108-
p.output <- StepPrefix + InstallPassiveStep
109-
} else if strings.Contains(msg, AgentAfterInstallLog) && !strings.Contains(msg, "chroot") {
110-
p.output <- StepPrefix + InstallAfterInstallStep
111-
} else if strings.Contains(msg, AgentCompleteLog) {
112-
p.output <- StepPrefix + InstallCompleteStep
113-
}
114-
}
115-
lastLen = len(buf)
75+
lastLen = p.processLogLines(buf, lastLen, &errorSent, oldLog)
11676
}
11777
// Wait for installerDone before exiting
11878
select {
11979
case <-p.installerDone:
120-
if len(buf) == lastLen {
121-
break logLoop
80+
// Installer is done, but there may be unprocessed logs
81+
for {
82+
buf := logBuffer.Bytes()
83+
if len(buf) > lastLen {
84+
lastLen = p.processLogLines(buf, lastLen, &errorSent, oldLog)
85+
} else {
86+
break
87+
}
12288
}
89+
break logLoop
12390
default:
12491
}
12592
}
@@ -267,3 +234,48 @@ func (p *installProcessPage) Abort() {
267234
default:
268235
}
269236
}
237+
238+
// processLogLines processes new log lines from the buffer and updates the UI steps.
239+
func (p *installProcessPage) processLogLines(buf []byte, lastLen int, errorSent *bool, oldLog *types.KairosLogger) int {
240+
newLogs := buf[lastLen:]
241+
lines := bytes.Split(newLogs, []byte("\n"))
242+
for _, line := range lines {
243+
strLine := string(line)
244+
if len(strLine) == 0 {
245+
continue
246+
}
247+
oldLog.Print(strLine)
248+
var logEntry map[string]interface{}
249+
msg := strLine
250+
if err := json.Unmarshal([]byte(strLine), &logEntry); err == nil {
251+
if m, ok := logEntry["message"].(string); ok {
252+
msg = m
253+
}
254+
if level, ok := logEntry["level"].(string); ok && (level == "error" || level == "fatal") {
255+
if !*errorSent {
256+
p.errorMsg = msg
257+
*errorSent = true
258+
}
259+
continue
260+
}
261+
}
262+
if strings.Contains(msg, AgentPartitionLog) {
263+
p.output <- StepPrefix + InstallPartitionStep
264+
} else if strings.Contains(msg, AgentBeforeInstallLog) {
265+
p.output <- StepPrefix + InstallBeforeInstallStep
266+
} else if strings.Contains(msg, AgentActiveLog) {
267+
p.output <- StepPrefix + InstallActiveStep
268+
} else if strings.Contains(msg, AgentBootloaderLog) {
269+
p.output <- StepPrefix + InstallBootloaderStep
270+
} else if strings.Contains(msg, AgentRecoveryLog) {
271+
p.output <- StepPrefix + InstallRecoveryStep
272+
} else if strings.Contains(msg, AgentPassiveLog) {
273+
p.output <- StepPrefix + InstallPassiveStep
274+
} else if strings.Contains(msg, AgentAfterInstallLog) && !strings.Contains(msg, "chroot") {
275+
p.output <- StepPrefix + InstallAfterInstallStep
276+
} else if strings.Contains(msg, AgentCompleteLog) {
277+
p.output <- StepPrefix + InstallCompleteStep
278+
}
279+
}
280+
return len(buf)
281+
}

0 commit comments

Comments
 (0)