Skip to content

Commit

Permalink
Merge pull request #2767 from tompaana/explain-displays-custom-section
Browse files Browse the repository at this point in the history
Porter explain command displays custom section data when output is JSON or YAML
  • Loading branch information
schristoff authored May 31, 2023
2 parents 9d2df25 + c3a51f1 commit 9dfd04f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ and we will add you. **All** contributors belong here. 💯
* [Chengwei Guo](https://github.com/cw-Guo)
* [Sarah Christoff](https://github.com/schristoff)
* [Aleksey Barabanov](https://github.com/alekseybb197)
* [Tomi Paananen](https://github.com/tompaana)
2 changes: 2 additions & 0 deletions docs/content/cli/explain.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Explain a bundle

Explain how to use a bundle by printing the parameters, credentials, outputs, actions.

The [Custom](../bundle/manifest/_index.md#custom) section data is only shown when the output format is JSON or YAML. Because the data can have an arbitrary structure, there is no coherent way to display it in a table.

```
porter explain REFERENCE [flags]
```
Expand Down
45 changes: 35 additions & 10 deletions pkg/porter/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ type ExplainOpts struct {

// PrintableBundle holds a subset of pertinent values to be explained from a bundle
type PrintableBundle struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Version string `json:"version" yaml:"version"`
PorterVersion string `json:"porterVersion,omitempty" yaml:"porterVersion,omitempty"`
Parameters []PrintableParameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
Credentials []PrintableCredential `json:"credentials,omitempty" yaml:"credentials,omitempty"`
Outputs []PrintableOutput `json:"outputs,omitempty" yaml:"outputs,omitempty"`
Actions []PrintableAction `json:"customActions,omitempty" yaml:"customActions,omitempty"`
Dependencies []PrintableDependency `json:"dependencies,omitempty" yaml:"dependencies,omitempty"`
Mixins []string `json:"mixins" yaml:"mixins"`
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Version string `json:"version" yaml:"version"`
PorterVersion string `json:"porterVersion,omitempty" yaml:"porterVersion,omitempty"`
Parameters []PrintableParameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
Credentials []PrintableCredential `json:"credentials,omitempty" yaml:"credentials,omitempty"`
Outputs []PrintableOutput `json:"outputs,omitempty" yaml:"outputs,omitempty"`
Actions []PrintableAction `json:"customActions,omitempty" yaml:"customActions,omitempty"`
Dependencies []PrintableDependency `json:"dependencies,omitempty" yaml:"dependencies,omitempty"`
Mixins []string `json:"mixins" yaml:"mixins"`
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
}

type PrintableCredential struct {
Expand Down Expand Up @@ -209,6 +210,7 @@ func generatePrintable(bun cnab.ExtendedBundle, action string) (*PrintableBundle
Outputs: make([]PrintableOutput, 0, len(bun.Outputs)),
Dependencies: make([]PrintableDependency, 0, len(deps)),
Mixins: make([]string, 0, len(stamp.Mixins)),
Custom: make(map[string]interface{}),
}

for a, v := range bun.Actions {
Expand Down Expand Up @@ -300,6 +302,12 @@ func generatePrintable(bun cnab.ExtendedBundle, action string) (*PrintableBundle
}
sort.Strings(pb.Mixins)

for key, value := range bun.Custom {
if isUserDefinedCustomSectionKey(key) {
pb.Custom[key] = value
}
}

return &pb, nil
}

Expand All @@ -313,6 +321,23 @@ func shouldIncludeInExplainOutput(scoped bundle.Scoped, action string) bool {
return bundle.AppliesTo(scoped, action)
}

// isUserDefinedCustomSectionKey returns true if the given key in the custom section data is
// user-defined and not one that Porter uses for its own purposes.
func isUserDefinedCustomSectionKey(key string) bool {
porterKeyPrefixes := []string{
"io.cnab",
"sh.porter",
}

for _, keyPrefix := range porterKeyPrefixes {
if strings.HasPrefix(key, keyPrefix) {
return false
}
}

return true
}

func generateApplyToString(appliesTo []string) string {
if len(appliesTo) == 0 {
return "All Actions"
Expand Down
10 changes: 9 additions & 1 deletion pkg/porter/testdata/explain/expected-json-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@
"mixins": [
"helm",
"terraform"
]
],
"custom": {
"user-defined": {
"nested": {
"enabled": true,
"structure": "is okay"
}
}
}
}
5 changes: 5 additions & 0 deletions pkg/porter/testdata/explain/expected-yaml-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ parameters:
mixins:
- helm
- terraform
custom:
user-defined:
nested:
enabled: true
structure: is okay
6 changes: 6 additions & 0 deletions pkg/porter/testdata/explain/params-bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
"terraform": {},
"helm": {}
}
},
"user-defined": {
"nested": {
"structure": "is okay",
"enabled": true
}
}
},
"definitions": {
Expand Down

0 comments on commit 9dfd04f

Please sign in to comment.