github.com/vmware/govmomi@v0.43.0/govc/host/date/change.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 date
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type change struct {
    31  	*flags.HostSystemFlag
    32  
    33  	types.HostNtpConfig
    34  	types.HostDateTimeConfig
    35  	date string
    36  }
    37  
    38  func init() {
    39  	cli.Register("host.date.change", &change{})
    40  }
    41  
    42  type serverConfig types.HostNtpConfig
    43  
    44  func (s *serverConfig) String() string {
    45  	return strings.Join(s.Server, ",")
    46  }
    47  
    48  func (s *serverConfig) Set(v string) error {
    49  	s.Server = append(s.Server, v)
    50  	return nil
    51  }
    52  
    53  func (cmd *change) Register(ctx context.Context, f *flag.FlagSet) {
    54  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    55  	cmd.HostSystemFlag.Register(ctx, f)
    56  
    57  	f.Var((*serverConfig)(&cmd.HostNtpConfig), "server", "IP or FQDN for NTP server(s)")
    58  	f.StringVar(&cmd.TimeZone, "tz", "", "Change timezone of the host")
    59  	f.StringVar(&cmd.date, "date", "", "Update the date/time on the host")
    60  }
    61  
    62  func (cmd *change) Description() string {
    63  	return `Change date and time for HOST.
    64  
    65  Examples:
    66    govc host.date.change -date "$(date -u)"
    67    govc host.date.change -server time.vmware.com
    68    govc host.service enable ntpd
    69    govc host.service start ntpd`
    70  }
    71  
    72  func (cmd *change) Process(ctx context.Context) error {
    73  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    74  		return err
    75  	}
    76  	return nil
    77  }
    78  
    79  func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
    80  	host, err := cmd.HostSystem()
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	s, err := host.ConfigManager().DateTimeSystem(ctx)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	if cmd.date != "" {
    91  		d, err := time.Parse(time.UnixDate, cmd.date)
    92  		if err != nil {
    93  			return err
    94  		}
    95  		return s.Update(ctx, d)
    96  	}
    97  
    98  	if len(cmd.HostNtpConfig.Server) > 0 {
    99  		cmd.NtpConfig = &cmd.HostNtpConfig
   100  	}
   101  
   102  	return s.UpdateConfig(ctx, cmd.HostDateTimeConfig)
   103  }