Cleanup get command

This commit is contained in:
Justin Santa Barbara 2017-04-06 21:36:41 -04:00
parent a7b77472e4
commit db0c86e8ee
6 changed files with 68 additions and 49 deletions

View File

@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/util/i18n"
@ -54,21 +55,8 @@ var (
get_short = i18n.T(`Get one or many resources.`)
)
// GetCmd represents the get command
type GetCmd struct {
type GetOptions struct {
output string
cobraCommand *cobra.Command
}
var getCmd = GetCmd{
cobraCommand: &cobra.Command{
Use: "get",
SuggestFor: []string{"list"},
Short: get_short,
Long: get_long,
Example: get_example,
},
}
const (
@ -77,12 +65,26 @@ const (
OutputJSON = "json"
)
func init() {
cmd := getCmd.cobraCommand
func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
options := &GetOptions{}
rootCommand.AddCommand(cmd)
cmd := &cobra.Command{
Use: "get",
SuggestFor: []string{"list"},
Short: get_short,
Long: get_long,
Example: get_example,
}
cmd.PersistentFlags().StringVarP(&getCmd.output, "output", "o", OutputTable, "output format. One of: table, yaml, json")
cmd.PersistentFlags().StringVarP(&options.output, "output", "o", OutputTable, "output format. One of: table, yaml, json")
// create subcommands
cmd.AddCommand(NewCmdGetCluster(f, out, options))
cmd.AddCommand(NewCmdGetFederations(f, out, options))
cmd.AddCommand(NewCmdGetInstanceGroups(f, out, options))
cmd.AddCommand(NewCmdGetSecrets(f, out, options))
return cmd
}
type marshalFunc func(obj runtime.Object) ([]byte, error)

View File

@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/registry"
"k8s.io/kops/util/pkg/tables"
@ -48,6 +49,8 @@ var (
)
type GetClusterOptions struct {
*GetOptions
// FullSpec determines if we should output the completed (fully populated) spec
FullSpec bool
@ -55,8 +58,10 @@ type GetClusterOptions struct {
ClusterNames []string
}
func init() {
var options GetClusterOptions
func NewCmdGetCluster(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra.Command {
options := GetClusterOptions{
GetOptions: getOptions,
}
cmd := &cobra.Command{
Use: "clusters",
@ -86,7 +91,7 @@ func init() {
cmd.Flags().BoolVar(&options.FullSpec, "full", options.FullSpec, "Show fully populated configuration")
getCmd.cobraCommand.AddCommand(cmd)
return cmd
}
func RunGetClusters(context Factory, out io.Writer, options *GetClusterOptions) error {
@ -135,7 +140,7 @@ func RunGetClusters(context Factory, out io.Writer, options *GetClusterOptions)
}
}
switch getCmd.output {
switch options.output {
case OutputTable:
t := &tables.Table{}
@ -176,7 +181,7 @@ func RunGetClusters(context Factory, out io.Writer, options *GetClusterOptions)
return nil
default:
return fmt.Errorf("Unknown output format: %q", getCmd.output)
return fmt.Errorf("Unknown output format: %q", options.output)
}
}

View File

@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/util/pkg/tables"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
@ -42,11 +43,13 @@ var (
)
type GetFederationOptions struct {
*GetOptions
}
func init() {
var options GetFederationOptions
func NewCmdGetFederations(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra.Command {
options := GetFederationOptions{
GetOptions: getOptions,
}
cmd := &cobra.Command{
Use: "federations",
Aliases: []string{"federation"},
@ -61,7 +64,7 @@ func init() {
},
}
getCmd.cobraCommand.AddCommand(cmd)
return cmd
}
func RunGetFederations(context Factory, out io.Writer, options *GetFederationOptions) error {
@ -83,7 +86,7 @@ func RunGetFederations(context Factory, out io.Writer, options *GetFederationOpt
fmt.Fprintf(out, "No federations found\n")
return nil
}
switch getCmd.output {
switch options.output {
case OutputTable:
@ -118,7 +121,7 @@ func RunGetFederations(context Factory, out io.Writer, options *GetFederationOpt
}
}
default:
return fmt.Errorf("Unknown output format: %q", getCmd.output)
return fmt.Errorf("Unknown output format: %q", options.output)
}
return nil
}

View File

@ -23,7 +23,9 @@ import (
"strings"
"github.com/spf13/cobra"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/util/pkg/tables"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
@ -44,12 +46,15 @@ var (
get_instancegroups_short = i18n.T(`Get one or many instancegroups`)
)
type GetInstanceGroupsCmd struct {
type GetInstanceGroupsOptions struct {
*GetOptions
}
var getInstanceGroupsCmd GetInstanceGroupsCmd
func NewCmdGetInstanceGroups(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra.Command {
options := GetInstanceGroupsOptions{
GetOptions: getOptions,
}
func init() {
cmd := &cobra.Command{
Use: "instancegroups",
Aliases: []string{"instancegroup", "ig"},
@ -57,17 +62,17 @@ func init() {
Long: get_instancegroups_long,
Example: get_instancegroups_example,
Run: func(cmd *cobra.Command, args []string) {
err := getInstanceGroupsCmd.Run(args)
err := RunGetInstanceGroups(&options, args)
if err != nil {
exitWithError(err)
}
},
}
getCmd.cobraCommand.AddCommand(cmd)
return cmd
}
func (c *GetInstanceGroupsCmd) Run(args []string) error {
func RunGetInstanceGroups(options *GetInstanceGroupsOptions, args []string) error {
out := os.Stdout
clusterName := rootCommand.ClusterName()
@ -113,7 +118,7 @@ func (c *GetInstanceGroupsCmd) Run(args []string) error {
return nil
}
switch getCmd.output {
switch options.output {
case OutputTable:
t := &tables.Table{}
@ -156,7 +161,7 @@ func (c *GetInstanceGroupsCmd) Run(args []string) error {
}
}
default:
return fmt.Errorf("Unknown output format: %q", getCmd.output)
return fmt.Errorf("Unknown output format: %q", options.output)
}
return nil
}

View File

@ -23,6 +23,8 @@ import (
"strings"
"github.com/spf13/cobra"
"io"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops/registry"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/tables"
@ -44,13 +46,15 @@ var (
get_secret_short = i18n.T(`Get one or many secrets.`)
)
type GetSecretsCommand struct {
type GetSecretsOptions struct {
*GetOptions
Type string
}
var getSecretsCommand GetSecretsCommand
func init() {
func NewCmdGetSecrets(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra.Command {
options := GetSecretsOptions{
GetOptions: getOptions,
}
cmd := &cobra.Command{
Use: "secrets",
Aliases: []string{"secret"},
@ -58,16 +62,15 @@ func init() {
Long: get_secret_long,
Example: get_secret_example,
Run: func(cmd *cobra.Command, args []string) {
err := getSecretsCommand.Run(args)
err := RunGetSecrets(&options, args)
if err != nil {
exitWithError(err)
}
},
}
getCmd.cobraCommand.AddCommand(cmd)
cmd.Flags().StringVarP(&getSecretsCommand.Type, "type", "", "", "Filter by secret type")
cmd.Flags().StringVarP(&options.Type, "type", "", "", "Filter by secret type")
return cmd
}
func listSecrets(keyStore fi.CAStore, secretStore fi.SecretStore, secretType string, names []string) ([]*fi.KeystoreItem, error) {
@ -139,7 +142,7 @@ func listSecrets(keyStore fi.CAStore, secretStore fi.SecretStore, secretType str
return items, nil
}
func (c *GetSecretsCommand) Run(args []string) error {
func RunGetSecrets(options *GetSecretsOptions, args []string) error {
cluster, err := rootCommand.Cluster()
if err != nil {
return err
@ -155,7 +158,7 @@ func (c *GetSecretsCommand) Run(args []string) error {
return err
}
items, err := listSecrets(keyStore, secretStore, c.Type, args)
items, err := listSecrets(keyStore, secretStore, options.Type, args)
if err != nil {
return err
}
@ -165,7 +168,7 @@ func (c *GetSecretsCommand) Run(args []string) error {
return nil
}
switch getCmd.output {
switch options.output {
case OutputTable:
@ -211,6 +214,6 @@ func (c *GetSecretsCommand) Run(args []string) error {
return nil
default:
return fmt.Errorf("Unknown output format: %q", getCmd.output)
return fmt.Errorf("Unknown output format: %q", options.output)
}
}

View File

@ -131,6 +131,7 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
cmd.AddCommand(NewCmdDelete(f, out))
cmd.AddCommand(NewCmdEdit(f, out))
cmd.AddCommand(NewCmdExport(f, out))
cmd.AddCommand(NewCmdGet(f, out))
cmd.AddCommand(NewCmdUpdate(f, out))
cmd.AddCommand(NewCmdReplace(f, out))
cmd.AddCommand(NewCmdRollingUpdate(f, out))