Skip to content

Commit

Permalink
govc: add -file flag for cluster.module.rm
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 2, 2023
1 parent 55bf25b commit 40d8d7d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
5 changes: 4 additions & 1 deletion govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,15 @@ Options:
## cluster.module.rm

```
Usage: govc cluster.module.rm [OPTIONS] ID
Usage: govc cluster.module.rm [OPTIONS] -file= | ID
Delete cluster module ID.
When -file is specified, it reads and deletes all modules contained in the file.
Examples:
govc cluster.module.rm module_id
govc cluster.module.rm -file modules.txt
Options:
```
Expand Down
42 changes: 36 additions & 6 deletions govc/cluster/module/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package module

import (
"bufio"
"context"
"flag"
"fmt"
"os"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
Expand All @@ -27,6 +30,7 @@ import (

type rm struct {
*flags.ClientFlag
file string
}

func init() {
Expand All @@ -36,30 +40,56 @@ func init() {
func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

f.StringVar(&cmd.file, "file", "", "File containing a list of module ids to delete")
}

func (cmd *rm) Usage() string {
return "ID"
return "-file= | ID"
}

func (cmd *rm) Description() string {
return `Delete cluster module ID.
When -file is specified, it reads and deletes all modules contained in the file.
Examples:
govc cluster.module.rm module_id`
govc cluster.module.rm module_id
govc cluster.module.rm -file modules.txt`
}

func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
if !(f.NArg() == 1 && cmd.file == "") && !(f.NArg() == 0 && cmd.file != "") {
return flag.ErrHelp
}

moduleID := f.Arg(0)

c, err := cmd.RestClient()
if err != nil {
return err
}
m := cluster.NewManager(c)

if cmd.file != "" {
modulesFile, err := os.Open(cmd.file)
if err != nil {
return fmt.Errorf("error reading file %q: %w", cmd.file, err)
}
defer modulesFile.Close()

scanner := bufio.NewScanner(modulesFile)
for scanner.Scan() {
moduleID := scanner.Text()
if moduleID == "" {
continue
}
if err := m.DeleteModule(ctx, moduleID); err != nil {
return err
}
}
return nil
}

moduleID := f.Arg(0)

return cluster.NewManager(c).DeleteModule(ctx, moduleID)
return m.DeleteModule(ctx, moduleID)
}

0 comments on commit 40d8d7d

Please sign in to comment.