@@ -3,7 +3,6 @@ package cmd
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "io"
7
6
"strings"
8
7
"sync"
9
8
"time"
@@ -16,6 +15,7 @@ import (
16
15
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
17
16
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
18
17
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
18
+ "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
19
19
20
20
buildapi "github.com/openshift/origin/pkg/build/apis/build"
21
21
buildclient "github.com/openshift/origin/pkg/build/client"
@@ -56,96 +56,92 @@ var (
56
56
57
57
// CancelBuildOptions contains all the options for running the CancelBuild cli command.
58
58
type CancelBuildOptions struct {
59
- In io.Reader
60
- Out , ErrOut io.Writer
61
-
62
59
DumpLogs bool
63
60
Restart bool
64
61
States []string
65
62
Namespace string
66
63
BuildNames []string
67
64
68
- HasError bool
69
- ReportError func (error )
70
- Mapper meta.RESTMapper
71
- Client buildinternalclient.Interface
72
- BuildClient buildtypedclient.BuildResourceInterface
73
- BuildLister buildlister.BuildLister
65
+ HasError bool
66
+ ReportError func (error )
67
+ PrinterCancel printers.ResourcePrinter
68
+ PrinterRestart printers.ResourcePrinter
69
+ Mapper meta.RESTMapper
70
+ Client buildinternalclient.Interface
71
+ BuildClient buildtypedclient.BuildResourceInterface
72
+ BuildLister buildlister.BuildLister
74
73
75
74
// timeout is used by unit tests to shorten the polling period
76
75
timeout time.Duration
76
+
77
+ genericclioptions.IOStreams
78
+ }
79
+
80
+ func NewCancelBuildOptions (streams genericclioptions.IOStreams ) * CancelBuildOptions {
81
+ return & CancelBuildOptions {
82
+ IOStreams : streams ,
83
+ States : []string {"new" , "pending" , "running" },
84
+ }
77
85
}
78
86
79
87
// NewCmdCancelBuild implements the OpenShift cli cancel-build command
80
88
func NewCmdCancelBuild (name , baseName string , f kcmdutil.Factory , streams genericclioptions.IOStreams ) * cobra.Command {
81
- o := & CancelBuildOptions {}
82
-
89
+ o := NewCancelBuildOptions (streams )
83
90
cmd := & cobra.Command {
84
91
Use : fmt .Sprintf ("%s (BUILD | BUILDCONFIG)" , name ),
85
92
Short : "Cancel running, pending, or new builds" ,
86
93
Long : cancelBuildLong ,
87
94
Example : fmt .Sprintf (cancelBuildExample , baseName , name ),
88
95
SuggestFor : []string {"builds" , "stop-build" },
89
96
Run : func (cmd * cobra.Command , args []string ) {
90
- kcmdutil .CheckErr (o .Complete (f , cmd , args , streams .In , streams .Out , streams .ErrOut ))
97
+ kcmdutil .CheckErr (o .Complete (f , cmd , args ))
98
+ kcmdutil .CheckErr (o .Validate ())
91
99
kcmdutil .CheckErr (o .RunCancelBuild ())
92
100
},
93
101
}
94
102
95
103
cmd .Flags ().StringSliceVar (& o .States , "state" , o .States , "Only cancel builds in this state" )
96
104
cmd .Flags ().BoolVar (& o .DumpLogs , "dump-logs" , o .DumpLogs , "Specify if the build logs for the cancelled build should be shown." )
97
105
cmd .Flags ().BoolVar (& o .Restart , "restart" , o .Restart , "Specify if a new build should be created after the current build is cancelled." )
106
+
98
107
return cmd
99
108
}
100
109
101
110
// Complete completes all the required options.
102
- func (o * CancelBuildOptions ) Complete (f kcmdutil.Factory , cmd * cobra.Command , args []string , in io.Reader , out , errout io.Writer ) error {
103
- o .In = in
104
- o .Out = out
105
- o .ErrOut = errout
111
+ func (o * CancelBuildOptions ) Complete (f kcmdutil.Factory , cmd * cobra.Command , args []string ) error {
112
+ if len (args ) == 0 {
113
+ return fmt .Errorf ("build or a buildconfig name is required" )
114
+ }
115
+
106
116
o .ReportError = func (err error ) {
107
117
o .HasError = true
108
118
fmt .Fprintf (o .ErrOut , "error: %s\n " , err .Error ())
109
119
}
110
120
121
+ // FIXME: this double printers should not be necessary
122
+ o .PrinterCancel = & printers.NamePrinter {Operation : "cancelled" }
123
+ o .PrinterRestart = & printers.NamePrinter {Operation : "restarted" }
124
+
111
125
if o .timeout .Seconds () == 0 {
112
126
o .timeout = 30 * time .Second
113
127
}
114
128
115
- if len (args ) == 0 {
116
- return kcmdutil .UsageErrorf (cmd , "Must pass a name of a build or a buildconfig to cancel" )
117
- }
118
-
119
- namespace , _ , err := f .ToRawKubeConfigLoader ().Namespace ()
129
+ var err error
130
+ o .Namespace , _ , err = f .ToRawKubeConfigLoader ().Namespace ()
120
131
if err != nil {
121
132
return err
122
133
}
123
134
124
- if len (o .States ) == 0 {
125
- // If --state is not specified, set the default to "new", "pending" and
126
- // "running".
127
- o .States = []string {"new" , "pending" , "running" }
128
- } else {
129
- for _ , state := range o .States {
130
- if len (state ) > 0 && ! isStateCancellable (state ) {
131
- return kcmdutil .UsageErrorf (cmd , "The '--state' flag has invalid value. Must be one of 'new', 'pending', or 'running'" )
132
- }
133
- }
134
- }
135
-
136
135
config , err := f .ToRESTConfig ()
137
136
if err != nil {
138
137
return err
139
138
}
140
- client , err : = buildinternalclient .NewForConfig (config )
139
+ o . Client , err = buildinternalclient .NewForConfig (config )
141
140
if err != nil {
142
141
return err
143
142
}
144
-
145
- o .Namespace = namespace
146
- o .Client = client
147
- o .BuildLister = buildclient .NewClientBuildLister (client .Build ())
148
- o .BuildClient = client .Build ().Builds (namespace )
143
+ o .BuildLister = buildclient .NewClientBuildLister (o .Client .Build ())
144
+ o .BuildClient = o .Client .Build ().Builds (o .Namespace )
149
145
o .Mapper , err = f .ToRESTMapper ()
150
146
if err != nil {
151
147
return err
@@ -176,10 +172,19 @@ func (o *CancelBuildOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, ar
176
172
return nil
177
173
}
178
174
175
+ func (o * CancelBuildOptions ) Validate () error {
176
+ for _ , state := range o .States {
177
+ if len (state ) > 0 && ! isStateCancellable (state ) {
178
+ return fmt .Errorf ("invalid --state flag value, must be one of 'new', 'pending', or 'running'" )
179
+ }
180
+ }
181
+
182
+ return nil
183
+ }
184
+
179
185
// RunCancelBuild implements all the necessary functionality for CancelBuild.
180
186
func (o * CancelBuildOptions ) RunCancelBuild () error {
181
187
var builds []* buildapi.Build
182
-
183
188
for _ , name := range o .BuildNames {
184
189
build , err := o .BuildClient .Get (name , metav1.GetOptions {})
185
190
if err != nil {
@@ -252,7 +257,10 @@ func (o *CancelBuildOptions) RunCancelBuild() error {
252
257
return
253
258
}
254
259
255
- kcmdutil .PrintSuccess (false , o .Out , build , false , "cancelled" )
260
+ if err := o .PrinterCancel .PrintObj (kcmdutil .AsDefaultVersionedOrOriginal (build , nil ), o .Out ); err != nil {
261
+ o .ReportError (fmt .Errorf ("build %s/%s failed to print: %v" , build .Namespace , build .Name , err ))
262
+ return
263
+ }
256
264
}(b )
257
265
}
258
266
wg .Wait ()
@@ -265,7 +273,10 @@ func (o *CancelBuildOptions) RunCancelBuild() error {
265
273
o .ReportError (fmt .Errorf ("build %s/%s failed to restart: %v" , b .Namespace , b .Name , err ))
266
274
continue
267
275
}
268
- kcmdutil .PrintSuccess (false , o .Out , build , false , fmt .Sprintf ("restarted build %q" , b .Name ))
276
+ if err := o .PrinterRestart .PrintObj (kcmdutil .AsDefaultVersionedOrOriginal (b , nil ), o .Out ); err != nil {
277
+ o .ReportError (fmt .Errorf ("build %s/%s failed to print: %v" , build .Namespace , build .Name , err ))
278
+ continue
279
+ }
269
280
}
270
281
}
271
282
0 commit comments