github.com/vmware/govmomi@v0.37.2/govc/cluster/module/rm.go (about)

     1  /*
     2  Copyright (c) 2022 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 module
    18  
    19  import (
    20  	"bufio"
    21  	"context"
    22  	"flag"
    23  	"os"
    24  	"strings"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/vapi/cluster"
    29  )
    30  
    31  type rm struct {
    32  	*flags.ClientFlag
    33  	ignoreNotFound bool
    34  }
    35  
    36  func init() {
    37  	cli.Register("cluster.module.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.ignoreNotFound, "ignore-not-found", false, "Treat \"404 Not Found\" as a successful delete.")
    45  }
    46  
    47  func (cmd *rm) Usage() string {
    48  	return "ID"
    49  }
    50  
    51  func (cmd *rm) Description() string {
    52  	return `Delete cluster module ID.
    53  
    54  If ID is "-", read a list from stdin.
    55  
    56  Examples:
    57    govc cluster.module.rm module_id
    58    govc cluster.module.rm - < input-file.txt`
    59  }
    60  
    61  func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
    62  	if f.NArg() != 1 {
    63  		return flag.ErrHelp
    64  	}
    65  
    66  	moduleID := f.Arg(0)
    67  
    68  	c, err := cmd.RestClient()
    69  	if err != nil {
    70  		return err
    71  	}
    72  	m := cluster.NewManager(c)
    73  
    74  	if moduleID == "-" {
    75  		scanner := bufio.NewScanner(os.Stdin)
    76  		for scanner.Scan() {
    77  			moduleID := scanner.Text()
    78  			if moduleID == "" {
    79  				continue
    80  			}
    81  			if err := cmd.deleteModule(ctx, m, moduleID); err != nil {
    82  				return err
    83  			}
    84  		}
    85  		return nil
    86  	}
    87  
    88  	return cmd.deleteModule(ctx, m, moduleID)
    89  }
    90  
    91  func (cmd *rm) deleteModule(ctx context.Context, m *cluster.Manager, moduleID string) error {
    92  	if err := m.DeleteModule(ctx, moduleID); err != nil {
    93  		if cmd.ignoreNotFound && strings.HasSuffix(err.Error(), "404 Not Found") {
    94  			return nil
    95  		}
    96  		return err
    97  	}
    98  	return nil
    99  }