github.com/vmware/govmomi@v0.37.2/govc/library/session/ls.go (about)

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