Skip to content

Commit

Permalink
✨ support setting annotations on run/shell commands (#3624)
Browse files Browse the repository at this point in the history
* ✨ support setting annotations on run/shell commands

great for debugging, otherwise hidden to avoid distractions

Signed-off-by: Dominik Richter <[email protected]>

* 🟢 tests now verify that annotations are nil

afaics, we do not require these objects to exist if they are empty

Signed-off-by: Dominik Richter <[email protected]>

* 🟢 typed nil values in golang 🤕

Signed-off-by: Dominik Richter <[email protected]>

* 🟢 check err on markhidden

Signed-off-by: Dominik Richter <[email protected]>

* 🟢 one more lint

Signed-off-by: Dominik Richter <[email protected]>

---------

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Mar 21, 2024
1 parent 48b64be commit f34593d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
11 changes: 9 additions & 2 deletions apps/cnquery/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ func init() {
RunCmd.Flags().String("llx", "", "Generate the executable code bundle and save it to the specified file.")
RunCmd.Flags().MarkHidden("llx")
RunCmd.Flags().String("use-llx", "", "Run the code specified in the code bundle on disk")
RunCmd.Flags().MarkHidden("use-llx")
_ = RunCmd.Flags().MarkHidden("use-llx")
RunCmd.Flags().StringToString("annotations", nil, "Specify annotations for this run")
_ = RunCmd.Flags().MarkHidden("annotations")
}

var RunCmd = &cobra.Command{
Use: "run",
Short: "Run an MQL query",
Long: `Run an MQL query on the CLI and displays its results.`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("platform-id", cmd.Flags().Lookup("platform-id"))
_ = viper.BindPFlag("platform-id", cmd.Flags().Lookup("platform-id"))
_ = viper.BindPFlag("annotations", cmd.Flags().Lookup("annotations"))
},
// we have to initialize an empty run so it shows up as a runnable command in --help
Run: func(cmd *cobra.Command, args []string) {},
Expand All @@ -60,7 +63,11 @@ var RunCmdRun = func(cmd *cobra.Command, runtime *providers.Runtime, cliRes *plu
if llx, _ := cmd.Flags().GetString("use-llx"); llx != "" {
conf.Input = llx
}

conf.PlatformId, _ = cmd.Flags().GetString("platform-id")
annotations, _ := cmd.Flags().GetStringToString("annotations")
cliRes.Asset.AddAnnotations(annotations)

in := &inventory.Inventory{
Spec: &inventory.InventorySpec{
Assets: []*inventory.Asset{cliRes.Asset},
Expand Down
8 changes: 7 additions & 1 deletion apps/cnquery/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ func init() {

shellCmd.Flags().StringP("command", "c", "", "MQL query to executed in the shell.")
shellCmd.Flags().String("platform-id", "", "Select a specific target asset by providing its platform ID.")
shellCmd.Flags().StringToString("annotations", nil, "Specify annotations for this run")
_ = shellCmd.Flags().MarkHidden("annotations")
}

var shellCmd = &cobra.Command{
Use: "shell",
Short: "Interactive query shell for MQL",
Long: `Allows the interactive exploration of MQL queries`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("platform-id", cmd.Flags().Lookup("platform-id"))
_ = viper.BindPFlag("platform-id", cmd.Flags().Lookup("platform-id"))
_ = viper.BindPFlag("annotations", cmd.Flags().Lookup("annotations"))
},
// we have to initialize an empty run so it shows up as a runnable command in --help
Run: func(cmd *cobra.Command, args []string) {},
Expand Down Expand Up @@ -82,6 +85,9 @@ func ParseShellConfig(cmd *cobra.Command, cliRes *plugin.ParseCLIRes) *ShellConf
}
}

annotations, _ := cmd.Flags().GetStringToString("annotations")
cliRes.Asset.AddAnnotations(annotations)

shellConf := ShellConfig{
Features: config.Features,
PlatformID: viper.GetString("platform-id"),
Expand Down
5 changes: 4 additions & 1 deletion providers-sdk/v1/inventory/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ func (a *Asset) AddLabels(labels map[string]string) {
}

func (a *Asset) AddAnnotations(annotations map[string]string) {
if len(annotations) == 0 {
return
}

if a.Annotations == nil {
a.Annotations = map[string]string{}
}

// copy annotations
for k := range annotations {
a.Annotations[k] = annotations[k]
}
Expand Down
4 changes: 2 additions & 2 deletions providers-sdk/v1/inventory/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestAddAnnotations(t *testing.T) {
},
}
asset.AddAnnotations(map[string]string{})
assert.Equal(t, map[string]string{}, asset.Annotations)
assert.Equal(t, map[string]string(nil), asset.Annotations)
})

t.Run("test nil", func(t *testing.T) {
Expand All @@ -53,7 +53,7 @@ func TestAddAnnotations(t *testing.T) {
},
}
asset.AddAnnotations(nil)
assert.Equal(t, map[string]string{}, asset.Annotations)
assert.Equal(t, map[string]string(nil), asset.Annotations)
})

t.Run("test merge", func(t *testing.T) {
Expand Down

0 comments on commit f34593d

Please sign in to comment.