github.com/vmware/govmomi@v0.51.0/cli/library/session/rm.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  	"errors"
    10  	"flag"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/vapi/library"
    15  )
    16  
    17  type rm struct {
    18  	*flags.ClientFlag
    19  
    20  	cancel bool
    21  	files  bool
    22  }
    23  
    24  func init() {
    25  	cli.Register("library.session.rm", &rm{})
    26  }
    27  
    28  func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
    29  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    30  	cmd.ClientFlag.Register(ctx, f)
    31  
    32  	f.BoolVar(&cmd.cancel, "f", false, "Cancel session if active")
    33  	f.BoolVar(&cmd.files, "i", false, "Remove session item file")
    34  }
    35  
    36  func (cmd *rm) Description() string {
    37  	return `Remove a library item update session.
    38  
    39  Examples:
    40    govc library.session.rm session_id
    41    govc library.session.rm -i session_id foo.ovf`
    42  }
    43  
    44  func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
    45  	id := f.Arg(0)
    46  
    47  	c, err := cmd.RestClient()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	m := library.NewManager(c)
    53  	cancel := m.CancelLibraryItemUpdateSession
    54  	remove := m.DeleteLibraryItemUpdateSession
    55  	rmfile := m.RemoveLibraryItemUpdateSessionFile
    56  
    57  	_, err = m.GetLibraryItemUpdateSession(ctx, id)
    58  	if err != nil {
    59  		cancel = m.CancelLibraryItemDownloadSession
    60  		remove = m.DeleteLibraryItemDownloadSession
    61  		rmfile = func(context.Context, string, string) error {
    62  			return errors.New("cannot delete a download session file")
    63  		}
    64  	}
    65  
    66  	if cmd.files {
    67  		return rmfile(ctx, id, f.Arg(1))
    68  	}
    69  
    70  	if cmd.cancel {
    71  		err := cancel(ctx, id)
    72  		if err != nil {
    73  			return nil
    74  		}
    75  	}
    76  	return remove(ctx, id)
    77  }