Skip to content

Commit

Permalink
unified printing strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
stevekuznetsov committed Jan 6, 2016
1 parent 07228ff commit 2b17e27
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 17 deletions.
15 changes: 13 additions & 2 deletions pkg/cmd/admin/groups/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"fmt"
"io"

"github.com/spf13/cobra"

kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/sets"

"github.com/spf13/cobra"

"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/cmd/util/mutation"
userapi "github.com/openshift/origin/pkg/user/api"
)

Expand All @@ -34,6 +35,9 @@ type NewGroupOptions struct {

Group string
Users []string

// MutationOutputOptions allows us to correctly output the mutations we make to objects in etcd
MutationOutputOptions mutation.MutationOutputOptions
}

func NewCmdNewGroup(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
Expand All @@ -49,10 +53,14 @@ func NewCmdNewGroup(name, fullName string, f *clientcmd.Factory, out io.Writer)
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}

options.MutationOutputOptions = mutation.NewMutationOutputOptions(f.Factory, cmd, out)

kcmdutil.CheckErr(options.AddGroup())
},
}

kcmdutil.AddOutputFlagsForMutation(cmd)

return cmd
}

Expand Down Expand Up @@ -90,5 +98,8 @@ func (o *NewGroupOptions) AddGroup() error {
}

_, err := o.GroupClient.Create(group)
if err != nil {
o.MutationOutputOptions.PrintSuccess(group, "created")
}
return err
}
20 changes: 14 additions & 6 deletions pkg/cmd/admin/groups/sync/cli/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/spf13/cobra"

configapilatest "github.com/openshift/origin/pkg/cmd/server/api/latest"
kapi "k8s.io/kubernetes/pkg/api"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
kerrs "k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/sets"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/cmd/server/api/validation"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/cmd/util/mutation"
)

const (
Expand Down Expand Up @@ -99,6 +99,9 @@ type SyncOptions struct {

// Out is the writer to write output to
Out io.Writer

// MutationOutputOptions allows us to correctly output the mutations we make to objects in etcd
MutationOutputOptions mutation.MutationOutputOptions
}

func NewSyncOptions() *SyncOptions {
Expand Down Expand Up @@ -127,6 +130,8 @@ func NewCmdSync(name, fullName string, f *clientcmd.Factory, out io.Writer) *cob
cmdutil.CheckErr(cmdutil.UsageError(c, err.Error()))
}

options.MutationOutputOptions = mutation.NewMutationOutputOptions(f.Factory, c, options.Out)

if err := options.Validate(); err != nil {
cmdutil.CheckErr(cmdutil.UsageError(c, err.Error()))
}
Expand Down Expand Up @@ -380,12 +385,15 @@ func (o *SyncOptions) Run(cmd *cobra.Command, f *clientcmd.Factory) error {
return kerrs.NewAggregate(syncErrors)
}

list := &kapi.List{}
for _, item := range openshiftGroups {
list.Items = append(list.Items, item)
var action string
if o.Confirm {
action = "updated"
} else {
action = "would be updated"
}
if err := f.Factory.PrintObject(cmd, list, o.Out); err != nil {
return err

for _, item := range openshiftGroups {
o.MutationOutputOptions.PrintSuccess(item, action)
}

return kerrs.NewAggregate(syncErrors)
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/admin/groups/sync/groupsyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func (s *LDAPGroupSyncer) Sync() ([]*userapi.Group, []error) {
openshiftGroups = append(openshiftGroups, openshiftGroup)

if !s.DryRun {
fmt.Fprintf(s.Out, "group/%s\n", openshiftGroup.Name)
if err := s.updateOpenShiftGroup(openshiftGroup); err != nil {
fmt.Fprintf(s.Err, "Error updating OpenShift group %q for LDAP group %q: %v.\n", openshiftGroup.Name, ldapGroupUID, err)
errors = append(errors, err)
Expand Down
9 changes: 8 additions & 1 deletion pkg/cmd/admin/project/new_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/openshift/origin/pkg/cmd/admin/policy"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/cmd/util/mutation"
projectapi "github.com/openshift/origin/pkg/project/api"
)

Expand All @@ -30,6 +31,9 @@ type NewProjectOptions struct {

AdminRole string
AdminUser string

// MutationOutputOptions allows us to correctly output the mutations we make to objects in etcd
MutationOutputOptions mutation.MutationOutputOptions
}

const newProjectLong = `
Expand All @@ -52,6 +56,8 @@ func NewCmdNewProject(name, fullName string, f *clientcmd.Factory, out io.Writer
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}

options.MutationOutputOptions = mutation.NewMutationOutputOptions(f.Factory, cmd, out)

var err error
if options.Client, _, err = f.Clients(); err != nil {
kcmdutil.CheckErr(err)
Expand All @@ -72,6 +78,7 @@ func NewCmdNewProject(name, fullName string, f *clientcmd.Factory, out io.Writer
cmd.Flags().StringVar(&options.DisplayName, "display-name", "", "Project display name")
cmd.Flags().StringVar(&options.Description, "description", "", "Project description")
cmd.Flags().StringVar(&options.NodeSelector, "node-selector", "", "Restrict pods onto nodes matching given label selector. Format: '<key1>=<value1>, <key2>=<value2>...'. Specifying \"\" means any node, not default. If unspecified, cluster default node selector will be used.")
kcmdutil.AddOutputFlagsForMutation(cmd)

return cmd
}
Expand Down Expand Up @@ -107,7 +114,7 @@ func (o *NewProjectOptions) Run(useNodeSelector bool) error {
return err
}

fmt.Printf("Created project %v\n", o.ProjectName)
o.MutationOutputOptions.PrintSuccess(project, "created")

errs := []error{}
if len(o.AdminUser) != 0 {
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/cli/secrets/basicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"

cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/mutation"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -52,6 +53,9 @@ type CreateBasicAuthSecretOptions struct {
Out io.Writer

SecretsInterface client.SecretsInterface

// MutationOutputOptions allows us to correctly output the mutations we make to objects in etcd
MutationOutputOptions mutation.MutationOutputOptions
}

// NewCmdCreateBasicAuthSecret implements the OpenShift cli secrets new-basicauth subcommand
Expand All @@ -71,6 +75,8 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f *kcmdutil.Factory, rea
kcmdutil.CheckErr(kcmdutil.UsageError(c, err.Error()))
}

o.MutationOutputOptions = mutation.NewMutationOutputOptions(f, c, o.Out)

if err := o.Validate(); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(c, err.Error()))
}
Expand Down Expand Up @@ -115,7 +121,7 @@ func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
return err
}

fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name)
o.MutationOutputOptions.PrintSuccess(secret, "created")
return nil
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/cmd/cli/secrets/dockercfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/kubernetes/pkg/credentialprovider"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"

"github.com/openshift/origin/pkg/cmd/util/mutation"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -53,6 +54,9 @@ type CreateDockerConfigOptions struct {
SecretsInterface client.SecretsInterface

Out io.Writer

// MutationOutputOptions allows us to correctly output the mutations we make to objects in etcd
MutationOutputOptions mutation.MutationOutputOptions
}

// NewCmdCreateDockerConfigSecret creates a command object for making a dockercfg secret
Expand All @@ -69,6 +73,8 @@ func NewCmdCreateDockerConfigSecret(name, fullName string, f *cmdutil.Factory, o
cmdutil.CheckErr(cmdutil.UsageError(c, err.Error()))
}

o.MutationOutputOptions = mutation.NewMutationOutputOptions(f, c, o.Out)

if err := o.Validate(); err != nil {
cmdutil.CheckErr(cmdutil.UsageError(c, err.Error()))
}
Expand Down Expand Up @@ -107,8 +113,7 @@ func (o CreateDockerConfigOptions) CreateDockerSecret() error {
return err
}

fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name)

o.MutationOutputOptions.PrintSuccess(secret, "created")
return nil
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/cli/secrets/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
kvalidation "k8s.io/kubernetes/pkg/api/validation"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
Expand Down Expand Up @@ -95,7 +96,9 @@ func NewCmdCreateSecret(name, fullName string, f *clientcmd.Factory, out io.Writ
return
}

_, err := options.CreateSecret()
mapper, _ := f.Object()
shortOutput := cmdutil.GetFlagString(c, "output") == "name"
_, err := options.CreateSecret(mapper, shortOutput)
cmdutil.CheckErr(err)
},
}
Expand Down Expand Up @@ -170,15 +173,16 @@ func (o *CreateSecretOptions) Validate() error {
return nil
}

func (o *CreateSecretOptions) CreateSecret() (*kapi.Secret, error) {
func (o *CreateSecretOptions) CreateSecret(mapper meta.RESTMapper, shortOutput bool) (*kapi.Secret, error) {
secret, err := o.BundleSecret()
if err != nil {
return nil, err
}

persistedSecret, err := o.SecretsInterface.Create(secret)

if err == nil {
fmt.Fprintf(o.Out, "secret/%s\n", persistedSecret.Name)
cmdutil.PrintSuccess(mapper, shortOutput, o.Out, "Secret", persistedSecret.Name, "created")
}

return persistedSecret, err
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/cli/secrets/sshauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
client "k8s.io/kubernetes/pkg/client/unversioned"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"

"github.com/openshift/origin/pkg/cmd/util/mutation"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -48,6 +49,9 @@ type CreateSSHAuthSecretOptions struct {
Out io.Writer

SecretsInterface client.SecretsInterface

// MutationOutputOptions allows us to correctly output the mutations we make to objects in etcd
MutationOutputOptions mutation.MutationOutputOptions
}

// NewCmdCreateSSHAuthSecret implements the OpenShift cli secrets new-sshauth subcommand
Expand All @@ -66,6 +70,8 @@ func NewCmdCreateSSHAuthSecret(name, fullName string, f *kcmdutil.Factory, out i
kcmdutil.CheckErr(kcmdutil.UsageError(c, err.Error()))
}

o.MutationOutputOptions = mutation.NewMutationOutputOptions(f, c, o.Out)

if err := o.Validate(); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(c, err.Error()))
}
Expand Down Expand Up @@ -109,7 +115,7 @@ func (o *CreateSSHAuthSecretOptions) CreateSSHAuthSecret() error {
return err
}

fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name)
o.MutationOutputOptions.PrintSuccess(secret, "created")
return nil
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/cmd/util/mutation/mutate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mutation

import (
"io"
"io/ioutil"

"github.com/spf13/cobra"

"k8s.io/kubernetes/pkg/api/meta"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime"
)

func NewMutationOutputOptions(factory *kcmdutil.Factory, cmd *cobra.Command, out io.Writer) MutationOutputOptions {
mapper, typer := factory.Object()
if out == nil {
out = ioutil.Discard
}

return MutationOutputOptions{
mapper: mapper,
resourceMapper: resource.Mapper{ObjectTyper: typer, RESTMapper: mapper, ClientMapper: factory.ClientMapperForCommand()},
shortOutput: kcmdutil.GetFlagString(cmd, "output") == "name",
out: out,
}
}

// MutationOutputOptions holds all of the information necessary to correctly output the reslult of mutating
// an object in etcd.
type MutationOutputOptions struct {
mapper meta.RESTMapper
resourceMapper resource.Mapper
shortOutput bool
out io.Writer
}

// PrintSuccess wraps around kcmdutil.PrintSuccess
func (o *MutationOutputOptions) PrintSuccess(object runtime.Object, operation string) error {
info, err := o.resourceMapper.InfoForObject(object)
if err != nil {
return err
}
kcmdutil.PrintSuccess(o.mapper, o.shortOutput, o.out, info.Mapping.Resource, info.Name, operation)
return nil
}

0 comments on commit 2b17e27

Please sign in to comment.