github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/cmd/helm/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 main
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"path/filepath"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	"k8s.io/helm/pkg/chartutil"
    28  	"k8s.io/helm/pkg/helm/helmpath"
    29  	"k8s.io/helm/pkg/proto/hapi/chart"
    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  	  |
    41  	  |- .helmignore        # Contains patterns to ignore when packaging Helm charts.
    42  	  |
    43  	  |- Chart.yaml         # Information about your chart
    44  	  |
    45  	  |- values.yaml        # The default values for your templates
    46  	  |
    47  	  |- charts/            # Charts that this chart depends on
    48  	  |
    49  	  |- templates/         # The template files
    50  	  |
    51  	  |- templates/tests/   # The test files
    52  
    53  'helm create' takes a path for an argument. If directories in the given path
    54  do not exist, Helm will attempt to create them as it goes. If the given
    55  destination exists and there are files in that directory, conflicting files
    56  will be overwritten, but other files will be left alone.
    57  `
    58  
    59  type createCmd struct {
    60  	home    helmpath.Home
    61  	name    string
    62  	out     io.Writer
    63  	starter string
    64  }
    65  
    66  func newCreateCmd(out io.Writer) *cobra.Command {
    67  	cc := &createCmd{out: out}
    68  
    69  	cmd := &cobra.Command{
    70  		Use:   "create NAME",
    71  		Short: "create a new chart with the given name",
    72  		Long:  createDesc,
    73  		RunE: func(cmd *cobra.Command, args []string) error {
    74  			cc.home = settings.Home
    75  			if len(args) == 0 {
    76  				return errors.New("the name of the new chart is required")
    77  			}
    78  			if len(args) > 1 {
    79  				return errors.New("command 'create' doesn't support multiple arguments")
    80  			}
    81  			cc.name = args[0]
    82  			return cc.run()
    83  		},
    84  	}
    85  
    86  	cmd.Flags().StringVarP(&cc.starter, "starter", "p", "", "the named Helm starter scaffold")
    87  	return cmd
    88  }
    89  
    90  func (c *createCmd) run() error {
    91  	fmt.Fprintf(c.out, "Creating %s\n", c.name)
    92  	chartname := filepath.Base(c.name)
    93  	cfile := &chart.Metadata{
    94  		Name:        chartname,
    95  		Description: "A Helm chart for Kubernetes",
    96  		Version:     "0.1.0",
    97  		AppVersion:  "1.0",
    98  		ApiVersion:  chartutil.ApiVersionV1,
    99  	}
   100  
   101  	if c.starter != "" {
   102  		// Create from the starter
   103  		lstarter := filepath.Join(c.home.Starters(), c.starter)
   104  		return chartutil.CreateFrom(cfile, filepath.Dir(c.name), lstarter)
   105  	}
   106  
   107  	_, err := chartutil.Create(cfile, filepath.Dir(c.name))
   108  	return err
   109  }