ref(cmd): refactor create cmd

pull/952/head
Adam Reese 9 years ago
parent 0d7cb4ac83
commit 4dd95addd2

@ -18,9 +18,12 @@ package main
import ( import (
"errors" "errors"
"fmt"
"io"
"path/filepath" "path/filepath"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/chart"
) )
@ -50,31 +53,40 @@ destination exists and there are files in that directory, conflicting files
will be overwritten, but other files will be left alone. will be overwritten, but other files will be left alone.
` `
func init() { type createCmd struct {
RootCommand.AddCommand(createCmd) name string
out io.Writer
} }
var createCmd = &cobra.Command{ func newCreateCmd(out io.Writer) *cobra.Command {
Use: "create NAME", cc := &createCmd{
Short: "create a new chart with the given name", out: out,
Long: createDesc, }
RunE: runCreate, cmd := &cobra.Command{
Use: "create NAME",
Short: "create a new chart with the given name",
Long: createDesc,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("the name of the new chart is required")
}
cc.name = args[0]
return cc.run()
},
}
return cmd
} }
func runCreate(cmd *cobra.Command, args []string) error { func (c *createCmd) run() error {
if len(args) == 0 { fmt.Fprintf(c.out, "Creating %s\n", c.name)
return errors.New("the name of the new chart is required")
}
cname := args[0]
cmd.Printf("Creating %s\n", cname)
chartname := filepath.Base(cname) chartname := filepath.Base(c.name)
cfile := &chart.Metadata{ cfile := &chart.Metadata{
Name: chartname, Name: chartname,
Description: "A Helm chart for Kubernetes", Description: "A Helm chart for Kubernetes",
Version: "0.1.0", Version: "0.1.0",
} }
_, err := chartutil.Create(cfile, filepath.Dir(cname)) _, err := chartutil.Create(cfile, filepath.Dir(c.name))
return err return err
} }

@ -85,9 +85,11 @@ func newRootCmd(out io.Writer) *cobra.Command {
p.StringVarP(&tillerNamespace, "namespace", "", "", "kubernetes namespace") p.StringVarP(&tillerNamespace, "namespace", "", "", "kubernetes namespace")
p.BoolVarP(&flagDebug, "debug", "", false, "enable verbose output") p.BoolVarP(&flagDebug, "debug", "", false, "enable verbose output")
cmd.AddCommand(newListCmd(nil, out)) cmd.AddCommand(
cmd.AddCommand(newGetCmd(nil, out)) newCreateCmd(out),
newGetCmd(nil, out),
newListCmd(nil, out),
)
return cmd return cmd
} }

Loading…
Cancel
Save