github.com/vmware/govmomi@v0.43.0/govc/host/date/info.go (about) 1 /* 2 Copyright (c) 2016-2023 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 "fmt" 23 "io" 24 "strings" 25 "text/tabwriter" 26 "time" 27 28 "github.com/vmware/govmomi/govc/cli" 29 "github.com/vmware/govmomi/govc/flags" 30 "github.com/vmware/govmomi/govc/host/service" 31 "github.com/vmware/govmomi/vim25/mo" 32 "github.com/vmware/govmomi/vim25/types" 33 ) 34 35 type info struct { 36 *flags.HostSystemFlag 37 *flags.OutputFlag 38 } 39 40 func init() { 41 cli.Register("host.date.info", &info{}) 42 } 43 44 func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { 45 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 46 cmd.HostSystemFlag.Register(ctx, f) 47 48 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 49 cmd.OutputFlag.Register(ctx, f) 50 } 51 52 func (cmd *info) Description() string { 53 return `Display date and time info for HOST.` 54 } 55 56 func (cmd *info) Process(ctx context.Context) error { 57 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 58 return err 59 } 60 if err := cmd.OutputFlag.Process(ctx); err != nil { 61 return err 62 } 63 return nil 64 } 65 66 type dateInfo struct { 67 types.HostDateTimeInfo 68 Service *types.HostService `json:"service"` 69 Current *time.Time `json:"current"` 70 } 71 72 func (info *dateInfo) servers() string { 73 if len(info.NtpConfig.Server) == 0 { 74 return "None" 75 } 76 return strings.Join(info.NtpConfig.Server, ", ") 77 } 78 79 func (info *dateInfo) Write(w io.Writer) error { 80 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 81 82 fmt.Fprintf(tw, "Current date and time:\t%s\n", info.Current.Format(time.UnixDate)) 83 if info.Service != nil { 84 fmt.Fprintf(tw, "NTP client status:\t%s\n", service.Policy(*info.Service)) 85 fmt.Fprintf(tw, "NTP service status:\t%s\n", service.Status(*info.Service)) 86 } 87 fmt.Fprintf(tw, "NTP servers:\t%s\n", info.servers()) 88 89 return tw.Flush() 90 } 91 92 func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { 93 host, err := cmd.HostSystem() 94 if err != nil { 95 return err 96 } 97 98 s, err := host.ConfigManager().DateTimeSystem(ctx) 99 if err != nil { 100 return err 101 } 102 103 var hs mo.HostDateTimeSystem 104 if err = s.Properties(ctx, s.Reference(), nil, &hs); err != nil { 105 return nil 106 } 107 108 ss, err := host.ConfigManager().ServiceSystem(ctx) 109 if err != nil { 110 return err 111 } 112 113 services, err := ss.Service(ctx) 114 if err != nil { 115 return err 116 } 117 118 res := &dateInfo{HostDateTimeInfo: hs.DateTimeInfo} 119 120 for i, service := range services { 121 if service.Key == "ntpd" { 122 res.Service = &services[i] 123 break 124 } 125 } 126 127 res.Current, err = s.Query(ctx) 128 if err != nil { 129 return err 130 } 131 132 return cmd.WriteResult(res) 133 }