From 523a2b776752b882bf666efc74661125e06de8a7 Mon Sep 17 00:00:00 2001 From: Christian Zunker <827818+czunker@users.noreply.github.com> Date: Mon, 18 Sep 2023 06:13:09 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Check=20args=20before=20calling?= =?UTF-8?q?=20a=20plugin=20(#1503)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thsi prevents panics wherre a arg is missing: ``` cnquery run vagrant -c "asset" ! using builtin provider for os → loaded configuration from /etc/opt/mondoo/mondoo.yml using source default panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x112b609] goroutine 1 [running]: go.mondoo.com/cnquery/providers/os/connection.(*VagrantConnection).Asset(0x40c72d?) :1 +0x9 go.mondoo.com/cnquery/providers/os/provider.(*Service).connect(0xc000308910, 0xc0006e3aa0, {0x1e546c0?, 0xc00009f600}) /home/christian/workspace/mondoo/github.com/cnquery/providers/os/provider/provider.go:187 +0x2b0 go.mondoo.com/cnquery/providers/os/provider.(*Service).Connect(0x1e3faa0?, 0xc0006e3aa0, {0x1e546c0?, 0xc00009f600?}) /home/christian/workspace/mondoo/github.com/cnquery/providers/os/provider/provider.go:129 +0x45 ... ``` With this PR, it looks like this: ``` cnquery run vagrant -c "asset" Error: accepts 1 arg(s), received 0 Usage: cnquery run vagrant host [flags] Flags: --ast Parse the query and return the abstract syntax tree (AST). -c, --command string MQL query to executed in the shell. --discover strings Enable the discovery of nested assets. Supports: all,auto -h, --help help for vagrant -j, --json Run the query and return the object in a JSON structure. --parse Parse the query and return the logical structure. --platform-id string Select a specific target asset by providing its platform ID. --record string Record all resouce calls and use resouces in the recording --use-recording string Use a recording to inject resouces data (read-only) Global Flags: --api-proxy string Set proxy for communications with Mondoo API --config string Set config file path (default $HOME/.config/mondoo/mondoo.yml) --log-level string Set log level: error, warn, info, debug, trace (default "info") -v, --verbose Enable verbose output accepts 1 arg(s), received 0 ``` Signed-off-by: Christian Zunker --- cli/providers/providers.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cli/providers/providers.go b/cli/providers/providers.go index d3f3426455..070eeb630e 100644 --- a/cli/providers/providers.go +++ b/cli/providers/providers.go @@ -149,6 +149,21 @@ func attachConnectorCmd(provider *plugin.Provider, connector *plugin.Connector, PreRun: cmd.Command.PreRun, } + if connector.MinArgs == connector.MaxArgs { + if connector.MinArgs == 0 { + res.Args = cobra.NoArgs + } else { + res.Args = cobra.ExactArgs(int(connector.MinArgs)) + } + } else { + if connector.MaxArgs > 0 && connector.MinArgs == 0 { + res.Args = cobra.MaximumNArgs(int(connector.MaxArgs)) + } else if connector.MaxArgs == 0 && connector.MinArgs > 0 { + res.Args = cobra.MinimumNArgs(int(connector.MinArgs)) + } else { + res.Args = cobra.RangeArgs(int(connector.MinArgs), int(connector.MaxArgs)) + } + } cmd.Command.Flags().VisitAll(func(flag *pflag.Flag) { res.Flags().AddFlag(flag) })