github.com/vmware/govmomi@v0.43.0/govc/disk/snapshot/ls.go (about) 1 /* 2 Copyright (c) 2018-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 snapshot 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "os" 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/vim25/types" 31 "github.com/vmware/govmomi/vslm" 32 ) 33 34 type ls struct { 35 *flags.DatastoreFlag 36 long bool 37 } 38 39 func init() { 40 cli.Register("disk.snapshot.ls", &ls{}) 41 } 42 43 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { 44 cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx) 45 cmd.DatastoreFlag.Register(ctx, f) 46 47 f.BoolVar(&cmd.long, "l", false, "Long listing format") 48 } 49 50 func (cmd *ls) Usage() string { 51 return "ID" 52 } 53 54 func (cmd *ls) Description() string { 55 return `List snapshots for disk ID on DS. 56 57 Examples: 58 govc snapshot.disk.ls -l 9b06a8b-d047-4d3c-b15b-43ea9608b1a6` 59 } 60 61 type lsResult struct { 62 Info *types.VStorageObjectSnapshotInfo `json:"info"` 63 cmd *ls 64 } 65 66 func (r *lsResult) Write(w io.Writer) error { 67 tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) 68 69 for _, o := range r.Info.Snapshots { 70 _, _ = fmt.Fprintf(tw, "%s\t%s", o.Id.Id, o.Description) 71 if r.cmd.long { 72 created := o.CreateTime.Format(time.Stamp) 73 _, _ = fmt.Fprintf(tw, "\t%s", created) 74 } 75 _, _ = fmt.Fprintln(tw) 76 } 77 78 return tw.Flush() 79 } 80 81 func (r *lsResult) Dump() interface{} { 82 return r.Info 83 } 84 85 func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { 86 ds, err := cmd.Datastore() 87 if err != nil { 88 return err 89 } 90 91 m := vslm.NewObjectManager(ds.Client()) 92 info, err := m.RetrieveSnapshotInfo(ctx, ds, f.Arg(0)) 93 if err != nil { 94 return err 95 } 96 97 res := lsResult{Info: info, cmd: cmd} 98 99 return cmd.WriteResult(&res) 100 }