github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/live/livecmd.go (about)

     1  // Copyright 2020 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 live
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  
    21  	"github.com/GoogleContainerTools/kpt/commands/live/apply"
    22  	"github.com/GoogleContainerTools/kpt/commands/live/destroy"
    23  	initialization "github.com/GoogleContainerTools/kpt/commands/live/init"
    24  	"github.com/GoogleContainerTools/kpt/commands/live/installrg"
    25  	"github.com/GoogleContainerTools/kpt/commands/live/migrate"
    26  	"github.com/GoogleContainerTools/kpt/commands/live/status"
    27  	"github.com/GoogleContainerTools/kpt/commands/util"
    28  	"github.com/GoogleContainerTools/kpt/internal/docs/generated/livedocs"
    29  	"github.com/GoogleContainerTools/kpt/pkg/live"
    30  	"github.com/spf13/cobra"
    31  	"k8s.io/cli-runtime/pkg/genericclioptions"
    32  	"k8s.io/klog/v2"
    33  	"sigs.k8s.io/cli-utils/pkg/manifestreader"
    34  )
    35  
    36  func GetCommand(ctx context.Context, _, version string) *cobra.Command {
    37  	liveCmd := &cobra.Command{
    38  		Use:   "live",
    39  		Short: livedocs.LiveShort,
    40  		Long:  livedocs.LiveShort + "\n" + livedocs.LiveLong,
    41  	}
    42  
    43  	ioStreams := genericclioptions.IOStreams{
    44  		In:     os.Stdin,
    45  		Out:    os.Stdout,
    46  		ErrOut: os.Stderr,
    47  	}
    48  
    49  	f := util.NewFactory(liveCmd, version)
    50  	invFactory := live.NewClusterClientFactory()
    51  	loader := status.NewRGInventoryLoader(ctx, f)
    52  
    53  	// Init command which updates a Kptfile for the ResourceGroup inventory object.
    54  	klog.V(2).Infoln("init command updates Kptfile for ResourceGroup inventory")
    55  	initCmd := initialization.NewCommand(ctx, f, ioStreams)
    56  	applyCmd := apply.NewCommand(ctx, f, ioStreams, false)
    57  	destroyCmd := destroy.NewCommand(ctx, f, ioStreams)
    58  	statusCmd := status.NewCommand(ctx, f, invFactory, loader)
    59  	installRGCmd := installrg.NewCommand(ctx, f, ioStreams)
    60  	liveCmd.AddCommand(initCmd, applyCmd, destroyCmd, statusCmd, installRGCmd)
    61  
    62  	// Add the migrate command to change from ConfigMap to ResourceGroup inventory
    63  	// object.
    64  	klog.V(2).Infoln("adding kpt live migrate command")
    65  	// TODO: Remove the loader implementation for ConfigMap once we remove the
    66  	// migrate command.
    67  	cmLoader := manifestreader.NewManifestLoader(f)
    68  	migrateCmd := migrate.NewCommand(ctx, f, cmLoader, ioStreams)
    69  
    70  	liveCmd.AddCommand(migrateCmd)
    71  
    72  	return liveCmd
    73  }