github.com/vmware/govmomi@v0.37.2/govc/extension/register.go (about)

     1  /*
     2  Copyright (c) 2015 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 extension
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"flag"
    23  	"os"
    24  	"time"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/object"
    29  	"github.com/vmware/govmomi/vim25/types"
    30  )
    31  
    32  type register struct {
    33  	*flags.ClientFlag
    34  
    35  	update bool
    36  }
    37  
    38  func init() {
    39  	cli.Register("extension.register", &register{})
    40  }
    41  
    42  func (cmd *register) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    44  	cmd.ClientFlag.Register(ctx, f)
    45  
    46  	f.BoolVar(&cmd.update, "update", false, "Update extension")
    47  }
    48  
    49  func (cmd *register) Process(ctx context.Context) error {
    50  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	return nil
    54  }
    55  
    56  func (cmd *register) Run(ctx context.Context, f *flag.FlagSet) error {
    57  	c, err := cmd.Client()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	m, err := object.GetExtensionManager(c)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	var e types.Extension
    68  	e.Description = new(types.Description)
    69  
    70  	if err = json.NewDecoder(os.Stdin).Decode(&e); err != nil {
    71  		return err
    72  	}
    73  
    74  	e.LastHeartbeatTime = time.Now().UTC()
    75  
    76  	if cmd.update {
    77  		return m.Update(ctx, e)
    78  	}
    79  
    80  	return m.Register(ctx, e)
    81  }