github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/create.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cmd
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"path/filepath"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"helm.sh/helm/v3/cmd/helm/require"
    27  	"helm.sh/helm/v3/pkg/chart"
    28  	"helm.sh/helm/v3/pkg/chartutil"
    29  	"helm.sh/helm/v3/pkg/helmpath"
    30  )
    31  
    32  const createDesc = `
    33  This command creates a chart directory along with the common files and
    34  directories used in a chart.
    35  
    36  For example, 'helm create foo' will create a directory structure that looks
    37  something like this:
    38  
    39      foo/
    40      ├── .helmignore   # Contains patterns to ignore when packaging Helm charts.
    41      ├── Chart.yaml    # Information about your chart
    42      ├── values.yaml   # The default values for your templates
    43      ├── charts/       # Charts that this chart depends on
    44      └── templates/    # The template files
    45          └── tests/    # The test files
    46  
    47  'helm create' takes a path for an argument. If directories in the given path
    48  do not exist, Helm will attempt to create them as it goes. If the given
    49  destination exists and there are files in that directory, conflicting files
    50  will be overwritten, but other files will be left alone.
    51  `
    52  
    53  type createOptions struct {
    54  	starter    string // --starter
    55  	name       string
    56  	starterDir string
    57  }
    58  
    59  func newCreateCmd(out io.Writer) *cobra.Command {
    60  	o := &createOptions{}
    61  
    62  	cmd := &cobra.Command{
    63  		Use:   "create NAME",
    64  		Short: "create a new chart with the given name",
    65  		Long:  createDesc,
    66  		Args:  require.ExactArgs(1),
    67  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    68  			if len(args) == 0 {
    69  				// Allow file completion when completing the argument for the name
    70  				// which could be a path
    71  				return nil, cobra.ShellCompDirectiveDefault
    72  			}
    73  			// No more completions, so disable file completion
    74  			return nil, cobra.ShellCompDirectiveNoFileComp
    75  		},
    76  		RunE: func(cmd *cobra.Command, args []string) error {
    77  			o.name = args[0]
    78  			o.starterDir = helmpath.DataPath("starters")
    79  			return o.run(out)
    80  		},
    81  	}
    82  
    83  	cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "the name or absolute path to Helm starter scaffold")
    84  	return cmd
    85  }
    86  
    87  func (o *createOptions) run(out io.Writer) error {
    88  	fmt.Fprintf(out, "Creating %s\n", o.name)
    89  
    90  	chartname := filepath.Base(o.name)
    91  	cfile := &chart.Metadata{
    92  		Name:        chartname,
    93  		Description: "A Helm chart for Kubernetes",
    94  		Type:        "application",
    95  		Version:     "0.1.0",
    96  		AppVersion:  "0.1.0",
    97  		APIVersion:  chart.APIVersionV2,
    98  	}
    99  
   100  	if o.starter != "" {
   101  		// Create from the starter
   102  		lstarter := filepath.Join(o.starterDir, o.starter)
   103  		// If path is absolute, we don't want to prefix it with helm starters folder
   104  		if filepath.IsAbs(o.starter) {
   105  			lstarter = o.starter
   106  		}
   107  		return chartutil.CreateFrom(cfile, filepath.Dir(o.name), lstarter)
   108  	}
   109  
   110  	chartutil.Stderr = out
   111  	_, err := chartutil.Create(chartname, filepath.Dir(o.name))
   112  	return err
   113  }