github.com/vmware/govmomi@v0.37.2/govc/object/reload.go (about)

     1  /*
     2  Copyright (c) 2016 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 object
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  	"github.com/vmware/govmomi/vim25/methods"
    26  	"github.com/vmware/govmomi/vim25/types"
    27  )
    28  
    29  type reload struct {
    30  	*flags.DatacenterFlag
    31  }
    32  
    33  func init() {
    34  	cli.Register("object.reload", &reload{})
    35  }
    36  
    37  func (cmd *reload) Register(ctx context.Context, f *flag.FlagSet) {
    38  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    39  	cmd.DatacenterFlag.Register(ctx, f)
    40  }
    41  
    42  func (cmd *reload) Usage() string {
    43  	return "PATH..."
    44  }
    45  
    46  func (cmd *reload) Description() string {
    47  	return `Reload managed object state.
    48  
    49  Examples:
    50    govc datastore.upload $vm.vmx $vm/$vm.vmx
    51    govc object.reload /dc1/vm/$vm`
    52  }
    53  
    54  func (cmd *reload) Process(ctx context.Context) error {
    55  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    56  		return err
    57  	}
    58  	return nil
    59  }
    60  
    61  func (cmd *reload) Run(ctx context.Context, f *flag.FlagSet) error {
    62  	if f.NArg() == 0 {
    63  		return flag.ErrHelp
    64  	}
    65  
    66  	c, err := cmd.Client()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	objs, err := cmd.ManagedObjects(ctx, f.Args())
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	for _, obj := range objs {
    77  		req := types.Reload{
    78  			This: obj,
    79  		}
    80  
    81  		_, err = methods.Reload(ctx, c, &req)
    82  		if err != nil {
    83  			return err
    84  		}
    85  	}
    86  
    87  	return nil
    88  }