github.com/vmware/govmomi@v0.37.2/govc/library/cp.go (about)

     1  /*
     2  Copyright (c) 2020 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package library
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/vapi/library"
    27  )
    28  
    29  type cp struct {
    30  	*flags.ClientFlag
    31  
    32  	library.Item
    33  }
    34  
    35  func init() {
    36  	cli.Register("library.cp", &cp{})
    37  }
    38  
    39  func (cmd *cp) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    41  	cmd.ClientFlag.Register(ctx, f)
    42  
    43  	f.StringVar(&cmd.Name, "n", "", "Library item name")
    44  }
    45  
    46  func (cmd *cp) Usage() string {
    47  	return "SRC DST"
    48  }
    49  
    50  func (cmd *cp) Description() string {
    51  	return `Copy SRC library item to DST library.
    52  Examples:
    53    govc library.cp /my-content/my-item /my-other-content
    54    govc library.cp -n my-item2 /my-content/my-item /my-other-content`
    55  }
    56  
    57  func (cmd *cp) Run(ctx context.Context, f *flag.FlagSet) error {
    58  	srcPath := f.Arg(0)
    59  	dstPath := f.Arg(1)
    60  
    61  	c, err := cmd.RestClient()
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	m := library.NewManager(c)
    67  	src, err := flags.ContentLibraryItem(ctx, c, srcPath)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	dst, err := flags.ContentLibrary(ctx, c, dstPath)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	cmd.LibraryID = dst.ID
    78  	if cmd.Name == "" {
    79  		cmd.Name = src.Name
    80  	}
    81  
    82  	id, err := m.CopyLibraryItem(ctx, src, cmd.Item)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	fmt.Println(id)
    88  
    89  	return nil
    90  }