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