github.com/vmware/govmomi@v0.51.0/cli/library/session/ls.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package session 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "io" 12 "text/tabwriter" 13 14 "github.com/vmware/govmomi/cli" 15 "github.com/vmware/govmomi/cli/flags" 16 "github.com/vmware/govmomi/vapi/library" 17 ) 18 19 type ls struct { 20 *flags.ClientFlag 21 *flags.OutputFlag 22 23 files bool 24 } 25 26 func init() { 27 cli.Register("library.session.ls", &ls{}) 28 } 29 30 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { 31 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 32 cmd.ClientFlag.Register(ctx, f) 33 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 34 cmd.OutputFlag.Register(ctx, f) 35 36 f.BoolVar(&cmd.files, "i", false, "List session item files (with -json only)") 37 } 38 39 func (cmd *ls) Process(ctx context.Context) error { 40 if err := cmd.ClientFlag.Process(ctx); err != nil { 41 return err 42 } 43 return cmd.OutputFlag.Process(ctx) 44 } 45 46 func (cmd *ls) Description() string { 47 return `List library item update sessions. 48 49 Examples: 50 govc library.session.ls 51 govc library.session.ls -json | jq .` 52 } 53 54 type librarySession struct { 55 *library.Session 56 LibraryItemPath string `json:"library_item_path"` 57 } 58 59 type info struct { 60 Sessions []librarySession `json:"sessions"` 61 Files map[string]any `json:"files"` 62 kind string 63 } 64 65 func (i *info) Write(w io.Writer) error { 66 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 67 _, _ = fmt.Fprintln(tw, "ID\tItem\tType\tVersion\tProgress\tState\tExpires") 68 69 for _, s := range i.Sessions { 70 _, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%d\t%s\t%s\n", 71 s.ID, s.LibraryItemPath, i.kind, s.LibraryItemContentVersion, s.ClientProgress, s.State, 72 s.ExpirationTime.Format("2006-01-02 15:04")) 73 } 74 75 return tw.Flush() 76 } 77 78 func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { 79 c, err := cmd.RestClient() 80 if err != nil { 81 return err 82 } 83 84 m := library.NewManager(c) 85 86 kinds := []struct { 87 kind string 88 list func(context.Context) ([]string, error) 89 get func(context.Context, string) (*library.Session, error) 90 }{ 91 {"Update", m.ListLibraryItemUpdateSession, m.GetLibraryItemUpdateSession}, 92 {"Download", m.ListLibraryItemDownloadSession, m.GetLibraryItemDownloadSession}, 93 } 94 95 for _, k := range kinds { 96 ids, err := k.list(ctx) 97 if err != nil { 98 return err 99 } 100 if len(ids) == 0 { 101 continue 102 } 103 i := &info{ 104 Files: make(map[string]any), 105 kind: k.kind, 106 } 107 108 for _, id := range ids { 109 session, err := k.get(ctx, id) 110 if err != nil { 111 return err 112 } 113 var path string 114 item, err := m.GetLibraryItem(ctx, session.LibraryItemID) 115 if err == nil { 116 // can only show library path if item exists 117 lib, err := m.GetLibraryByID(ctx, item.LibraryID) 118 if err != nil { 119 return err 120 } 121 path = fmt.Sprintf("/%s/%s", lib.Name, item.Name) 122 } 123 i.Sessions = append(i.Sessions, librarySession{session, path}) 124 if !cmd.files { 125 continue 126 } 127 if k.kind == "Update" { 128 f, err := m.ListLibraryItemUpdateSessionFile(ctx, id) 129 if err != nil { 130 return err 131 } 132 i.Files[id] = f 133 } else { 134 f, err := m.ListLibraryItemDownloadSessionFile(ctx, id) 135 if err != nil { 136 return err 137 } 138 i.Files[id] = f 139 } 140 } 141 142 err = cmd.WriteResult(i) 143 if err != nil { 144 return err 145 } 146 } 147 return nil 148 }