This commit is contained in:
marco 2025-05-06 15:14:52 +02:00
parent 531af79861
commit b87a5e56e6
8 changed files with 48 additions and 37 deletions

View file

@ -104,15 +104,15 @@ func (cli *cliAlerts) alertsToTable(alerts *models.GetAlertsResponse, printMachi
if *alerts == nil {
// avoid returning "null" in json
// could be cleaner if we used slice of alerts directly
fmt.Println("[]")
fmt.Fprintln(os.Stdout, "[]")
return nil
}
x, _ := json.MarshalIndent(alerts, "", " ")
fmt.Print(string(x))
fmt.Fprint(os.Stdout, string(x))
case "human":
if len(*alerts) == 0 {
fmt.Println("No active alerts")
fmt.Fprintln(os.Stdout, "No active alerts")
return nil
}
@ -156,7 +156,7 @@ func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) erro
alertDecisionsTable(color.Output, cfg.Cscli.Color, alert)
if len(alert.Meta) > 0 {
fmt.Printf("\n - Context :\n")
fmt.Fprintf(os.Stdout, "\n - Context :\n")
sort.Slice(alert.Meta, func(i, j int) bool {
return alert.Meta[i].Key < alert.Meta[j].Key
})
@ -183,7 +183,7 @@ func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) erro
}
if withDetail {
fmt.Printf("\n - Events :\n")
fmt.Fprintf(os.Stdout, "\n - Events :\n")
for _, event := range alert.Events {
alertEventTable(color.Output, cfg.Cscli.Color, event)
@ -451,14 +451,14 @@ func (cli *cliAlerts) inspect(ctx context.Context, details bool, alertIDs ...str
return fmt.Errorf("unable to serialize alert with id %s: %w", alertID, err)
}
fmt.Printf("%s\n", string(data))
fmt.Fprintln(os.Stdout, string(data))
case "raw":
data, err := yaml.Marshal(alert)
if err != nil {
return fmt.Errorf("unable to serialize alert with id %s: %w", alertID, err)
}
fmt.Println(string(data))
fmt.Fprintln(os.Stdout, string(data))
}
}

View file

@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/url"
"os"
"slices"
"strconv"
"strings"
@ -283,7 +284,7 @@ func (cli *cliAllowLists) create(ctx context.Context, db *database.Client, name
return err
}
fmt.Printf("allowlist '%s' created successfully\n", name)
fmt.Fprintf(os.Stdout, "allowlist '%s' created successfully\n", name)
return nil
}
@ -392,7 +393,7 @@ func (cli *cliAllowLists) delete(ctx context.Context, db *database.Client, name
return err
}
fmt.Printf("allowlist '%s' deleted successfully\n", name)
fmt.Fprintf(os.Stdout, "allowlist '%s' deleted successfully\n", name)
return nil
}
@ -475,7 +476,7 @@ func (cli *cliAllowLists) add(ctx context.Context, db *database.Client, name str
}
if len(toAdd) == 0 {
fmt.Println("no new values for allowlist")
fmt.Fprintln(os.Stdout, "no new values for allowlist")
return nil
}
@ -485,7 +486,7 @@ func (cli *cliAllowLists) add(ctx context.Context, db *database.Client, name str
}
if added > 0 {
fmt.Printf("added %d values to allowlist %s\n", added, name)
fmt.Fprintf(os.Stdout, "added %d values to allowlist %s\n", added, name)
}
return nil
@ -614,7 +615,7 @@ func (cli *cliAllowLists) remove(ctx context.Context, db *database.Client, name
}
if len(toRemove) == 0 {
fmt.Println("no value to remove from allowlist")
fmt.Fprintln(os.Stdout, "no value to remove from allowlist")
return nil
}
@ -624,7 +625,7 @@ func (cli *cliAllowLists) remove(ctx context.Context, db *database.Client, name
}
if deleted > 0 {
fmt.Printf("removed %d values from allowlist %s", deleted, name)
fmt.Fprintf(os.Stdout, "removed %d values from allowlist %s", deleted, name)
}
return nil

View file

@ -103,22 +103,22 @@ func (cli *cliDecisions) decisionsToTable(alerts *models.GetAlertsResponse, prin
if *alerts == nil {
// avoid returning "null" in `json"
// could be cleaner if we used slice of alerts directly
fmt.Println("[]")
fmt.Fprintln(os.Stdout, "[]")
return nil
}
x, _ := json.MarshalIndent(alerts, "", " ")
fmt.Printf("%s", string(x))
fmt.Fprintln(os.Stdout, string(x))
case "human":
if len(*alerts) == 0 {
fmt.Println("No active decisions")
fmt.Fprintln(os.Stdout, "No active decisions")
return nil
}
cli.decisionsTable(color.Output, alerts, printMachine)
if skipped > 0 {
fmt.Printf("%d duplicated entries skipped\n", skipped)
fmt.Fprintf(os.Stdout, "%d duplicated entries skipped\n", skipped)
}
}

View file

@ -18,6 +18,7 @@ import (
func TestAlertsListAsMachine(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setup()
@ -199,6 +200,7 @@ func TestAlertsListAsMachine(t *testing.T) {
func TestAlertsGetAsMachine(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setup()
@ -367,6 +369,7 @@ func TestAlertsGetAsMachine(t *testing.T) {
func TestAlertsCreateAsMachine(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setup()
@ -410,6 +413,7 @@ func TestAlertsCreateAsMachine(t *testing.T) {
func TestAlertsDeleteAsMachine(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setup()

View file

@ -14,6 +14,7 @@ import (
func TestApiAuth(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.TraceLevel)
mux, urlx, teardown := setup()

View file

@ -19,6 +19,7 @@ import (
func TestDecisionsList(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setup()
@ -78,6 +79,7 @@ func TestDecisionsList(t *testing.T) {
func TestDecisionsStream(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setup()
@ -154,6 +156,7 @@ func TestDecisionsStream(t *testing.T) {
func TestDecisionsStreamV3Compatibility(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setupWithPrefix("v3")
@ -222,6 +225,7 @@ func TestDecisionsStreamV3Compatibility(t *testing.T) {
func TestDecisionsStreamV3(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setupWithPrefix("v3")
@ -295,6 +299,7 @@ func TestDecisionsStreamV3(t *testing.T) {
func TestDecisionsFromBlocklist(t *testing.T) {
ctx := t.Context()
log.SetLevel(log.DebugLevel)
mux, urlx, teardown := setupWithPrefix("v3")