Merge pull request #180 from justinsb/delete_cluster_positional_arg

kops delete cluster now recognizes clustername as a positional arg
This commit is contained in:
Justin Santa Barbara 2016-07-21 01:23:37 -04:00 committed by GitHub
commit aa51cfa967
2 changed files with 29 additions and 5 deletions

View File

@ -23,11 +23,11 @@ var deleteCluster DeleteClusterCmd
func init() {
cmd := &cobra.Command{
Use: "cluster",
Use: "cluster CLUSTERNAME [--yes]",
Short: "Delete cluster",
Long: `Deletes a k8s cluster.`,
Run: func(cmd *cobra.Command, args []string) {
err := deleteCluster.Run()
err := deleteCluster.Run(args)
if err != nil {
glog.Exitf("%v", err)
}
@ -45,9 +45,13 @@ func init() {
type getter func(o interface{}) interface{}
func (c *DeleteClusterCmd) Run() error {
func (c *DeleteClusterCmd) Run(args []string) error {
var clusterRegistry *api.ClusterRegistry
var err error
err := rootCommand.ProcessArgs(args)
if err != nil {
return err
}
var cloud fi.Cloud
clusterName := ""
@ -55,7 +59,7 @@ func (c *DeleteClusterCmd) Run() error {
if c.External {
region = c.Region
if region == "" {
return fmt.Errorf("--region is required")
return fmt.Errorf("--region is required (when --external)")
}
clusterName = rootCommand.clusterName
if clusterName == "" {

View File

@ -79,6 +79,26 @@ func (c *RootCmd) AddCommand(cmd *cobra.Command) {
c.cobraCommand.AddCommand(cmd)
}
// ProcessArgs will parse the positional args. It assumes one of these formats:
// * <no arguments at all>
// * <clustername> (and --name not specified)
// Everything else is an error.
func (c *RootCmd) ProcessArgs(args []string) error {
if len(args) == 0 {
return nil
}
if len(args) == 1 {
// Assume <clustername>
if c.clusterName != "" {
return fmt.Errorf("Cannot specify cluster via --name and positional argument")
}
c.clusterName = args[0]
return nil
}
return fmt.Errorf("expected a single <clustername> to be passed as an argument")
}
func (c *RootCmd) ClusterName() string {
if c.clusterName != "" {
return c.clusterName