github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/alpha/rpkg/proposedelete/command.go (about) 1 // Copyright 2022 The kpt Authors 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 proposedelete 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 "sigs.k8s.io/controller-runtime/pkg/client" 29 ) 30 31 const ( 32 command = "cmdrpkgpropose-delete" 33 ) 34 35 func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner { 36 r := &runner{ 37 ctx: ctx, 38 cfg: rcg, 39 } 40 c := &cobra.Command{ 41 Use: "propose-delete PACKAGE", 42 Aliases: []string{"propose-del"}, 43 Short: rpkgdocs.ProposeDeleteShort, 44 Long: rpkgdocs.ProposeDeleteShort + "\n" + rpkgdocs.ProposeDeleteLong, 45 Example: rpkgdocs.ProposeDeleteExamples, 46 SuggestFor: []string{}, 47 PreRunE: r.preRunE, 48 RunE: r.runE, 49 Hidden: porch.HidePorchCommands, 50 } 51 r.Command = c 52 53 // Create flags 54 55 return r 56 } 57 58 func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command { 59 return newRunner(ctx, rcg).Command 60 } 61 62 type runner struct { 63 ctx context.Context 64 cfg *genericclioptions.ConfigFlags 65 client client.Client 66 Command *cobra.Command 67 } 68 69 func (r *runner) preRunE(_ *cobra.Command, _ []string) error { 70 const op errors.Op = command + ".preRunE" 71 72 client, err := porch.CreateClientWithFlags(r.cfg) 73 if err != nil { 74 return errors.E(op, err) 75 } 76 r.client = client 77 return nil 78 } 79 80 func (r *runner) runE(_ *cobra.Command, args []string) error { 81 const op errors.Op = command + ".runE" 82 var messages []string 83 namespace := *r.cfg.Namespace 84 85 for _, name := range args { 86 pr := &v1alpha1.PackageRevision{} 87 if err := r.client.Get(r.ctx, client.ObjectKey{ 88 Namespace: namespace, 89 Name: name, 90 }, pr); err != nil { 91 return errors.E(op, err) 92 } 93 94 switch pr.Spec.Lifecycle { 95 case v1alpha1.PackageRevisionLifecyclePublished: 96 // ok 97 case v1alpha1.PackageRevisionLifecycleDeletionProposed: 98 fmt.Fprintf(r.Command.OutOrStderr(), "%s is already proposed for deletion\n", name) 99 continue 100 default: 101 msg := fmt.Sprintf("can only propose published packages for deletion; package %s is not published", name) 102 messages = append(messages, msg) 103 fmt.Fprintln(r.Command.ErrOrStderr(), msg) 104 continue 105 } 106 107 pr.Spec.Lifecycle = v1alpha1.PackageRevisionLifecycleDeletionProposed 108 if err := r.client.Update(r.ctx, pr); err != nil { 109 messages = append(messages, err.Error()) 110 fmt.Fprintf(r.Command.ErrOrStderr(), "%s failed (%s)\n", name, err) 111 } else { 112 fmt.Fprintf(r.Command.OutOrStderr(), "%s proposed for deletion\n", name) 113 } 114 } 115 116 if len(messages) > 0 { 117 return errors.E(op, fmt.Errorf("errors:\n %s", strings.Join(messages, "\n "))) 118 } 119 120 return nil 121 }