github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/alpha/sync/get/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 get
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/GoogleContainerTools/kpt/commands/util"
    22  	"github.com/GoogleContainerTools/kpt/internal/docs/generated/syncdocs"
    23  	"github.com/GoogleContainerTools/kpt/internal/errors"
    24  	"github.com/GoogleContainerTools/kpt/internal/util/porch"
    25  	"github.com/spf13/cobra"
    26  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    27  	"k8s.io/cli-runtime/pkg/genericclioptions"
    28  	"k8s.io/cli-runtime/pkg/printers"
    29  	"k8s.io/kubectl/pkg/cmd/get"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  )
    32  
    33  const (
    34  	command = "cmdsync.get"
    35  )
    36  
    37  func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner {
    38  	r := &runner{
    39  		ctx:        ctx,
    40  		cfg:        rcg,
    41  		printFlags: get.NewGetPrintFlags(),
    42  	}
    43  	c := &cobra.Command{
    44  		Use:     "get NAME",
    45  		Short:   syncdocs.GetShort,
    46  		Long:    syncdocs.GetShort + "\n" + syncdocs.GetLong,
    47  		Example: syncdocs.GetExamples,
    48  		PreRunE: r.preRunE,
    49  		RunE:    r.runE,
    50  		Hidden:  porch.HidePorchCommands,
    51  	}
    52  	r.Command = c
    53  
    54  	// Create flags
    55  	r.printFlags.AddFlags(c)
    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  	// Flags
    71  	printFlags *get.PrintFlags
    72  }
    73  
    74  func (r *runner) preRunE(cmd *cobra.Command, args []string) error {
    75  	const op errors.Op = command + ".preRunE"
    76  	client, err := porch.CreateDynamicClient(r.cfg)
    77  	if err != nil {
    78  		return errors.E(op, err)
    79  	}
    80  	r.client = client
    81  	return nil
    82  }
    83  
    84  func (r *runner) runE(cmd *cobra.Command, args []string) error {
    85  	const op errors.Op = command + ".runE"
    86  
    87  	if len(args) == 0 {
    88  		return errors.E(op, "NAME is required positional argument")
    89  	}
    90  
    91  	name := args[0]
    92  	namespace := util.RootSyncNamespace
    93  	if *r.cfg.Namespace != "" {
    94  		namespace = *r.cfg.Namespace
    95  	}
    96  	key := client.ObjectKey{
    97  		Namespace: namespace,
    98  		Name:      name,
    99  	}
   100  	rs := unstructured.Unstructured{
   101  		Object: map[string]interface{}{
   102  			"apiVersion": "configsync.gke.io/v1beta1",
   103  			"kind":       "RootSync",
   104  		},
   105  	}
   106  	if err := r.client.Get(r.ctx, key, &rs); err != nil {
   107  		return errors.E(op, fmt.Errorf("cannot get %s: %v", key, err))
   108  	}
   109  
   110  	printer, err := r.printFlags.ToPrinter()
   111  	if err != nil {
   112  		return errors.E(op, err)
   113  	}
   114  
   115  	w := printers.GetNewTabWriter(cmd.OutOrStdout())
   116  
   117  	if err := printer.PrintObj(&rs, w); err != nil {
   118  		return errors.E(op, err)
   119  	}
   120  	if err := w.Flush(); err != nil {
   121  		return errors.E(op, err)
   122  	}
   123  
   124  	return nil
   125  }