go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/apps/cnquery/cmd/run.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package cmd
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/rs/zerolog/log"
    10  	"github.com/spf13/cobra"
    11  	"github.com/spf13/viper"
    12  	"go.mondoo.com/cnquery/providers"
    13  	"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
    14  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    15  	"go.mondoo.com/cnquery/shared"
    16  	"go.mondoo.com/cnquery/shared/proto"
    17  )
    18  
    19  func init() {
    20  	rootCmd.AddCommand(RunCmd)
    21  
    22  	RunCmd.Flags().StringP("command", "c", "", "MQL query to executed in the shell.")
    23  	RunCmd.Flags().Bool("parse", false, "Parse the query and return the logical structure.")
    24  	RunCmd.Flags().Bool("ast", false, "Parse the query and return the abstract syntax tree (AST).")
    25  	RunCmd.Flags().BoolP("json", "j", false, "Run the query and return the object in a JSON structure.")
    26  	RunCmd.Flags().String("platform-id", "", "Select a specific target asset by providing its platform ID.")
    27  
    28  	RunCmd.Flags().String("llx", "", "Generate the executable code bundle and save it to the specified file.")
    29  	RunCmd.Flags().MarkHidden("llx")
    30  	RunCmd.Flags().String("use-llx", "", "Run the code specified in the code bundle on disk")
    31  	RunCmd.Flags().MarkHidden("use-llx")
    32  }
    33  
    34  var RunCmd = &cobra.Command{
    35  	Use:   "run",
    36  	Short: "Run an MQL query.",
    37  	Long:  `Run an MQL query on the CLI and displays its results.`,
    38  	PreRun: func(cmd *cobra.Command, args []string) {
    39  		viper.BindPFlag("platform-id", cmd.Flags().Lookup("platform-id"))
    40  	},
    41  	// we have to initialize an empty run so it shows up as a runnable command in --help
    42  	Run: func(cmd *cobra.Command, args []string) {},
    43  }
    44  
    45  var RunCmdRun = func(cmd *cobra.Command, runtime *providers.Runtime, cliRes *plugin.ParseCLIRes) {
    46  	conf := proto.RunQueryConfig{}
    47  
    48  	conf.Command, _ = cmd.Flags().GetString("command")
    49  	conf.DoAst, _ = cmd.Flags().GetBool("ast")
    50  	conf.DoParse, _ = cmd.Flags().GetBool("parse")
    51  	if doJSON, _ := cmd.Flags().GetBool("json"); doJSON {
    52  		conf.Format = "json"
    53  	}
    54  	if llx, _ := cmd.Flags().GetString("llx"); llx != "" {
    55  		conf.Format = "llx"
    56  		conf.Output = llx
    57  	}
    58  	if llx, _ := cmd.Flags().GetString("use-llx"); llx != "" {
    59  		conf.Input = llx
    60  	}
    61  	conf.PlatformId, _ = cmd.Flags().GetString("platform-id")
    62  	conf.Inventory = &inventory.Inventory{
    63  		Spec: &inventory.InventorySpec{
    64  			Assets: []*inventory.Asset{cliRes.Asset},
    65  		},
    66  	}
    67  	conf.Incognito, _ = cmd.Flags().GetBool("incognito")
    68  
    69  	x := cnqueryPlugin{}
    70  	w := shared.IOWriter{Writer: os.Stdout}
    71  	err := x.RunQuery(&conf, runtime, &w)
    72  	if err != nil {
    73  		log.Fatal().Err(err).Msg("failed to run query")
    74  	}
    75  }