github.com/rakanixu/helm@v2.8.2+incompatible/cmd/helm/create.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  'helm create' takes a path for an argument. If directories in the given path
    52  do not exist, Helm will attempt to create them as it goes. If the given
    53  destination exists and there are files in that directory, conflicting files
    54  will be overwritten, but other files will be left alone.
    55  `
    56  
    57  type createCmd struct {
    58  	home    helmpath.Home
    59  	name    string
    60  	out     io.Writer
    61  	starter string
    62  }
    63  
    64  func newCreateCmd(out io.Writer) *cobra.Command {
    65  	cc := &createCmd{out: out}
    66  
    67  	cmd := &cobra.Command{
    68  		Use:   "create NAME",
    69  		Short: "create a new chart with the given name",
    70  		Long:  createDesc,
    71  		RunE: func(cmd *cobra.Command, args []string) error {
    72  			cc.home = settings.Home
    73  			if len(args) == 0 {
    74  				return errors.New("the name of the new chart is required")
    75  			}
    76  			cc.name = args[0]
    77  			return cc.run()
    78  		},
    79  	}
    80  
    81  	cmd.Flags().StringVarP(&cc.starter, "starter", "p", "", "the named Helm starter scaffold")
    82  	return cmd
    83  }
    84  
    85  func (c *createCmd) run() error {
    86  	fmt.Fprintf(c.out, "Creating %s\n", c.name)
    87  
    88  	chartname := filepath.Base(c.name)
    89  	cfile := &chart.Metadata{
    90  		Name:        chartname,
    91  		Description: "A Helm chart for Kubernetes",
    92  		Version:     "0.1.0",
    93  		AppVersion:  "1.0",
    94  		ApiVersion:  chartutil.ApiVersionV1,
    95  	}
    96  
    97  	if c.starter != "" {
    98  		// Create from the starter
    99  		lstarter := filepath.Join(c.home.Starters(), c.starter)
   100  		return chartutil.CreateFrom(cfile, filepath.Dir(c.name), lstarter)
   101  	}
   102  
   103  	_, err := chartutil.Create(cfile, filepath.Dir(c.name))
   104  	return err
   105  }