Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/cli/gen_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"

"github.com/spf13/cobra"

"github.com/flashcatcloud/flashduty-cli/internal/timeutil"
)

// stdinReader is the source read when --data is exactly "-". A package var so
Expand Down Expand Up @@ -174,6 +176,22 @@ func printGenericResult(ctx *RunContext, data any) error {
return renderGenericTable(ctx, data)
}

// genParseTimeFlag parses a relative-or-absolute time flag into unix seconds,
// mirroring the curated incident-list --since/--until handling: a Go duration
// ("7d", "24h") is "now minus duration", "+7d" is the future, "now" is now, and
// a date/datetime/RFC3339/Unix-seconds value passes through. ok is false when the
// flag was not set, so the caller omits the field from the request body.
func genParseTimeFlag(cmd *cobra.Command, name, raw string) (val int64, ok bool, err error) {
if !cmd.Flags().Changed(name) {
return 0, false, nil
}
v, err := timeutil.Parse(raw)
if err != nil {
return 0, false, fmt.Errorf("invalid --%s: %w", name, err)
}
return v, true, nil
}

// genGroup finds an existing subcommand named `name` under parent, or creates a
// group command with that name. This lets generated commands attach to the same
// group a curated command already owns (partial-coverage services) and lets a
Expand Down
51 changes: 51 additions & 0 deletions internal/cli/gen_time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cli

import (
"testing"
"time"

"github.com/spf13/cobra"
)

func newTimeFlagCmd(t *testing.T, set bool, raw string) (*cobra.Command, string) {
t.Helper()
cmd := &cobra.Command{Use: "x"}
var v string
cmd.Flags().StringVar(&v, "start-time", "", "")
if set {
if err := cmd.Flags().Set("start-time", raw); err != nil {
t.Fatalf("set flag: %v", err)
}
}
return cmd, v
}

func TestGenParseTimeFlag(t *testing.T) {
// Unset flag → ok=false so the caller omits the field.
cmd, raw := newTimeFlagCmd(t, false, "")
if v, ok, err := genParseTimeFlag(cmd, "start-time", raw); err != nil || ok || v != 0 {
t.Errorf("unset: got (%d,%v,%v), want (0,false,nil)", v, ok, err)
}

// Relative duration → roughly now minus the duration.
cmd, raw = newTimeFlagCmd(t, true, "24h")
v, ok, err := genParseTimeFlag(cmd, "start-time", raw)
if err != nil || !ok {
t.Fatalf("24h: ok=%v err=%v", ok, err)
}
if delta := time.Now().Unix() - 24*3600 - v; delta < -5 || delta > 5 {
t.Errorf("24h: parsed %d not ~24h ago (delta %ds)", v, delta)
}

// Raw unix seconds pass through unchanged (back-compat with old int flag).
cmd, raw = newTimeFlagCmd(t, true, "1700000000")
if v, ok, err := genParseTimeFlag(cmd, "start-time", raw); err != nil || !ok || v != 1700000000 {
t.Errorf("unix passthrough: got (%d,%v,%v), want (1700000000,true,nil)", v, ok, err)
}

// Invalid value → error mentioning the flag.
cmd, raw = newTimeFlagCmd(t, true, "not-a-time")
if _, ok, err := genParseTimeFlag(cmd, "start-time", raw); err == nil || ok {
t.Errorf("invalid: expected error, got ok=%v err=%v", ok, err)
}
}
56 changes: 36 additions & 20 deletions internal/cli/zz_generated_alerts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading