github.com/mgoltzsche/khelm@v1.0.1/cmd/khelm/template.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/mgoltzsche/khelm/internal/output"
     8  	"github.com/mgoltzsche/khelm/pkg/config"
     9  	"github.com/mgoltzsche/khelm/pkg/helm"
    10  	"github.com/spf13/cobra"
    11  	"k8s.io/helm/pkg/strvals"
    12  )
    13  
    14  func templateCommand(h *helm.Helm, writer io.Writer) *cobra.Command {
    15  	req := config.NewChartConfig()
    16  	req.Name = "release-name"
    17  	outOpts := output.Options{Writer: writer}
    18  	trustAnyRepo := false
    19  	cmd := &cobra.Command{
    20  		Use: "template",
    21  		Args: func(cmd *cobra.Command, args []string) error {
    22  			if len(args) != 1 {
    23  				_ = cmd.Help()
    24  				return fmt.Errorf("accepts single CHART argument but received %d arguments", len(args))
    25  			}
    26  			return nil
    27  		},
    28  		SuggestFor: []string{"render", "build"},
    29  		Short:      "Renders a chart",
    30  		Example:    usageExample,
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			if cmd.Flags().Changed(flagTrustAnyRepo) {
    33  				h.TrustAnyRepository = &trustAnyRepo
    34  			}
    35  			out, err := output.New(outOpts)
    36  			if err != nil {
    37  				return err
    38  			}
    39  			req.Chart = args[0]
    40  			resources, err := render(h, req)
    41  			if err != nil {
    42  				return err
    43  			}
    44  			return out.Write(resources)
    45  		},
    46  		SilenceErrors: true,
    47  		SilenceUsage:  true,
    48  	}
    49  	cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
    50  		_ = cmd.Help()
    51  		return err
    52  	})
    53  	f := cmd.Flags()
    54  	f.StringVar(&req.Repository, "repo", "", "Chart repository url where to locate the requested chart")
    55  	f.StringVar(&req.Repository, "repository", "", "Chart repository url where to locate the requested chart")
    56  	f.Lookup("repository").Hidden = true
    57  	f.StringVar(&req.Version, "version", "", "Specify the exact chart version to use. If this is not specified, the latest version is used")
    58  	f.BoolVar(&trustAnyRepo, flagTrustAnyRepo, trustAnyRepo,
    59  		fmt.Sprintf("Allow to use repositories that are not registered within repositories.yaml (default is true when repositories.yaml does not exist; %s)", envTrustAnyRepo))
    60  	f.BoolVar(&req.NamespacedOnly, "namespaced-only", false, "Fail on known cluster-scoped resources and those of unknown kinds")
    61  	f.StringVar(&req.Keyring, "keyring", req.Keyring, "Keyring used to verify the chart")
    62  	f.BoolVar(&req.Verify, "verify", false, "Verify the package before using it")
    63  	f.BoolVar(&req.ReplaceLockFile, "replace-lock-file", false, "Remove requirements.lock and reload charts when it is out of sync")
    64  	f.StringVar(&req.Name, "name", req.Name, "Release name")
    65  	f.StringVar(&req.Namespace, "namespace", req.Namespace, "Set the installation namespace used by helm templates")
    66  	f.StringVar(&req.ForceNamespace, "force-namespace", req.ForceNamespace, "Set namespace on all namespaced resources (and those of unknown kinds)")
    67  	f.Var((*valuesFlag)(&req.Values), "set", "Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
    68  	f.StringSliceVarP(&req.ValueFiles, "values", "f", nil, "Specify values in a YAML file or a URL (can specify multiple)")
    69  	f.StringSliceVar(&req.APIVersions, "api-versions", nil, "Kubernetes api versions used for Capabilities.APIVersions")
    70  	f.StringVar(&req.KubeVersion, "kube-version", req.KubeVersion, "Kubernetes version used as Capabilities.KubeVersion.Major/Minor")
    71  	f.BoolVar(&req.ExcludeHooks, "no-hooks", req.ExcludeHooks, "If enabled hooks are omitted from the output")
    72  	f.BoolVar(&req.ExcludeHooks, "exclude-hooks", req.ExcludeHooks, "If enabled hooks are omitted from the output")
    73  	f.Lookup("exclude-hooks").Hidden = true
    74  	f.StringVarP(&outOpts.FileOrDir, "output", "o", "-", "Write rendered output to given file or directory (as kustomization)")
    75  	f.BoolVar(&outOpts.Replace, "output-replace", false, "Delete and recreate the whole output directory or file")
    76  	return cmd
    77  }
    78  
    79  type valuesFlag map[string]interface{}
    80  
    81  func (f *valuesFlag) Set(s string) error {
    82  	base := *(*map[string]interface{})(f)
    83  	return strvals.ParseInto(s, base)
    84  }
    85  
    86  func (f *valuesFlag) Type() string {
    87  	return "strings"
    88  }
    89  
    90  func (f *valuesFlag) String() string {
    91  	return ""
    92  }