diff --git a/pkg/cmd/template.go b/pkg/cmd/template.go index 81c112d51..b6d473e75 100644 --- a/pkg/cmd/template.go +++ b/pkg/cmd/template.go @@ -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" "helm.sh/helm/v4/pkg/chart/common" "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,20 +169,12 @@ 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 } - manifestsToRender = append(manifestsToRender, manifest) - missing = false } if missing { return fmt.Errorf("could not find template %s in chart", f) diff --git a/pkg/cmd/template_test.go b/pkg/cmd/template_test.go index 5bcccf5d0..2d8e17800 100644 --- a/pkg/cmd/template_test.go +++ b/pkg/cmd/template_test.go @@ -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"), diff --git a/pkg/cmd/testdata/output/template-show-only-globstar.txt b/pkg/cmd/testdata/output/template-show-only-globstar.txt new file mode 100644 index 000000000..dc85f2413 --- /dev/null +++ b/pkg/cmd/testdata/output/template-show-only-globstar.txt @@ -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