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