Skip to content

Commit

Permalink
Support multiple templatevars flag usage in same command line (#188)
Browse files Browse the repository at this point in the history
* support multiple templateVars usage

* add note in templates docs
  • Loading branch information
reubenmiller committed Oct 16, 2022
1 parent 9a4675f commit 5e2e6a3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
15 changes: 15 additions & 0 deletions docs/go-c8y-cli/docs/concepts/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ c8y inventory create \
}
```

When using `c8y` directly, `--templateVars` can be used multiple times in the same command. This provides a better tab completion experience for the template variable names.

<CodeExample>

```bash
c8y inventory create \
--template "./examples/templates/device.jsonnet" \
--templateVars "type=macOS" \
--templateVars "fragment=customer_Agent" \
--dry
```

</CodeExample>


## Template Functions (added by go-c8y-cli)

Below lists the additional functions which are available in jsonnet template files. These functions are added to your template automatically by the c8y cli tool itself, and are not part of the standard jsonnet library. The built-in [jsonnet standard library](https://jsonnet.org/ref/stdlib.html) provides additional functions that can be used in combination with those injected by go-c8y-cli.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmdutil/templateflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (f *Factory) WithTemplateFlag(cmd *cobra.Command) flags.Option {
return nil
}
cmd.Flags().String(flags.FlagDataTemplateName, "", "Body template")
cmd.Flags().String(flags.FlagDataTemplateVariablesName, "", "Body template variables")
cmd.Flags().StringArray(flags.FlagDataTemplateVariablesName, []string{}, "Body template variables")

_ = cmd.RegisterFlagCompletionFunc(flags.FlagDataTemplateName, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
templatePath := cfg.GetTemplatePaths()
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmdutil/templateflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Test_WithTemplateValue(t *testing.T) {
flags.WithOptions(
cmd,
flags.WithData(),
flags.WithTemplate(),
flags.WithTemplateNoCompletion(),
)
inputIterator, _ := NewRequestInputIterators(cmd, nil)

Expand Down
6 changes: 3 additions & 3 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func WithCommonCumulocityQueryOptions() Option {
}
}

// WithTemplate adds support for templates
func WithTemplate() Option {
// WithTemplateNoCompletion adds support for templates
func WithTemplateNoCompletion() Option {
return func(cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(FlagDataTemplateName, "", "Body template")
cmd.Flags().String(FlagDataTemplateVariablesName, "", "Body template variables")
cmd.Flags().StringArray(FlagDataTemplateVariablesName, []string{}, "Body template variables")
return cmd
}
}
Expand Down
25 changes: 19 additions & 6 deletions pkg/flags/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,19 +854,32 @@ func WithDataValueAdvanced(stripCumulocityKeys bool, raw bool, opts ...string) G
return "", "", nil
}

value, err := cmd.Flags().GetString(src)
values, err := cmd.Flags().GetStringArray(src)
if err != nil {
return dst, value, err
// Try to read from string instead
if value, err := cmd.Flags().GetString(src); err != nil {
return dst, value, err
} else {
values = append(values, value)
}
}

if len(values) == 0 {
return "", "", nil
}

if raw {
return dst, RawString(resolveContents(value)), nil
return dst, RawString(resolveContents(values[0])), nil
}
data := make(map[string]interface{})

err = jsonUtilities.ParseJSON(resolveContents(value), data)
if err != nil {
return dst, "", fmt.Errorf("json error: %s parameter does not contain valid json or shorthand json. %w", src, err)
// Merge multiple data objects together
for _, value := range values {
log.Printf("Parsing value: %v", value)
err = jsonUtilities.ParseJSON(resolveContents(value), data)
if err != nil {
return dst, "", fmt.Errorf("json error: %s parameter does not contain valid json or shorthand json. %w", src, err)
}
}

if stripCumulocityKeys {
Expand Down
8 changes: 8 additions & 0 deletions tests/manual/template/template_execute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,11 @@ tests:
stderr:
contains:
- 'commandError: Missing required parameter. template'

It accepts multiple template vars:
command: |
c8y template execute --template vars --templateVars prop1.value=1 --templateVars prop2=two
exit-code: 0
stdout:
exactly: |
{"prop1":{"value":1},"prop2":"two"}

0 comments on commit 5e2e6a3

Please sign in to comment.