From dcb693bb741a4996475cf89f5d685bb90ecbdfc3 Mon Sep 17 00:00:00 2001 From: kai Date: Mon, 7 Nov 2022 16:17:44 +0000 Subject: [PATCH] Show error if export flag set for interactive query. Closes #2696 Fail with helpful message if invalid typename passed to check command. Closes #2693 --- cmd/query.go | 3 +++ pkg/control/controldisplay/snapshot.go | 6 +++++- pkg/control/controlexecute/execution_tree.go | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/query.go b/cmd/query.go index 1ceda853f..0f141f7bd 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -153,6 +153,9 @@ func validateQueryArgs(ctx context.Context, args []string) error { if interactiveMode && (viper.IsSet(constants.ArgSnapshot) || viper.IsSet(constants.ArgShare)) { return fmt.Errorf("cannot share snapshots in interactive mode") } + if len(viper.GetStringSlice(constants.ArgExport)) > 0 { + return fmt.Errorf("cannot export query results in interactive mode") + } // if share or snapshot args are set, there must be a query specified err := cmdconfig.ValidateSnapshotArgs(ctx) if err != nil { diff --git a/pkg/control/controldisplay/snapshot.go b/pkg/control/controldisplay/snapshot.go index 974d0f399..9f4f4b47d 100644 --- a/pkg/control/controldisplay/snapshot.go +++ b/pkg/control/controldisplay/snapshot.go @@ -21,7 +21,11 @@ func executionTreeToSnapshot(e *controlexecute.ExecutionTree) (*dashboardtypes.S // get root benchmark/control switch root := e.Root.Children[0].(type) { case *controlexecute.ResultGroup: - dashboardNode = root.GroupItem.(modconfig.DashboardLeafNode) + var ok bool + dashboardNode, ok = root.GroupItem.(modconfig.DashboardLeafNode) + if !ok { + return nil, fmt.Errorf("invalid node found in control execution tree - cannot cast '%s' to a DashboardLeafNode", root.GroupItem.Name()) + } nodeType = "benchmark" case *controlexecute.ControlRun: dashboardNode = root.Control diff --git a/pkg/control/controlexecute/execution_tree.go b/pkg/control/controlexecute/execution_tree.go index 16d662b51..5b67b8ffc 100644 --- a/pkg/control/controlexecute/execution_tree.go +++ b/pkg/control/controlexecute/execution_tree.go @@ -3,6 +3,7 @@ package controlexecute import ( "context" "fmt" + "github.com/turbot/go-kit/helpers" "log" "sort" "time" @@ -194,6 +195,10 @@ func (e *ExecutionTree) getExecutionRootFromArg(arg string) (modconfig.ModTreeIt if !found || !ok { return nil, fmt.Errorf("no resources found matching argument '%s'", arg) } + // root item must be either a benchmark or a control + if !helpers.StringSliceContains([]string{modconfig.BlockTypeControl, modconfig.BlockTypeBenchmark}, root.BlockType()) { + return nil, fmt.Errorf("cannot execute '%s' using check, only controls and benchmarks may be run", resource.Name()) + } return root, nil }