@@ -2,21 +2,20 @@ package set
22
33import (
44 "fmt"
5- "io"
6- "os"
75
86 "github.com/spf13/cobra"
7+
98 "k8s.io/apimachinery/pkg/api/meta"
10- "k8s.io/apimachinery/pkg/runtime"
119 "k8s.io/apimachinery/pkg/types"
10+ utilerrors "k8s.io/apimachinery/pkg/util/errors"
1211 "k8s.io/client-go/dynamic"
1312 "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1413 kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1514 "k8s.io/kubernetes/pkg/kubectl/genericclioptions"
1615 "k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
16+ "k8s.io/kubernetes/pkg/printers"
1717
18- buildapi "github.com/openshift/origin/pkg/build/apis/build"
19- "github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
18+ buildv1 "github.com/openshift/api/build/v1"
2019 "github.com/openshift/origin/pkg/oc/util/ocscheme"
2120)
2221
@@ -52,126 +51,99 @@ var (
5251)
5352
5453type BuildHookOptions struct {
55- Out io.Writer
56- Err io.Writer
57-
58- Builder * resource.Builder
59- Infos []* resource.Info
60-
61- Encoder runtime.Encoder
62-
63- Filenames []string
64- Selector string
65- All bool
66- Output string
67-
68- Cmd * cobra.Command
69-
70- Local bool
71- ShortOutput bool
72- Mapper meta.RESTMapper
73- Client dynamic.Interface
74-
75- PrintObject func ([]* resource.Info ) error
54+ PrintFlags * genericclioptions.PrintFlags
7655
56+ Selector string
57+ All bool
58+ Output string
59+ Local bool
7760 Script string
7861 Entrypoint bool
7962 Remove bool
8063 PostCommit bool
8164
82- Command []string
65+ Mapper meta.RESTMapper
66+ Client dynamic.Interface
67+ Printer printers.ResourcePrinter
68+ Builder func () * resource.Builder
69+ Namespace string
70+ ExplicitNamespace bool
71+ Command []string
72+ Resources []string
73+ DryRun bool
74+
75+ resource.FilenameOptions
76+ genericclioptions.IOStreams
77+ }
78+
79+ func NewBuildHookOptions (streams genericclioptions.IOStreams ) * BuildHookOptions {
80+ return & BuildHookOptions {
81+ PrintFlags : genericclioptions .NewPrintFlags ("hooks updated" ).WithTypeSetter (ocscheme .PrintingInternalScheme ),
82+ IOStreams : streams ,
83+ }
8384}
8485
8586// NewCmdBuildHook implements the set build-hook command
8687func NewCmdBuildHook (fullName string , f kcmdutil.Factory , streams genericclioptions.IOStreams ) * cobra.Command {
87- options := & BuildHookOptions {
88- Out : streams .Out ,
89- Err : streams .ErrOut ,
90- }
88+ o := NewBuildHookOptions (streams )
9189 cmd := & cobra.Command {
9290 Use : "build-hook BUILDCONFIG --post-commit [--command] [--script] -- CMD" ,
9391 Short : "Update a build hook on a build config" ,
9492 Long : buildHookLong ,
9593 Example : fmt .Sprintf (buildHookExample , fullName ),
9694 Run : func (cmd * cobra.Command , args []string ) {
97- kcmdutil .CheckErr (options .Complete (f , cmd , args ))
98- kcmdutil .CheckErr (options .Validate ())
99- if err := options .Run (); err != nil {
100- // TODO: move me to kcmdutil
101- if err == kcmdutil .ErrExit {
102- os .Exit (1 )
103- }
104- kcmdutil .CheckErr (err )
105- }
95+ kcmdutil .CheckErr (o .Complete (f , cmd , args ))
96+ kcmdutil .CheckErr (o .Validate ())
97+ kcmdutil .CheckErr (o .Run ())
10698 },
10799 }
108-
109- kcmdutil .AddPrinterFlags (cmd )
110- cmd .Flags ().StringVarP (& options .Selector , "selector" , "l" , options .Selector , "Selector (label query) to filter build configs" )
111- cmd .Flags ().BoolVar (& options .All , "all" , options .All , "If true, select all build configs in the namespace" )
112- cmd .Flags ().StringSliceVarP (& options .Filenames , "filename" , "f" , options .Filenames , "Filename, directory, or URL to file to use to edit the resource." )
113-
114- cmd .Flags ().BoolVar (& options .PostCommit , "post-commit" , options .PostCommit , "If true, set the post-commit build hook on a build config" )
115- cmd .Flags ().BoolVar (& options .Entrypoint , "command" , options .Entrypoint , "If true, set the entrypoint of the hook container to the given command" )
116- cmd .Flags ().StringVar (& options .Script , "script" , options .Script , "Specify a script to run for the build-hook" )
117- cmd .Flags ().BoolVar (& options .Remove , "remove" , options .Remove , "If true, remove the build hook." )
118- cmd .Flags ().BoolVar (& options .Local , "local" , false , "If true, set image will NOT contact api-server but run locally." )
119-
120- cmd .MarkFlagFilename ("filename" , "yaml" , "yml" , "json" )
100+ usage := "to use to edit the resource"
101+ kcmdutil .AddFilenameOptionFlags (cmd , & o .FilenameOptions , usage )
102+ cmd .Flags ().StringVarP (& o .Selector , "selector" , "l" , o .Selector , "Selector (label query) to filter build configs" )
103+ cmd .Flags ().BoolVar (& o .All , "all" , o .All , "If true, select all build configs in the namespace" )
104+ cmd .Flags ().BoolVar (& o .PostCommit , "post-commit" , o .PostCommit , "If true, set the post-commit build hook on a build config" )
105+ cmd .Flags ().BoolVar (& o .Entrypoint , "command" , o .Entrypoint , "If true, set the entrypoint of the hook container to the given command" )
106+ cmd .Flags ().StringVar (& o .Script , "script" , o .Script , "Specify a script to run for the build-hook" )
107+ cmd .Flags ().BoolVar (& o .Remove , "remove" , o .Remove , "If true, remove the build hook." )
108+ cmd .Flags ().BoolVar (& o .Local , "local" , o .Local , "If true, set image will NOT contact api-server but run locally." )
109+
110+ o .PrintFlags .AddFlags (cmd )
121111 kcmdutil .AddDryRunFlag (cmd )
122112
123113 return cmd
124114}
125115
126116func (o * BuildHookOptions ) Complete (f kcmdutil.Factory , cmd * cobra.Command , args []string ) error {
127- resources : = args
117+ o . Resources = args
128118 if i := cmd .ArgsLenAtDash (); i != - 1 {
129- resources = args [:i ]
119+ o . Resources = args [:i ]
130120 o .Command = args [i :]
131121 }
132122 if len (o .Filenames ) == 0 && len (args ) < 1 {
133123 return kcmdutil .UsageErrorf (cmd , "one or more build configs must be specified as <name> or <resource>/<name>" )
134124 }
135125
136- cmdNamespace , explicit , err := f .ToRawKubeConfigLoader ().Namespace ()
126+ var err error
127+ o .Namespace , o .ExplicitNamespace , err = f .ToRawKubeConfigLoader ().Namespace ()
137128 if err != nil {
138129 return err
139130 }
140131
141- o .Cmd = cmd
142-
143- mapper , err := f .ToRESTMapper ()
132+ o .DryRun = kcmdutil .GetDryRunFlag (cmd )
133+ o .Mapper , err = f .ToRESTMapper ()
144134 if err != nil {
145135 return err
146136 }
147- o .Builder = f .NewBuilder ().
148- WithScheme (ocscheme .ReadingInternalScheme ).
149- LocalParam (o .Local ).
150- ContinueOnError ().
151- NamespaceParam (cmdNamespace ).DefaultNamespace ().
152- FilenameParam (explicit , & resource.FilenameOptions {Recursive : false , Filenames : o .Filenames }).
153- LabelSelectorParam (o .Selector ).
154- ResourceNames ("buildconfigs" , resources ... ).
155- Flatten ()
137+ o .Builder = f .NewBuilder
156138
157- if ! o .Local {
158- o .Builder = o .Builder .
159- LabelSelectorParam (o .Selector ).
160- ResourceNames ("buildconfigs" , resources ... )
161- if o .All {
162- o .Builder .ResourceTypes ("buildconfigs" ).SelectAllParam (o .All )
163- }
139+ if o .DryRun {
140+ o .PrintFlags .Complete ("%s (dry run)" )
164141 }
165-
166- o .Output = kcmdutil .GetFlagString (cmd , "output" )
167- o .PrintObject = func (infos []* resource.Info ) error {
168- return clientcmd .PrintResourceInfos (cmd , infos , o .Out )
142+ o .Printer , err = o .PrintFlags .ToPrinter ()
143+ if err != nil {
144+ return err
169145 }
170146
171- o .Encoder = kcmdutil .InternalVersionJSONEncoder ()
172- o .ShortOutput = kcmdutil .GetFlagString (cmd , "output" ) == "name"
173- o .Mapper = mapper
174-
175147 clientConfig , err := f .ToRESTConfig ()
176148 if err != nil {
177149 return err
@@ -185,7 +157,6 @@ func (o *BuildHookOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args
185157}
186158
187159func (o * BuildHookOptions ) Validate () error {
188-
189160 if ! o .PostCommit {
190161 return fmt .Errorf ("you must specify a type of hook to set" )
191162 }
@@ -212,18 +183,32 @@ func (o *BuildHookOptions) Validate() error {
212183}
213184
214185func (o * BuildHookOptions ) Run () error {
215- infos := o .Infos
216- singleItemImplied := len (o .Infos ) <= 1
217- if o .Builder != nil {
218- loaded , err := o .Builder .Do ().IntoSingleItemImplied (& singleItemImplied ).Infos ()
219- if err != nil {
220- return err
186+ b := o .Builder ().
187+ WithScheme (ocscheme .ReadingInternalScheme ).
188+ LocalParam (o .Local ).
189+ ContinueOnError ().
190+ NamespaceParam (o .Namespace ).DefaultNamespace ().
191+ FilenameParam (o .ExplicitNamespace , & o .FilenameOptions ).
192+ Flatten ()
193+
194+ if ! o .Local {
195+ b = b .
196+ LabelSelectorParam (o .Selector ).
197+ ResourceNames ("buildconfigs" , o .Resources ... ).
198+ Latest ()
199+ if o .All {
200+ b = b .ResourceTypes ("buildconfigs" ).SelectAllParam (o .All )
221201 }
222- infos = loaded
223202 }
224203
225- patches := CalculatePatches (infos , o .Encoder , func (info * resource.Info ) (bool , error ) {
226- bc , ok := info .Object .(* buildapi.BuildConfig )
204+ singleItemImplied := false
205+ infos , err := b .Do ().IntoSingleItemImplied (& singleItemImplied ).Infos ()
206+ if err != nil {
207+ return err
208+ }
209+
210+ patches := CalculatePatchesExternal (infos , func (info * resource.Info ) (bool , error ) {
211+ bc , ok := info .Object .(* buildv1.BuildConfig )
227212 if ! ok {
228213 return false , nil
229214 }
@@ -235,39 +220,40 @@ func (o *BuildHookOptions) Run() error {
235220 return fmt .Errorf ("%s/%s is not a build config" , infos [0 ].Mapping .Resource , infos [0 ].Name )
236221 }
237222
238- if len (o .Output ) > 0 || o .Local || kcmdutil .GetDryRunFlag (o .Cmd ) {
239- return o .PrintObject (infos )
240- }
241-
242- failed := false
223+ allErrs := []error {}
243224 for _ , patch := range patches {
244225 info := patch .Info
245226 if patch .Err != nil {
246- fmt . Fprintf ( o . Err , "error: %s/%s %v\n " , info .Mapping .Resource , info .Name , patch .Err )
227+ allErrs = append ( allErrs , fmt . Errorf ( "error: %s/%s %v\n " , info .Mapping .Resource , info .Name , patch .Err ) )
247228 continue
248229 }
249230
250231 if string (patch .Patch ) == "{}" || len (patch .Patch ) == 0 {
251- fmt .Fprintf (o .Err , "info: %s %q was not changed\n " , info .Mapping .Resource , info .Name )
232+ fmt .Fprintf (o .ErrOut , "info: %s %q was not changed\n " , info .Mapping .Resource , info .Name )
233+ continue
234+ }
235+
236+ if o .Local || o .DryRun {
237+ if err := o .Printer .PrintObj (info .Object , o .Out ); err != nil {
238+ allErrs = append (allErrs , err )
239+ }
252240 continue
253241 }
254242
255243 actual , err := o .Client .Resource (info .Mapping .Resource ).Namespace (info .Namespace ).Patch (info .Name , types .StrategicMergePatchType , patch .Patch )
256244 if err != nil {
257- fmt .Fprintf (o .Err , "error: %v\n " , err )
258- failed = true
245+ allErrs = append (allErrs , fmt .Errorf ("failed to patch build hook: %v\n " , err ))
259246 continue
260247 }
261248
262- kcmdutil .PrintSuccess (o .ShortOutput , o .Out , actual , false , "updated" )
263- }
264- if failed {
265- return kcmdutil .ErrExit
249+ if err := o .Printer .PrintObj (actual , o .Out ); err != nil {
250+ allErrs = append (allErrs , err )
251+ }
266252 }
267- return nil
253+ return utilerrors . NewAggregate ( allErrs )
268254}
269255
270- func (o * BuildHookOptions ) updateBuildConfig (bc * buildapi .BuildConfig ) {
256+ func (o * BuildHookOptions ) updateBuildConfig (bc * buildv1 .BuildConfig ) {
271257 if o .Remove {
272258 bc .Spec .PostCommit .Args = nil
273259 bc .Spec .PostCommit .Command = nil
0 commit comments