github.com/aaronmell/helm@v3.0.0-beta.2+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  	"fmt"
    21  	"io"
    22  	"path/filepath"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"helm.sh/helm/cmd/helm/require"
    27  	"helm.sh/helm/pkg/chart"
    28  	"helm.sh/helm/pkg/chartutil"
    29  	"helm.sh/helm/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  
    46  'helm create' takes a path for an argument. If directories in the given path
    47  do not exist, Helm will attempt to create them as it goes. If the given
    48  destination exists and there are files in that directory, conflicting files
    49  will be overwritten, but other files will be left alone.
    50  `
    51  
    52  type createOptions struct {
    53  	starter    string // --starter
    54  	name       string
    55  	starterDir string
    56  }
    57  
    58  func newCreateCmd(out io.Writer) *cobra.Command {
    59  	o := &createOptions{}
    60  
    61  	cmd := &cobra.Command{
    62  		Use:   "create NAME",
    63  		Short: "create a new chart with the given name",
    64  		Long:  createDesc,
    65  		Args:  require.ExactArgs(1),
    66  		RunE: func(cmd *cobra.Command, args []string) error {
    67  			o.name = args[0]
    68  			o.starterDir = helmpath.DataPath("starters")
    69  			return o.run(out)
    70  		},
    71  	}
    72  
    73  	cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "The name or absolute path to Helm starter scaffold")
    74  	return cmd
    75  }
    76  
    77  func (o *createOptions) run(out io.Writer) error {
    78  	fmt.Fprintf(out, "Creating %s\n", o.name)
    79  
    80  	chartname := filepath.Base(o.name)
    81  	cfile := &chart.Metadata{
    82  		Name:        chartname,
    83  		Description: "A Helm chart for Kubernetes",
    84  		Type:        "application",
    85  		Version:     "0.1.0",
    86  		AppVersion:  "0.1.0",
    87  		APIVersion:  chart.APIVersionV2,
    88  	}
    89  
    90  	if o.starter != "" {
    91  		// Create from the starter
    92  		lstarter := filepath.Join(o.starterDir, o.starter)
    93  		// If path is absolute, we dont want to prefix it with helm starters folder
    94  		if filepath.IsAbs(o.starter) {
    95  			lstarter = o.starter
    96  		}
    97  		return chartutil.CreateFrom(cfile, filepath.Dir(o.name), lstarter)
    98  	}
    99  
   100  	_, err := chartutil.Create(chartname, filepath.Dir(o.name))
   101  	return err
   102  }