github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+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/cmd/helm/helmpath"
    28  	"k8s.io/helm/pkg/chartutil"
    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{
    66  		out: out,
    67  	}
    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 = helmpath.Home(homePath())
    75  			if len(args) == 0 {
    76  				return errors.New("the name of the new chart is required")
    77  			}
    78  			cc.name = args[0]
    79  			return cc.run()
    80  		},
    81  	}
    82  
    83  	cmd.Flags().StringVarP(&cc.starter, "starter", "p", "", "the named Helm starter scaffold")
    84  	return cmd
    85  }
    86  
    87  func (c *createCmd) run() error {
    88  	fmt.Fprintf(c.out, "Creating %s\n", c.name)
    89  
    90  	chartname := filepath.Base(c.name)
    91  	cfile := &chart.Metadata{
    92  		Name:        chartname,
    93  		Description: "A Helm chart for Kubernetes",
    94  		Version:     "0.1.0",
    95  		ApiVersion:  chartutil.ApiVersionV1,
    96  	}
    97  
    98  	if c.starter != "" {
    99  		// Create from the starter
   100  		lstarter := filepath.Join(c.home.Starters(), c.starter)
   101  		return chartutil.CreateFrom(cfile, filepath.Dir(c.name), lstarter)
   102  	}
   103  
   104  	_, err := chartutil.Create(cfile, filepath.Dir(c.name))
   105  	return err
   106  }