github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/live/status/fake-loader.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 status
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  
    21  	"github.com/GoogleContainerTools/kpt/internal/util/argutil"
    22  	"github.com/GoogleContainerTools/kpt/pkg/live"
    23  	"github.com/spf13/cobra"
    24  	"k8s.io/kubectl/pkg/cmd/util"
    25  	"sigs.k8s.io/cli-utils/cmd/status"
    26  	"sigs.k8s.io/cli-utils/pkg/inventory"
    27  	"sigs.k8s.io/cli-utils/pkg/object"
    28  )
    29  
    30  type FakeLoader struct {
    31  	ctx       context.Context
    32  	factory   util.Factory
    33  	InvClient *inventory.FakeClient
    34  }
    35  
    36  var _ status.Loader = &FakeLoader{}
    37  
    38  func NewFakeLoader(ctx context.Context, f util.Factory, objs object.ObjMetadataSet) *FakeLoader {
    39  	return &FakeLoader{
    40  		ctx:       ctx,
    41  		factory:   f,
    42  		InvClient: inventory.NewFakeClient(objs),
    43  	}
    44  }
    45  
    46  func (r *FakeLoader) GetInvInfo(cmd *cobra.Command, args []string) (inventory.Info, error) {
    47  	if len(args) == 0 {
    48  		// default to the current working directory
    49  		cwd, err := os.Getwd()
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  		args = append(args, cwd)
    54  	}
    55  
    56  	path := args[0]
    57  	var err error
    58  	if args[0] != "-" {
    59  		path, err = argutil.ResolveSymlink(r.ctx, path)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  	}
    64  
    65  	_, inv, err := live.Load(r.factory, path, cmd.InOrStdin())
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	invInfo, err := live.ToInventoryInfo(inv)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	return invInfo, nil
    76  }