github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/alpha/rpkg/approve/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 approve
    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 = "cmdrpkgapprove"
    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:     "approve PACKAGE",
    49  		Short:   rpkgdocs.ApproveShort,
    50  		Long:    rpkgdocs.ApproveShort + "\n" + rpkgdocs.ApproveLong,
    51  		Example: rpkgdocs.ApproveExamples,
    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  	client, err := porch.CreateRESTClient(r.cfg)
    74  	if err != nil {
    75  		return errors.E(op, err)
    76  	}
    77  	r.client = client
    78  	return nil
    79  }
    80  
    81  func (r *runner) runE(cmd *cobra.Command, args []string) error {
    82  	const op errors.Op = command + ".runE"
    83  	var messages []string
    84  
    85  	namespace := *r.cfg.Namespace
    86  
    87  	for _, name := range args {
    88  		if err := porch.UpdatePackageRevisionApproval(r.ctx, r.client, client.ObjectKey{
    89  			Namespace: namespace,
    90  			Name:      name,
    91  		}, v1alpha1.PackageRevisionLifecyclePublished); err != nil {
    92  			messages = append(messages, err.Error())
    93  			fmt.Fprintf(r.Command.ErrOrStderr(), "%s failed (%s)\n", name, err)
    94  		} else {
    95  			fmt.Fprintf(r.Command.OutOrStderr(), "%s approved\n", name)
    96  		}
    97  	}
    98  
    99  	if len(messages) > 0 {
   100  		return errors.E(op, fmt.Errorf("errors:\n  %s", strings.Join(messages, "\n  ")))
   101  	}
   102  
   103  	return nil
   104  }