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 24df569
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 32 deletions.
4 changes: 4 additions & 0 deletions contrib/completions/bash/oadm
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ _oadm_new-project()
flags+=("--description=")
flags+=("--display-name=")
flags+=("--node-selector=")
flags+=("--output=")
two_word_flags+=("-o")
flags+=("--alsologtostderr")
flags+=("--api-version=")
flags+=("--boot-id-file=")
Expand Down Expand Up @@ -1595,6 +1597,8 @@ _oadm_groups_new()
flags_with_completion=()
flags_completion=()

flags+=("--output=")
two_word_flags+=("-o")
flags+=("--alsologtostderr")
flags+=("--api-version=")
flags+=("--boot-id-file=")
Expand Down
4 changes: 4 additions & 0 deletions contrib/completions/bash/openshift
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@ _openshift_admin_new-project()
flags+=("--description=")
flags+=("--display-name=")
flags+=("--node-selector=")
flags+=("--output=")
two_word_flags+=("-o")
flags+=("--alsologtostderr")
flags+=("--api-version=")
flags+=("--boot-id-file=")
Expand Down Expand Up @@ -2208,6 +2210,8 @@ _openshift_admin_groups_new()
flags_with_completion=()
flags_completion=()

flags+=("--output=")
two_word_flags+=("-o")
flags+=("--alsologtostderr")
flags+=("--api-version=")
flags+=("--boot-id-file=")
Expand Down
20 changes: 17 additions & 3 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,11 @@ func (o *NewGroupOptions) AddGroup() error {
}

_, err := o.GroupClient.Create(group)
return err
if err != nil {
return err
}
if err := o.MutationOutputOptions.PrintSuccess(group, "created"); err != nil {
return err
}
return nil
}
7 changes: 7 additions & 0 deletions pkg/cmd/admin/groups/sync/cli/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -69,6 +70,9 @@ type PruneOptions 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 NewPruneOptions() *PruneOptions {
Expand Down Expand Up @@ -96,6 +100,8 @@ func NewCmdPrune(name, fullName string, f *clientcmd.Factory, out io.Writer) *co
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 @@ -187,6 +193,7 @@ func (o *PruneOptions) Run(cmd *cobra.Command, f *clientcmd.Factory) error {

Out: o.Out,
Err: os.Stderr,
MutationOutputOptions: o.MutationOutputOptions,
}

listerMapper, err := getOpenShiftGroupListerMapper(clientConfig.Host(), o)
Expand Down
23 changes: 8 additions & 15 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 @@ -337,6 +342,7 @@ func (o *SyncOptions) Run(cmd *cobra.Command, f *clientcmd.Factory) error {

Out: o.Out,
Err: os.Stderr,
MutationOutputOptions: o.MutationOutputOptions,
}

switch o.Source {
Expand Down Expand Up @@ -375,20 +381,7 @@ func (o *SyncOptions) Run(cmd *cobra.Command, f *clientcmd.Factory) error {
}

// Now we run the Syncer and report any errors
openshiftGroups, syncErrors := syncer.Sync()
if o.Confirm {
return kerrs.NewAggregate(syncErrors)
}

list := &kapi.List{}
for _, item := range openshiftGroups {
list.Items = append(list.Items, item)
}
if err := f.Factory.PrintObject(cmd, list, o.Out); err != nil {
return err
}

return kerrs.NewAggregate(syncErrors)
return kerrs.NewAggregate(syncer.Sync())
}

func buildSyncBuilder(clientConfig ldapclient.Config, syncConfig *api.LDAPSyncConfig) (SyncBuilder, error) {
Expand Down
12 changes: 11 additions & 1 deletion pkg/cmd/admin/groups/sync/grouppruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/admin/groups/sync/interfaces"
"github.com/openshift/origin/pkg/cmd/util/mutation"
)

// GroupPruner runs a prune job on Groups
Expand All @@ -33,6 +34,9 @@ type LDAPGroupPruner struct {
// Out is used to provide output while the sync job is happening
Out io.Writer
Err io.Writer

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

var _ GroupPruner = &LDAPGroupPruner{}
Expand Down Expand Up @@ -79,7 +83,13 @@ func (s *LDAPGroupPruner) Prune() []error {
}
}

fmt.Fprintf(s.Out, "group/%s\n", groupName)
// The entire prune operation is not atomic, so we need to output what we do when we do it, so if we encounter
// a failure on another group the work we have already done is accounted for.
if s.DryRun {
s.MutationOutputOptions.PrintSuccessForDescription("Group", groupName, "would be deleted")
} else {
s.MutationOutputOptions.PrintSuccessForDescription("Group", groupName, "deleted")
}
}

return errors
Expand Down
27 changes: 20 additions & 7 deletions pkg/cmd/admin/groups/sync/groupsyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/openshift/origin/pkg/auth/ldaputil"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/admin/groups/sync/interfaces"
"github.com/openshift/origin/pkg/cmd/util/mutation"
userapi "github.com/openshift/origin/pkg/user/api"
)

// GroupSyncer runs a Sync job on Groups
type GroupSyncer interface {
// Sync syncs groups in OpenShift with records from an external source
Sync() (groupsAffected []*userapi.Group, errors []error)
Sync() (errors []error)
}

// LDAPGroupSyncer sync Groups with records on an external LDAP server
Expand All @@ -43,21 +44,23 @@ type LDAPGroupSyncer struct {
// Out is used to provide output while the sync job is happening
Out io.Writer
Err io.Writer

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

var _ GroupSyncer = &LDAPGroupSyncer{}

// Sync allows the LDAPGroupSyncer to be a GroupSyncer
func (s *LDAPGroupSyncer) Sync() ([]*userapi.Group, []error) {
openshiftGroups := []*userapi.Group{}
func (s *LDAPGroupSyncer) Sync() []error {
var errors []error

// determine what to sync
glog.V(1).Infof("Listing with %v", s.GroupLister)
ldapGroupUIDs, err := s.GroupLister.ListGroups()
if err != nil {
errors = append(errors, err)
return nil, errors
return errors
}
glog.V(1).Infof("Sync ldapGroupUIDs %v", ldapGroupUIDs)

Expand Down Expand Up @@ -88,19 +91,29 @@ func (s *LDAPGroupSyncer) Sync() ([]*userapi.Group, []error) {
errors = append(errors, err)
continue
}
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)
continue
}
}

// The entire sync operation is not atomic, so we need to output what we do when we do it, so if we encounter
// a failure on another group the work we have already done is accounted for.
if s.DryRun {
if err := s.MutationOutputOptions.PrintSuccess(openshiftGroup, "would be updated"); err != nil {
errors = append(errors, err)
}
} else {
if err := s.MutationOutputOptions.PrintSuccess(openshiftGroup, "updated"); err != nil {
errors = append(errors, err)
}
}
}

return openshiftGroups, errors
return errors
}

// determineUsers determines the OpenShift Users that correspond to a list of LDAP member entries
Expand Down
11 changes: 10 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,9 @@ func (o *NewProjectOptions) Run(useNodeSelector bool) error {
return err
}

fmt.Printf("Created project %v\n", o.ProjectName)
if err := o.MutationOutputOptions.PrintSuccess(project, "created"); err != nil {
return err
}

errs := []error{}
if len(o.AdminUser) != 0 {
Expand Down
10 changes: 9 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,9 @@ func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
return err
}

fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name)
if err := o.MutationOutputOptions.PrintSuccess(secret, "created"); err != nil {
return err
}
return nil
}

Expand Down
Loading

0 comments on commit 24df569

Please sign in to comment.