update: enhance command execution with real-time logging and add shorthand flag for deployment file
This commit is contained in:
parent
4b8a27a2d7
commit
5f448d7fc7
3 changed files with 90 additions and 8 deletions
46
main.go
46
main.go
|
|
@ -14,6 +14,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
|
|
@ -23,11 +24,54 @@ import (
|
|||
"headshed/infctl-cli/config"
|
||||
)
|
||||
|
||||
type customMessageOnlyHandler struct {
|
||||
output *os.File
|
||||
}
|
||||
|
||||
func (h *customMessageOnlyHandler) Enabled(_ context.Context, _ slog.Level) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *customMessageOnlyHandler) Handle(_ context.Context, r slog.Record) error {
|
||||
// Directly retrieve the message from the record
|
||||
msg := r.Message
|
||||
if msg != "" {
|
||||
_, err := fmt.Fprintln(h.output, msg)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *customMessageOnlyHandler) WithAttrs(_ []slog.Attr) slog.Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *customMessageOnlyHandler) WithGroup(_ string) slog.Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var levelVar slog.LevelVar
|
||||
levelVar.Set(slog.LevelDebug)
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: &levelVar}))
|
||||
|
||||
var logger *slog.Logger
|
||||
if os.Getenv("LOG_FORMAT") == "basic" {
|
||||
logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey {
|
||||
return slog.Attr{}
|
||||
}
|
||||
return a
|
||||
},
|
||||
}))
|
||||
|
||||
} else if os.Getenv("LOG_FORMAT") == "none" {
|
||||
logger = slog.New(&customMessageOnlyHandler{output: os.Stdout})
|
||||
|
||||
} else {
|
||||
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: &levelVar}))
|
||||
}
|
||||
|
||||
slog.SetDefault(logger)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue