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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/flashcatcloud/flashduty-cli
go 1.25.1

require (
github.com/flashcatcloud/go-flashduty v0.5.4
github.com/flashcatcloud/go-flashduty v0.5.5-0.20260703065853-f90c46dd6441
github.com/mattn/go-runewidth v0.0.24
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/clipperhouse/uax29/v2 v2.2.0 h1:ChwIKnQN3kcZteTXMgb1wztSgaU+ZemkgWdohwgs8tY=
github.com/clipperhouse/uax29/v2 v2.2.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/flashcatcloud/go-flashduty v0.5.4 h1:rCVaB88wrBJWYb8B63bZtjcnojiNDuH0d7GuIYnheq0=
github.com/flashcatcloud/go-flashduty v0.5.4/go.mod h1:aA0RtZEs0AYOwwdNKdtVeD8YMOdnmVY1zAlVD+9Ovx8=
github.com/flashcatcloud/go-flashduty v0.5.5-0.20260703065853-f90c46dd6441 h1:N84LGiMMOlKG3Euq0Sp9bAhWkohM0XBpe3ebjAe1+SM=
github.com/flashcatcloud/go-flashduty v0.5.5-0.20260703065853-f90c46dd6441/go.mod h1:aA0RtZEs0AYOwwdNKdtVeD8YMOdnmVY1zAlVD+9Ovx8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-runewidth v0.0.24 h1:cpokDiIn0MGnhdHwuWnJBITySJ20QyNGnY2kR/ay2DU=
Expand Down
34 changes: 34 additions & 0 deletions internal/cli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ func requireExactArg(name string) cobra.PositionalArgs {
}
}

// requireBodyFieldOrArgs validates a required variadic positional that folds
// into a request-body field. The same field can also come from --data or from
// its typed flag, so zero positional args are valid when either source is set.
func requireBodyFieldOrArgs(name, flagName string) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
if len(args) > 0 || bodyFieldSourceSet(cmd, flagName) {
return nil
}
return fmt.Errorf("missing %s. Usage: %s", name, cmd.UseLine())
}
}

// requireBodyFieldOrExactArg validates a required scalar positional that folds
// into a request-body field. The value may instead come from --data or from the
// typed flag, but extra positionals are still rejected.
func requireBodyFieldOrExactArg(name, flagName string) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
switch {
case len(args) == 1:
return nil
case len(args) > 1:
return fmt.Errorf("expects exactly one %s. Usage: %s", name, cmd.UseLine())
case bodyFieldSourceSet(cmd, flagName):
return nil
default:
return fmt.Errorf("missing %s. Usage: %s", name, cmd.UseLine())
}
}
}

func bodyFieldSourceSet(cmd *cobra.Command, flagName string) bool {
return cmd.Flags().Changed("data") || cmd.Flags().Changed(flagName)
}

// optionalArg returns a positional argument validator that accepts zero or one
// argument named name. It backs generated commands whose positional folds into
// an OPTIONAL body field because the operation also accepts an alternative
Expand Down
52 changes: 52 additions & 0 deletions internal/cli/gen_positional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,58 @@ func TestGenPositionalFlagOverridesPositional(t *testing.T) {
}
}

func TestGenPositionalRequiredFieldCanComeFromDataOrFlag(t *testing.T) {
saveAndResetGlobals(t)
stub := newGFStub(t)

if _, err := execCommand("safari", "automation-rule-run", "--data", `{"rule_id":"auto_1"}`); err != nil {
t.Fatalf("automation-rule-run --data: %v", err)
}
if stub.lastPath != "/safari/automation/rule/run" {
t.Fatalf("path = %q, want /safari/automation/rule/run", stub.lastPath)
}
if got := stub.lastBody["rule_id"]; got != "auto_1" {
t.Errorf("rule_id from --data = %#v, want auto_1", got)
}

if _, err := execCommand("safari", "automation-rule-run", "--rule-id", "auto_2"); err != nil {
t.Fatalf("automation-rule-run --rule-id: %v", err)
}
if got := stub.lastBody["rule_id"]; got != "auto_2" {
t.Errorf("rule_id from --rule-id = %#v, want auto_2", got)
}

if _, err := execCommand("incident-trigger-subscription", "upsert", "--data", `{"channel_ids":[2468013579],"consumer":"fc_safari","consumer_ref":"auto_1","severities":["Critical"],"source":"ai_sre_automation"}`); err != nil {
t.Fatalf("incident-trigger-subscription upsert --data: %v", err)
}
if stub.lastPath != "/incident-trigger-subscription/upsert" {
t.Fatalf("path = %q, want /incident-trigger-subscription/upsert", stub.lastPath)
}
raw, ok := stub.lastBody["channel_ids"].([]any)
if !ok || len(raw) != 1 || raw[0] != float64(2468013579) {
t.Errorf("channel_ids from --data = %#v, want [2468013579]", stub.lastBody["channel_ids"])
}

if _, err := execCommand("incident-trigger-subscription", "upsert", "--channel-ids", "2468013580", "--consumer", "fc_safari", "--consumer-ref", "auto_2", "--severities", "Warning", "--source", "ai_sre_automation"); err != nil {
t.Fatalf("incident-trigger-subscription upsert --channel-ids: %v", err)
}
raw, ok = stub.lastBody["channel_ids"].([]any)
if !ok || len(raw) != 1 || raw[0] != float64(2468013580) {
t.Errorf("channel_ids from --channel-ids = %#v, want [2468013580]", stub.lastBody["channel_ids"])
}

if _, err := execCommand("incident-trigger-subscription", "upsert", "--channel-ids", "2468013581", "--consumer", "fc_safari", "--consumer-ref", "auto_3", "--severities", "Warning", "--source", "ai_sre_automation", "--enabled=false"); err != nil {
t.Fatalf("incident-trigger-subscription upsert --enabled=false: %v", err)
}
if got, ok := stub.lastBody["enabled"].(bool); !ok || got {
t.Errorf("enabled = %#v, want explicit false", stub.lastBody["enabled"])
}

if _, err := execCommand("safari", "automation-rule-run"); err == nil || !strings.Contains(err.Error(), "missing rule_id") {
t.Fatalf("automation-rule-run without arg/data/flag error = %v, want missing rule_id", err)
}
}

// TestGenFoldPositional unit-tests the runtime helper across all three kinds.
func TestGenFoldPositional(t *testing.T) {
// string scalar → args[0]
Expand Down
10 changes: 5 additions & 5 deletions internal/cli/zz_generated_a2a_agents.go

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

34 changes: 17 additions & 17 deletions internal/cli/zz_generated_alert_enrichment.go

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

Loading
Loading