github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/alpha/rpkg/rpkgcmd.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 rpkg
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  	"fmt"
    21  
    22  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/approve"
    23  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/clone"
    24  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/copy"
    25  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/del"
    26  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/get"
    27  	initialization "github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/init"
    28  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/propose"
    29  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/proposedelete"
    30  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/pull"
    31  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/push"
    32  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/reject"
    33  	"github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/update"
    34  	"github.com/GoogleContainerTools/kpt/internal/docs/generated/rpkgdocs"
    35  	"github.com/GoogleContainerTools/kpt/internal/util/porch"
    36  	"github.com/spf13/cobra"
    37  	"k8s.io/cli-runtime/pkg/genericclioptions"
    38  	"k8s.io/client-go/rest"
    39  )
    40  
    41  func NewCommand(ctx context.Context, version string) *cobra.Command {
    42  	repo := &cobra.Command{
    43  		Use:     "rpkg",
    44  		Aliases: []string{"rpackage"},
    45  		Short:   "[Alpha] " + rpkgdocs.RpkgShort,
    46  		Long:    "[Alpha] " + rpkgdocs.RpkgLong,
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			h, err := cmd.Flags().GetBool("help")
    49  			if err != nil {
    50  				return err
    51  			}
    52  			if h {
    53  				return cmd.Help()
    54  			}
    55  			return cmd.Usage()
    56  		},
    57  		Hidden: porch.HidePorchCommands,
    58  	}
    59  
    60  	pf := repo.PersistentFlags()
    61  
    62  	kubeflags := genericclioptions.NewConfigFlags(true)
    63  	kubeflags.AddFlags(pf)
    64  
    65  	kubeflags.WrapConfigFn = func(rc *rest.Config) *rest.Config {
    66  		rc.UserAgent = fmt.Sprintf("kpt/%s", version)
    67  		return rc
    68  	}
    69  
    70  	pf.AddGoFlagSet(flag.CommandLine)
    71  
    72  	repo.AddCommand(
    73  		get.NewCommand(ctx, kubeflags),
    74  		pull.NewCommand(ctx, kubeflags),
    75  		push.NewCommand(ctx, kubeflags),
    76  		clone.NewCommand(ctx, kubeflags),
    77  		initialization.NewCommand(ctx, kubeflags),
    78  		propose.NewCommand(ctx, kubeflags),
    79  		approve.NewCommand(ctx, kubeflags),
    80  		reject.NewCommand(ctx, kubeflags),
    81  		del.NewCommand(ctx, kubeflags),
    82  		copy.NewCommand(ctx, kubeflags),
    83  		update.NewCommand(ctx, kubeflags),
    84  		proposedelete.NewCommand(ctx, kubeflags),
    85  	)
    86  
    87  	return repo
    88  }