Files
Deployr/internal/logx/logx.go
T
ClaudeandClaude Opus 5 4909edee2d Deployr 0.1.0: cross-platform application deployment CLI
Replaces Ninite for endpoint software: one catalog, one command line, and the
same behaviour on Windows, macOS and Linux. Single static binary, no server and
no agent, driven from an RMM script runner or by hand.

- catalog of ~50 apps compiled into the binary, extendable with YAML overlays
  in catalog.d or via --catalog, no rebuild required
- providers: winget, choco, brew, cask, apt, dnf/yum, zypper, pacman, flatpak,
  snap, plus direct vendor downloads (msi/exe/msu/dmg/pkg/deb/rpm/zip/tar.gz/
  AppImage)
- sources are tried in order and fall through, so one catalog entry covers
  winget on Windows, a cask on macOS and the vendor .deb on Debian
- provider-independent detection (registry, paths, commands) makes runs
  idempotent even for software installed by hand
- unattended by construction: stdin closed, silent flags, per-command timeouts
- handles the traps that break naive wrappers: winget missing under SYSTEM,
  Homebrew refusing root, 1641/3010 reboot exits, "already installed" reported
  as failure
- --json summary on stdout with progress on stderr, and RMM-friendly exit codes
  (0 ok, 1 failed, 2 usage, 3 privileges)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 16:55:13 -05:00

125 lines
3.1 KiB
Go

// Package logx is a tiny leveled logger that writes human-readable lines to a
// stream and, optionally, tees everything to a log file. Output is deliberately
// plain ASCII so it survives RMM script-output capture and legacy consoles.
package logx
import (
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
)
// Level controls verbosity.
type Level int
const (
// LevelDebug logs every command that is executed.
LevelDebug Level = iota
// LevelInfo is the default.
LevelInfo
// LevelWarn hides progress chatter.
LevelWarn
// LevelError only reports failures.
LevelError
)
// Logger writes to a console stream and an optional file.
type Logger struct {
mu sync.Mutex
console io.Writer
file *os.File
level Level
}
// New returns a Logger writing to w at the given level.
func New(w io.Writer, level Level) *Logger {
if w == nil {
w = io.Discard
}
return &Logger{console: w, level: level}
}
// AttachFile starts teeing output to path, creating parent directories.
// A failure here is never fatal: logging to a file is a convenience, and an
// endpoint with a read-only or missing state directory must still be able to
// install software.
func (l *Logger) AttachFile(path string) error {
if path == "" {
return nil
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
return err
}
l.mu.Lock()
l.file = f
l.mu.Unlock()
l.writeFileOnly(fmt.Sprintf("\n=== deployr run started %s ===\n", time.Now().Format(time.RFC3339)))
return nil
}
// Close flushes and closes the log file, if any.
func (l *Logger) Close() {
l.mu.Lock()
defer l.mu.Unlock()
if l.file != nil {
_ = l.file.Close()
l.file = nil
}
}
// SetLevel changes verbosity at runtime.
func (l *Logger) SetLevel(level Level) {
l.mu.Lock()
l.level = level
l.mu.Unlock()
}
func (l *Logger) writeFileOnly(s string) {
l.mu.Lock()
defer l.mu.Unlock()
if l.file != nil {
_, _ = io.WriteString(l.file, s)
}
}
func (l *Logger) emit(level Level, prefix, format string, args ...any) {
msg := fmt.Sprintf(format, args...)
line := msg + "\n"
if prefix != "" {
line = prefix + " " + line
}
l.mu.Lock()
defer l.mu.Unlock()
if l.file != nil {
_, _ = io.WriteString(l.file, time.Now().Format("15:04:05")+" "+line)
}
if level >= l.level {
_, _ = io.WriteString(l.console, line)
}
}
// Debugf logs command-level detail.
func (l *Logger) Debugf(format string, args ...any) { l.emit(LevelDebug, " |", format, args...) }
// Infof logs normal progress.
func (l *Logger) Infof(format string, args ...any) { l.emit(LevelInfo, "", format, args...) }
// Warnf logs a recoverable problem.
func (l *Logger) Warnf(format string, args ...any) { l.emit(LevelWarn, "warn:", format, args...) }
// Errorf logs a failure.
func (l *Logger) Errorf(format string, args ...any) { l.emit(LevelError, "error:", format, args...) }
// Resultf logs a per-app outcome line, e.g. "[ok] chrome installed via winget".
func (l *Logger) Resultf(status, format string, args ...any) {
l.emit(LevelWarn, fmt.Sprintf("[%-4s]", status), format, args...)
}