Skip to content

Commit

Permalink
handle cmds with custom timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Sep 30, 2016
1 parent 813f688 commit cba9df7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pkg/cmd/cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type VersionOptions struct {
ClientConfig kclientcmd.ClientConfig
Clients func() (*client.Client, *kclient.Client, error)

Timeout time.Duration

IsServer bool
PrintEtcdVersion bool
PrintClientFeatures bool
Expand All @@ -49,7 +51,7 @@ func NewCmdVersion(fullName string, f *clientcmd.Factory, out io.Writer, options
Run: func(cmd *cobra.Command, args []string) {
options.BaseName = fullName

if err := options.Complete(f, out); err != nil {
if err := options.Complete(cmd, f, out); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}

Expand All @@ -62,13 +64,19 @@ func NewCmdVersion(fullName string, f *clientcmd.Factory, out io.Writer, options
return cmd
}

func (o *VersionOptions) Complete(f *clientcmd.Factory, out io.Writer) error {
func (o *VersionOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, out io.Writer) error {
o.Out = out

if f == nil {
return nil
}

// apply global timeout, if zero, apply custom 10s timeout
o.Timeout = kcmdutil.GetFlagDuration(cmd, "timeout")
if o.Timeout == 0 {
o.Timeout = time.Duration(10 * time.Second)
}

o.Clients = f.Clients
o.ClientConfig = f.OpenShiftClientConfig
return nil
Expand Down Expand Up @@ -101,9 +109,6 @@ func (o VersionOptions) RunVersion() error {
return nil
}

// max amount of time we want to wait for server to respond
timeout := 10 * time.Second

done := make(chan error)
oVersion := ""
kVersion := ""
Expand Down Expand Up @@ -169,7 +174,7 @@ func (o VersionOptions) RunVersion() error {
if closed && err != nil {
return err
}
case <-time.After(timeout):
case <-time.After(o.Timeout):
return fmt.Errorf("%s", "error: server took too long to respond with version information.")
}

Expand Down

0 comments on commit cba9df7

Please sign in to comment.