github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/live/status/cmdstatus.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 status
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  
    21  	"github.com/GoogleContainerTools/kpt/internal/docs/generated/livedocs"
    22  	"github.com/GoogleContainerTools/kpt/internal/util/argutil"
    23  	"github.com/GoogleContainerTools/kpt/pkg/live"
    24  	kptstatus "github.com/GoogleContainerTools/kpt/pkg/status"
    25  	"github.com/spf13/cobra"
    26  	"k8s.io/kubectl/pkg/cmd/util"
    27  	"sigs.k8s.io/cli-utils/cmd/status"
    28  	"sigs.k8s.io/cli-utils/pkg/apply/poller"
    29  	"sigs.k8s.io/cli-utils/pkg/inventory"
    30  )
    31  
    32  func NewRunner(ctx context.Context, factory util.Factory,
    33  	invFactory inventory.ClientFactory, loader status.Loader) *status.Runner {
    34  	r := status.GetRunner(ctx, factory, invFactory, loader)
    35  	r.PollerFactoryFunc = pollerFactoryFunc
    36  	r.Command.Use = "status [PKG_PATH | -]"
    37  	r.Command.Short = livedocs.StatusShort
    38  	r.Command.Long = livedocs.StatusShort + "\n" + livedocs.StatusLong
    39  	r.Command.Example = livedocs.StatusExamples
    40  	return r
    41  }
    42  
    43  func NewCommand(ctx context.Context, factory util.Factory,
    44  	invFactory inventory.ClientFactory, loader status.Loader) *cobra.Command {
    45  	return NewRunner(ctx, factory, invFactory, loader).Command
    46  }
    47  
    48  func pollerFactoryFunc(f util.Factory) (poller.Poller, error) {
    49  	return kptstatus.NewStatusPoller(f)
    50  }
    51  
    52  type RGInventoryLoader struct {
    53  	factory util.Factory
    54  	ctx     context.Context
    55  }
    56  
    57  func NewRGInventoryLoader(ctx context.Context, factory util.Factory) *RGInventoryLoader {
    58  	return &RGInventoryLoader{
    59  		factory: factory,
    60  		ctx:     ctx,
    61  	}
    62  }
    63  
    64  func (rir *RGInventoryLoader) GetInvInfo(cmd *cobra.Command, args []string) (inventory.Info, error) {
    65  	if len(args) == 0 {
    66  		// default to the current working directory
    67  		cwd, err := os.Getwd()
    68  		if err != nil {
    69  			return nil, err
    70  		}
    71  		args = append(args, cwd)
    72  	}
    73  
    74  	path := args[0]
    75  	var err error
    76  	if args[0] != "-" {
    77  		path, err = argutil.ResolveSymlink(rir.ctx, path)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  	}
    82  
    83  	_, inv, err := live.Load(rir.factory, path, cmd.InOrStdin())
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	invInfo, err := live.ToInventoryInfo(inv)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return invInfo, nil
    94  }