feat: add globstar support to --show-only

Signed-off-by: Mario Valderrama <valderra.ma@outlook.com>
pull/31164/head
Mario Valderrama 1 month ago
parent 3726d01c5c
commit a4249bcdb4

@ -30,10 +30,11 @@ import (
"sort"
"strings"
release "helm.sh/helm/v4/pkg/release/v1"
"github.com/gobwas/glob"
"github.com/spf13/cobra"
release "helm.sh/helm/v4/pkg/release/v1"
"helm.sh/helm/v4/pkg/action"
chartutil "helm.sh/helm/v4/pkg/chart/v2/util"
"helm.sh/helm/v4/pkg/cli/values"
@ -151,9 +152,16 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
manifestNameRegex := regexp.MustCompile("# Source: [^/]+/(.+)")
var manifestsToRender []string
for _, f := range showFiles {
missing := true
// Use linux-style filepath separators to unify user's input path
f = filepath.ToSlash(f)
// manifest.Path is connected using linux-style filepath separators on Windows as
// well as macOS/linux
g, err := glob.Compile(f, '/')
if err != nil {
return fmt.Errorf("invalid pattern %q", f)
}
missing := true
for _, manifestKey := range manifestsKeys {
manifest := splitManifests[manifestKey]
submatch := manifestNameRegex.FindStringSubmatch(manifest)
@ -161,21 +169,13 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
continue
}
manifestName := submatch[1]
// manifest.Name is rendered using linux-style filepath separators on Windows as
// well as macOS/linux.
manifestPathSplit := strings.Split(manifestName, "/")
// manifest.Path is connected using linux-style filepath separators on Windows as
// well as macOS/linux
manifestPath := strings.Join(manifestPathSplit, "/")
// if the filepath provided matches a manifest path in the
// chart, render that manifest
if matched, _ := filepath.Match(f, manifestPath); !matched {
continue
}
if g.Match(manifestName) {
manifestsToRender = append(manifestsToRender, manifest)
missing = false
}
}
if missing {
return fmt.Errorf("could not find template %s in chart", f)
}

@ -113,6 +113,12 @@ func TestTemplateCmd(t *testing.T) {
// Repeat to ensure manifest ordering regressions are caught
repeat: 10,
},
{
name: "template with show-only globstar",
cmd: fmt.Sprintf("template '%s' --show-only **/service.yaml ", chartPath),
golden: "output/template-show-only-globstar.txt",
repeat: 10,
},
{
name: "sorted output of manifests (order of filenames, then order of objects within each YAML file)",
cmd: fmt.Sprintf("template '%s'", "testdata/testcharts/object-order"),

@ -0,0 +1,55 @@
---
# Source: subchart/charts/subcharta/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: subcharta
labels:
helm.sh/chart: "subcharta-0.1.0"
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: apache
selector:
app.kubernetes.io/name: subcharta
---
# Source: subchart/charts/subchartb/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: subchartb
labels:
helm.sh/chart: "subchartb-0.1.0"
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: nginx
selector:
app.kubernetes.io/name: subchartb
---
# Source: subchart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: subchart
labels:
helm.sh/chart: "subchart-0.1.0"
app.kubernetes.io/instance: "release-name"
kube-version/major: "1"
kube-version/minor: "20"
kube-version/version: "v1.20.0"
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: nginx
selector:
app.kubernetes.io/name: subchart
Loading…
Cancel
Save