github.com/latiif/helm@v2.15.0+incompatible/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. It provides a basic example and is not
    35  meant to cover all Kubernetes resources.
    36  
    37  For example, 'helm create foo' will create a directory structure that looks
    38  something like this:
    39  
    40  	foo/
    41  	  |
    42  	  |- .helmignore        # Contains patterns to ignore when packaging Helm charts.
    43  	  |
    44  	  |- Chart.yaml         # Information about your chart
    45  	  |
    46  	  |- values.yaml        # The default values for your templates
    47  	  |
    48  	  |- charts/            # Charts that this chart depends on
    49  	  |
    50  	  |- templates/         # The template files
    51  	  |
    52  	  |- templates/tests/   # The test files
    53  
    54  'helm create' takes a path for an argument. If directories in the given path
    55  do not exist, Helm will attempt to create them as it goes. If the given
    56  destination exists and there are files in that directory, conflicting files
    57  will be overwritten, but other files will be left alone.
    58  
    59  The chart that is created by invoking this command contains a Deployment, Ingress
    60  and a Service. To use other Kubernetes resources with your chart, refer to
    61  [The Chart Template Developer's Guide](https://helm.sh/docs/chart_template_guide).
    62  `
    63  
    64  type createCmd struct {
    65  	home    helmpath.Home
    66  	name    string
    67  	out     io.Writer
    68  	starter string
    69  }
    70  
    71  func newCreateCmd(out io.Writer) *cobra.Command {
    72  	cc := &createCmd{out: out}
    73  
    74  	cmd := &cobra.Command{
    75  		Use:   "create NAME",
    76  		Short: "Create a new chart with the given name",
    77  		Long:  createDesc,
    78  		RunE: func(cmd *cobra.Command, args []string) error {
    79  			cc.home = settings.Home
    80  			if len(args) == 0 {
    81  				return errors.New("the name of the new chart is required")
    82  			}
    83  			if len(args) > 1 {
    84  				return errors.New("command 'create' doesn't support multiple arguments")
    85  			}
    86  			cc.name = args[0]
    87  			return cc.run()
    88  		},
    89  	}
    90  
    91  	cmd.Flags().StringVarP(&cc.starter, "starter", "p", "", "The name or absolute path to Helm starter scaffold")
    92  	return cmd
    93  }
    94  
    95  func (c *createCmd) run() error {
    96  	fmt.Fprintf(c.out, "Creating %s\n", c.name)
    97  	chartname := filepath.Base(c.name)
    98  	cfile := &chart.Metadata{
    99  		Name:        chartname,
   100  		Description: "A Helm chart for Kubernetes",
   101  		Version:     "0.1.0",
   102  		AppVersion:  "1.0",
   103  		ApiVersion:  chartutil.ApiVersionV1,
   104  	}
   105  
   106  	if c.starter != "" {
   107  		// Create from the starter
   108  		lstarter := filepath.Join(c.home.Starters(), c.starter)
   109  		// If path is absolute, we don't want to prefix it with helm starters folder
   110  		if filepath.IsAbs(c.starter) {
   111  			lstarter = c.starter
   112  		}
   113  		return chartutil.CreateFrom(cfile, filepath.Dir(c.name), lstarter)
   114  	}
   115  
   116  	_, err := chartutil.Create(cfile, filepath.Dir(c.name))
   117  	return err
   118  }