Files
plainleaf/cli/logs.go
T
Zef Hemel b5bdab5d1a CLI work
* Replaced initial node.js prototype of CLI with proper Go
  implementation
* Added code signing for CLI + server binaries for macOS
2026-03-29 16:48:39 +02:00

57 lines
1.3 KiB
Go

package cli
import (
"fmt"
"time"
"github.com/spf13/cobra"
)
func LogsCommand() *cobra.Command {
var lines int
var follow bool
cmd := &cobra.Command{
Use: "logs",
Short: "Show console logs from the headless client",
RunE: func(cmd *cobra.Command, args []string) error {
conn, err := connFromFlags(cmd)
if err != nil {
return err
}
logs, err := conn.Logs(lines, 0)
if err != nil {
return err
}
var lastTimestamp int64
for _, entry := range logs {
ts := time.UnixMilli(entry.Timestamp).UTC().Format(time.RFC3339)
fmt.Printf("%s [%s] %s\n", ts, entry.Level, entry.Text)
if entry.Timestamp > lastTimestamp {
lastTimestamp = entry.Timestamp
}
}
if follow {
for {
time.Sleep(500 * time.Millisecond)
newLogs, err := conn.Logs(0, lastTimestamp)
if err != nil {
return err
}
for _, entry := range newLogs {
ts := time.UnixMilli(entry.Timestamp).UTC().Format(time.RFC3339)
fmt.Printf("%s [%s] %s\n", ts, entry.Level, entry.Text)
if entry.Timestamp > lastTimestamp {
lastTimestamp = entry.Timestamp
}
}
}
}
return nil
},
}
cmd.Flags().IntVarP(&lines, "lines", "n", 100, "Number of log entries to show")
cmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow log output")
return cmd
}