github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/alpha/rpkg/del/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 del
    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  	porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  
    28  	"github.com/spf13/cobra"
    29  	"k8s.io/cli-runtime/pkg/genericclioptions"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  )
    32  
    33  const (
    34  	command = "cmdrpkgdel"
    35  )
    36  
    37  func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner {
    38  	r := &runner{
    39  		ctx: ctx,
    40  		cfg: rcg,
    41  	}
    42  	c := &cobra.Command{
    43  		Use:        "del PACKAGE",
    44  		Aliases:    []string{"delete"},
    45  		SuggestFor: []string{},
    46  		Short:      rpkgdocs.DelShort,
    47  		Long:       rpkgdocs.DelShort + "\n" + rpkgdocs.DelLong,
    48  		Example:    rpkgdocs.DelExamples,
    49  		PreRunE:    r.preRunE,
    50  		RunE:       r.runE,
    51  		Hidden:     porch.HidePorchCommands,
    52  	}
    53  	r.Command = c
    54  
    55  	// Create flags
    56  
    57  	return r
    58  }
    59  
    60  func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command {
    61  	return newRunner(ctx, rcg).Command
    62  }
    63  
    64  type runner struct {
    65  	ctx     context.Context
    66  	cfg     *genericclioptions.ConfigFlags
    67  	client  client.Client
    68  	Command *cobra.Command
    69  }
    70  
    71  func (r *runner) preRunE(cmd *cobra.Command, args []string) error {
    72  	const op errors.Op = command + ".preRunE"
    73  
    74  	client, err := porch.CreateClient(r.cfg)
    75  	if err != nil {
    76  		return errors.E(op, err)
    77  	}
    78  	r.client = client
    79  	return nil
    80  }
    81  
    82  func (r *runner) runE(cmd *cobra.Command, args []string) error {
    83  	const op errors.Op = command + ".runE"
    84  	var messages []string
    85  
    86  	for _, pkg := range args {
    87  		pr := &porchapi.PackageRevision{
    88  			TypeMeta: metav1.TypeMeta{
    89  				Kind:       "PackageRevision",
    90  				APIVersion: porchapi.SchemeGroupVersion.Identifier(),
    91  			},
    92  			ObjectMeta: metav1.ObjectMeta{
    93  				Namespace: *r.cfg.Namespace,
    94  				Name:      pkg,
    95  			},
    96  		}
    97  
    98  		if err := r.client.Delete(r.ctx, pr); err != nil {
    99  			messages = append(messages, err.Error())
   100  			fmt.Fprintf(r.Command.ErrOrStderr(), "%s failed (%s)\n", pkg, err)
   101  		} else {
   102  			fmt.Fprintf(r.Command.OutOrStderr(), "%s deleted\n", pkg)
   103  		}
   104  	}
   105  
   106  	if len(messages) > 0 {
   107  		return errors.E(op, fmt.Errorf("errors:\n  %s", strings.Join(messages, "\n  ")))
   108  	}
   109  
   110  	return nil
   111  }