Files
narratio/internal/app/command_reporting.go

49 lines
1.3 KiB
Go
Raw Normal View History

package app
import (
"fmt"
"io"
"go.maximumdirect.net/commandkit"
)
// Native reporting retains its own delivery failures until invocation finalization.
// Console delivery failures must not interrupt domain work or checkpoint saves.
type nativeConsole struct{ reporter *commandkit.Reporter }
func (r nativeConsole) Info(message string, fields ...any) {
_ = r.reporter.Info(message, consoleFields(fields))
}
func (r nativeConsole) Warn(message string, fields ...any) {
_ = r.reporter.Warn(message, consoleFields(fields))
}
func (r nativeConsole) Debug(message string, fields ...any) {
_ = r.reporter.Debug(message, consoleFields(fields))
}
func (r nativeConsole) Progress(message string, fields ...any) {
_ = r.reporter.Progress(message, consoleFields(fields))
}
func consoleFields(fields []any) commandkit.Detail {
if len(fields) == 0 {
return nil
}
return func(w io.Writer) error {
for index := 0; index < len(fields); index += 2 {
value := any("")
if index+1 < len(fields) {
value = fields[index+1]
}
if _, err := fmt.Fprintf(w, "%v=%v\n", fields[index], value); err != nil {
return err
}
}
return nil
}
}
func withCommandReporting(deps commandDependencies, invocation commandkit.Invocation) commandDependencies {
deps.reporter = nativeConsole{reporter: invocation.Reporter()}
return deps
}