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.12.0
github.com/flashcatcloud/go-flashduty v0.13.1
github.com/mattn/go-runewidth v0.0.27
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.12.0 h1:2jjQsTB212XvwpRcX8W6uQ+Zjzmlu857T7YtTs5nH8Q=
github.com/flashcatcloud/go-flashduty v0.12.0/go.mod h1:aA0RtZEs0AYOwwdNKdtVeD8YMOdnmVY1zAlVD+9Ovx8=
github.com/flashcatcloud/go-flashduty v0.13.1 h1:6AzKyeaY+dvcVOod8DqOhzjA6ilK/tTTALGrjIW+RdY=
github.com/flashcatcloud/go-flashduty v0.13.1/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.27 h1:Feg/Oou5zI/wnpgDF6omIU0OokC9GxLC/WRknhVlIR0=
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,10 +822,16 @@ personal channels, or a template.`,
var notify flashduty.AddIncidentResponderRequestNotify
if followPreference || notifyChannel != "" || templateID != "" {
notify = flashduty.AddIncidentResponderRequestNotify{
FollowPreference: followPreference,
PersonalChannels: parseStringSlice(notifyChannel),
TemplateID: templateID,
}
// Explicit wire value whenever the intent is "use these channels"
// (channels given without the flag) or the flag was set explicitly.
// Template-only keeps the server default (nil = personal
// preference), so a template alone never suppresses delivery.
if cmd.Flags().Changed("follow-preference") || notifyChannel != "" {
notify.FollowPreference = &followPreference
}
}

return runCommand(cmd, args, func(ctx *RunContext) error {
Expand Down
62 changes: 58 additions & 4 deletions internal/cli/monit_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

func newMonitQueryCmd() *cobra.Command {
cmd := newGroupCmd("monit-query", "Probe monit-backed datasources (prometheus|victorialogs|loki|mysql)")
cmd := newGroupCmd("monit-query", "Probe monit-backed datasources (9 types via data; diagnose/rows support prometheus|victorialogs|loki|mysql)")
cmd.AddCommand(newMonitQueryDiagnoseCmd())
cmd.AddCommand(newMonitQueryDataCmd())
cmd.AddCommand(newMonitQueryRowsCmd())
return cmd
}
Expand Down Expand Up @@ -82,16 +83,69 @@ func newMonitQueryDiagnoseCmd() *cobra.Command {
return cmd
}

func newMonitQueryDataCmd() *cobra.Command {
var (
dsType, dsName, expr string
delaySeconds int64
argsKV []string
)

cmd := &cobra.Command{
Use: "data",
Short: "Structured datasource query (returns a stable query_result.v1: frames/records/samples)",
Long: curatedLong("Structured datasource query returning the stable query_result.v1 result — frames, records, or samples — instead of the legacy flattened rows.", "Diagnostics", "QueryData"),
RunE: func(cmd *cobra.Command, args []string) error {
if dsType == "" || dsName == "" || expr == "" {
return fmt.Errorf("--ds-type, --ds-name, --expr are required")
}
argsMap, err := parseKVSlice(argsKV)
if err != nil {
return fmt.Errorf("invalid --args: %w", err)
}
if err := normalizeRawTimeArgs(dsType, argsMap); err != nil {
return err
}

return runCommand(cmd, args, func(ctx *RunContext) error {
input := &flashduty.QueryDataRequest{
DsType: dsType,
DsName: dsName,
Expr: expr,
DelaySeconds: delaySeconds,
Args: argsMap,
}
result, _, err := ctx.Client.Diagnostics.QueryData(cmdContext(ctx.Cmd), input)
if err != nil {
return err
}
return ctx.Printer.Print(result, nil)
})
},
}

cmd.Flags().StringVar(&dsType, "ds-type", "", "Datasource type (required)")
cmd.Flags().StringVar(&dsName, "ds-name", "", "Datasource name as configured (required)")
registerEnumFlag(cmd, "ds-type", "prometheus", "victorialogs", "loki", "mysql", "sls", "elasticsearch", "postgres", "oracle", "clickhouse")
cmd.Flags().StringVar(&expr, "expr", "", "Query expression (required)")
cmd.Flags().Int64Var(&delaySeconds, "delay-seconds", 0, "Look-back offset in seconds for point-in-time queries (default 0)")
cmd.Flags().StringSliceVar(&argsKV, "args", nil, "Arg entries KEY=VALUE (repeatable; values must be strings per monit-query contract). "+
"For loki/victorialogs raw mode, <ds-type>.start/<ds-type>.end accept a relative duration ('15m'), 'now', a date/RFC3339 timestamp, "+
"or a unix epoch in seconds or milliseconds — normalized to the form the datasource requires before sending")

return cmd
}

func newMonitQueryRowsCmd() *cobra.Command {
var (
dsType, dsName, expr string
argsKV []string
)

cmd := &cobra.Command{
Use: "rows",
Short: "Raw datasource passthrough (returns values/rows as the datasource itself would)",
Long: curatedLong("Raw datasource passthrough returning values/rows as the datasource itself would.", "Diagnostics", "QueryRows"),
Use: "rows",
Short: "Raw datasource passthrough (returns values/rows as the datasource itself would). Deprecated — prefer 'monit-query data'",
Deprecated: "use 'monit-query data' instead",
Long: curatedLong("Deprecated. Raw datasource passthrough returning values/rows as the datasource itself would. Migrate to 'monit-query data', which preserves frames/records/samples without forcing results into legacy rows.", "Diagnostics", "QueryRows"),
RunE: func(cmd *cobra.Command, args []string) error {
if dsType == "" || dsName == "" || expr == "" {
return fmt.Errorf("--ds-type, --ds-name, --expr are required")
Expand Down
129 changes: 129 additions & 0 deletions internal/cli/monit_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func TestMonitQueryRowsFlags(t *testing.T) {
}
}

func TestMonitQueryDataFlags(t *testing.T) {
cmd := newMonitQueryDataCmd()
for _, name := range []string{"ds-type", "ds-name", "expr", "args", "delay-seconds"} {
if cmd.Flags().Lookup(name) == nil {
t.Errorf("flag --%s missing", name)
}
}
}

// --- monit-query diagnose -------------------------------------------------

func TestMonitQueryDiagnoseHappyPath(t *testing.T) {
Expand Down Expand Up @@ -202,6 +211,104 @@ func TestMonitQueryDiagnoseInvalidTimeStart(t *testing.T) {

// --- monit-query rows -----------------------------------------------------

func TestMonitQueryDataHappyPath(t *testing.T) {
saveAndResetGlobals(t)
stub := newGFStub(t)
// data returns the stable query_result.v1 envelope: data.{format,result}.
stub.data = map[string]any{
"format": "query_result.v1",
"result": map[string]any{
"kind": "samples",
"samples": []any{
map[string]any{"labels": map[string]any{"job": "api"}, "value": 1.25},
},
},
}

out, err := execCommand(
"monit-query", "data",
"--ds-type", "prometheus",
"--ds-name", "prom-prod",
"--expr", "up",
"--delay-seconds", "30",
"--args", "step=15s",
"--output-format", "json",
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if stub.lastPath != "/monit/query/data" {
t.Fatalf("expected /monit/query/data, got %q", stub.lastPath)
}
body := stub.lastBody
if body["ds_type"] != "prometheus" || body["ds_name"] != "prom-prod" || body["expr"] != "up" {
t.Errorf("unexpected data input: %#v", body)
}
if fmt.Sprint(body["delay_seconds"]) != "30" {
t.Errorf("expected delay_seconds 30, got %v", body["delay_seconds"])
}
args, _ := body["args"].(map[string]any)
if args["step"] != "15s" {
t.Errorf("expected args step=15s, got %#v", args)
}
var rendered map[string]any
if err := json.Unmarshal([]byte(out), &rendered); err != nil {
t.Fatalf("decode CLI JSON: %v\n%s", err, out)
}
if rendered["format"] != "query_result.v1" {
t.Errorf("expected format query_result.v1, got %v", rendered["format"])
}
}

func TestMonitQueryDataRequiredFlags(t *testing.T) {
cases := []struct {
name string
args []string
}{
{
name: "missing ds-type",
args: []string{
"monit-query", "data",
"--ds-name", "prom-prod",
"--expr", "up",
},
},
{
name: "missing ds-name",
args: []string{
"monit-query", "data",
"--ds-type", "prometheus",
"--expr", "up",
},
},
{
name: "missing expr",
args: []string{
"monit-query", "data",
"--ds-type", "prometheus",
"--ds-name", "prom-prod",
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
saveAndResetGlobals(t)
stub := newGFStub(t)

_, err := execCommand(tc.args...)
if err == nil {
t.Fatal("expected required-flag error, got nil")
}
if !strings.Contains(err.Error(), "required") {
t.Errorf("expected error to mention 'required', got %q", err.Error())
}
if stub.requests != 0 {
t.Errorf("data should not have been called: %d request(s)", stub.requests)
}
})
}
}

func TestMonitQueryRowsHappyPath(t *testing.T) {
saveAndResetGlobals(t)
stub := newGFStub(t)
Expand Down Expand Up @@ -406,6 +513,28 @@ func TestMonitQueryRowsRawModeNormalizesRFC3339(t *testing.T) {
}
}

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

_, err := execCommand(
"monit-query", "data",
"--ds-type", "prometheus",
"--ds-name", "prom-prod",
"--expr", "up",
"--args", "no-equals-sign",
)
if err == nil {
t.Fatal("expected error for malformed --args, got nil")
}
if !strings.Contains(err.Error(), "--args") {
t.Errorf("expected error to mention --args, got %q", err.Error())
}
if stub.requests != 0 {
t.Errorf("data should not have been called: %d request(s)", stub.requests)
}
}

func TestMonitQueryRowsInvalidArgs(t *testing.T) {
saveAndResetGlobals(t)
stub := newGFStub(t)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/zz_generated_account.go

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

22 changes: 11 additions & 11 deletions internal/cli/zz_generated_applications.go

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

Loading