github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/alpha/rpkg/reject/command.go (about) 1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package reject 16 17 import ( 18 "context" 19 "fmt" 20 "strings" 21 22 "github.com/GoogleContainerTools/kpt/internal/docs/generated/rpkgdocs" 23 "github.com/GoogleContainerTools/kpt/internal/errors" 24 "github.com/GoogleContainerTools/kpt/internal/util/porch" 25 "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 26 "github.com/spf13/cobra" 27 "k8s.io/cli-runtime/pkg/genericclioptions" 28 "k8s.io/client-go/rest" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 ) 31 32 const ( 33 command = "cmdrpkgreject" 34 ) 35 36 func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command { 37 return newRunner(ctx, rcg).Command 38 } 39 40 func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner { 41 r := &runner{ 42 ctx: ctx, 43 cfg: rcg, 44 client: nil, 45 } 46 47 c := &cobra.Command{ 48 Use: "reject PACKAGE", 49 Short: rpkgdocs.RejectShort, 50 Long: rpkgdocs.RejectShort + "\n" + rpkgdocs.RejectLong, 51 Example: rpkgdocs.RejectExamples, 52 PreRunE: r.preRunE, 53 RunE: r.runE, 54 Hidden: porch.HidePorchCommands, 55 } 56 r.Command = c 57 58 return r 59 } 60 61 type runner struct { 62 ctx context.Context 63 cfg *genericclioptions.ConfigFlags 64 client rest.Interface 65 Command *cobra.Command 66 67 // Flags 68 } 69 70 func (r *runner) preRunE(cmd *cobra.Command, args []string) error { 71 const op errors.Op = command + ".preRunE" 72 73 if len(args) < 1 { 74 return errors.E(op, "PACKAGE_REVISION is a required positional argument") 75 } 76 77 client, err := porch.CreateRESTClient(r.cfg) 78 if err != nil { 79 return errors.E(op, err) 80 } 81 r.client = client 82 return nil 83 } 84 85 func (r *runner) runE(cmd *cobra.Command, args []string) error { 86 const op errors.Op = command + ".runE" 87 var messages []string 88 89 namespace := *r.cfg.Namespace 90 91 for _, name := range args { 92 if err := porch.UpdatePackageRevisionApproval(r.ctx, r.client, client.ObjectKey{ 93 Namespace: namespace, 94 Name: name, 95 }, v1alpha1.PackageRevisionLifecycleDraft); err != nil { 96 messages = append(messages, err.Error()) 97 fmt.Fprintf(r.Command.ErrOrStderr(), "%s failed (%s)\n", name, err) 98 } else { 99 fmt.Fprintf(r.Command.OutOrStderr(), "%s rejected\n", name) 100 } 101 } 102 103 if len(messages) > 0 { 104 return errors.E(op, fmt.Errorf("errors:\n %s", strings.Join(messages, "\n "))) 105 } 106 107 return nil 108 }