helm/pkg/cmd/create.go

114 lines
3.3 KiB
Go
Raw Normal View History

2016-06-22 14:28:45 -04:00
/*
Copyright The Helm Authors.
2016-06-22 14:28:45 -04:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
2016-04-14 18:54:04 -04:00
import (
2016-07-14 14:30:11 -04:00
"fmt"
"io"
2016-04-14 18:54:04 -04:00
"path/filepath"
"github.com/spf13/cobra"
2016-07-14 14:30:11 -04:00
chart "helm.sh/helm/v4/pkg/chart/v2"
chartutil "helm.sh/helm/v4/pkg/chart/v2/util"
"helm.sh/helm/v4/pkg/cmd/require"
"helm.sh/helm/v4/pkg/helmpath"
2016-04-14 18:54:04 -04:00
)
const createDesc = `
This command creates a chart directory along with the common files and
directories used in a chart.
For example, 'helm create foo' will create a directory structure that looks
something like this:
foo/
.helmignore # Contains patterns to ignore when packaging Helm charts.
Chart.yaml # Information about your chart
values.yaml # The default values for your templates
charts/ # Charts that this chart depends on
templates/ # The template files
tests/ # The test files
2016-04-14 18:54:04 -04:00
'helm create' takes a path for an argument. If directories in the given path
do not exist, Helm will attempt to create them as it goes. If the given
destination exists and there are files in that directory, conflicting files
will be overwritten, but other files will be left alone.
`
type createOptions struct {
starter string // --starter
name string
starterDir string
2016-04-14 18:54:04 -04:00
}
2016-07-14 14:30:11 -04:00
func newCreateCmd(out io.Writer) *cobra.Command {
o := &createOptions{}
2016-07-14 14:30:11 -04:00
cmd := &cobra.Command{
Use: "create NAME",
Short: "create a new chart with the given name",
Long: createDesc,
2018-05-16 12:56:45 -04:00
Args: require.ExactArgs(1),
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
// Allow file completion when completing the argument for the name
// which could be a path
return nil, cobra.ShellCompDirectiveDefault
}
// No more completions, so disable file completion
return noMoreArgsComp()
},
RunE: func(_ *cobra.Command, args []string) error {
o.name = args[0]
o.starterDir = helmpath.DataPath("starters")
return o.run(out)
2016-07-14 14:30:11 -04:00
},
}
cmd.Flags().StringVarP(&o.starter, "starter", "p", "", "the name or absolute path to Helm starter scaffold")
2016-07-14 14:30:11 -04:00
return cmd
2016-04-14 18:54:04 -04:00
}
func (o *createOptions) run(out io.Writer) error {
fmt.Fprintf(out, "Creating %s\n", o.name)
2016-04-14 18:54:04 -04:00
chartname := filepath.Base(o.name)
cfile := &chart.Metadata{
2016-04-14 18:54:04 -04:00
Name: chartname,
Description: "A Helm chart for Kubernetes",
Type: "application",
2016-04-14 18:54:04 -04:00
Version: "0.1.0",
AppVersion: "0.1.0",
APIVersion: chart.APIVersionV2,
2016-04-14 18:54:04 -04:00
}
if o.starter != "" {
// Create from the starter
lstarter := filepath.Join(o.starterDir, o.starter)
// If path is absolute, we don't want to prefix it with helm starters folder
if filepath.IsAbs(o.starter) {
lstarter = o.starter
}
return chartutil.CreateFrom(cfile, filepath.Dir(o.name), lstarter)
}
chartutil.Stderr = out
_, err := chartutil.Create(chartname, filepath.Dir(o.name))
return err
2016-04-14 18:54:04 -04:00
}