github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/alpha/repo/repocmd.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 repo
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  	"fmt"
    21  
    22  	"github.com/GoogleContainerTools/kpt/commands/alpha/repo/get"
    23  	"github.com/GoogleContainerTools/kpt/commands/alpha/repo/reg"
    24  	"github.com/GoogleContainerTools/kpt/commands/alpha/repo/unreg"
    25  	"github.com/GoogleContainerTools/kpt/internal/docs/generated/repodocs"
    26  	"github.com/GoogleContainerTools/kpt/internal/util/porch"
    27  	"github.com/spf13/cobra"
    28  	"k8s.io/cli-runtime/pkg/genericclioptions"
    29  	"k8s.io/client-go/rest"
    30  )
    31  
    32  func NewCommand(ctx context.Context, version string) *cobra.Command {
    33  	repo := &cobra.Command{
    34  		Use:     "repo",
    35  		Aliases: []string{"repository"},
    36  		Short:   "[Alpha] " + repodocs.RepoShort,
    37  		Long:    "[Alpha] " + repodocs.RepoLong,
    38  		RunE: func(cmd *cobra.Command, args []string) error {
    39  			h, err := cmd.Flags().GetBool("help")
    40  			if err != nil {
    41  				return err
    42  			}
    43  			if h {
    44  				return cmd.Help()
    45  			}
    46  			return cmd.Usage()
    47  		},
    48  		Hidden: porch.HidePorchCommands,
    49  	}
    50  
    51  	pf := repo.PersistentFlags()
    52  
    53  	kubeflags := genericclioptions.NewConfigFlags(true)
    54  	kubeflags.AddFlags(pf)
    55  
    56  	kubeflags.WrapConfigFn = func(rc *rest.Config) *rest.Config {
    57  		rc.UserAgent = fmt.Sprintf("kpt/%s", version)
    58  		return rc
    59  	}
    60  
    61  	pf.AddGoFlagSet(flag.CommandLine)
    62  
    63  	repo.AddCommand(
    64  		reg.NewCommand(ctx, kubeflags),
    65  		get.NewCommand(ctx, kubeflags),
    66  		unreg.NewCommand(ctx, kubeflags),
    67  	)
    68  
    69  	return repo
    70  }