github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/alpha/wasm/pull/command.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 pull
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/GoogleContainerTools/kpt/internal/docs/generated/wasmdocs"
    24  	"github.com/GoogleContainerTools/kpt/internal/errors"
    25  	"github.com/GoogleContainerTools/kpt/pkg/wasm"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  const (
    30  	command = "cmdwasmpull"
    31  )
    32  
    33  func newRunner(ctx context.Context) *runner {
    34  	r := &runner{
    35  		ctx: ctx,
    36  	}
    37  	c := &cobra.Command{
    38  		Use:     "pull [IMAGE] [LOCAL_PATH]",
    39  		Short:   wasmdocs.PullShort,
    40  		Long:    wasmdocs.PullShort + "\n" + wasmdocs.PullLong,
    41  		Example: wasmdocs.PullExamples,
    42  		RunE:    r.runE,
    43  	}
    44  	r.Command = c
    45  	return r
    46  }
    47  
    48  func NewCommand(ctx context.Context) *cobra.Command {
    49  	return newRunner(ctx).Command
    50  }
    51  
    52  type runner struct {
    53  	ctx           context.Context
    54  	Command       *cobra.Command
    55  	storageClient *wasm.Client
    56  }
    57  
    58  func (r *runner) runE(_ *cobra.Command, args []string) error {
    59  	const op errors.Op = command + ".runE"
    60  
    61  	if len(args) != 2 {
    62  		return errors.E(op, "2 positional arguments (a OCI image and a local path) required")
    63  	}
    64  
    65  	var err error
    66  	if r.storageClient == nil {
    67  		r.storageClient, err = wasm.NewClient(filepath.Join(os.TempDir(), "kpt"))
    68  		if err != nil {
    69  			return err
    70  		}
    71  	}
    72  
    73  	wasmImg := args[0]
    74  	fileName := args[1]
    75  	wasmFileReadCloser, err := r.storageClient.LoadWasm(r.ctx, wasmImg)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	defer wasmFileReadCloser.Close()
    80  
    81  	data, err := io.ReadAll(wasmFileReadCloser)
    82  	if err != nil {
    83  		return errors.E(op, "unable to read wasm contents")
    84  	}
    85  	err = os.MkdirAll(filepath.Dir(fileName), 0755)
    86  	if err != nil {
    87  		return errors.E(op, "unable to create directory %q: %w", filepath.Dir(fileName), err)
    88  	}
    89  	if err = os.WriteFile(fileName, data, 0666); err != nil {
    90  		return errors.E(op, "unable to write to file", fileName)
    91  	}
    92  	return nil
    93  }