From e17d88424967e2e883b6544a90606db7a94d1b0a Mon Sep 17 00:00:00 2001 From: Christian Schlotter Date: Tue, 1 Aug 2023 19:32:40 +0200 Subject: [PATCH] govc: add -file flag for cluster.module.rm --- govc/USAGE.md | 4 ++++ govc/cluster/module/rm.go | 38 ++++++++++++++++++++++++++++++++++++-- govc/test/cluster.bats | 24 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/govc/USAGE.md b/govc/USAGE.md index 11093c590..d5d73cc69 100644 --- a/govc/USAGE.md +++ b/govc/USAGE.md @@ -586,10 +586,14 @@ Usage: govc cluster.module.rm [OPTIONS] ID Delete cluster module ID. +If ID is "-", read a list from stdin. + Examples: govc cluster.module.rm module_id + govc cluster.module.rm - < input-file.txt Options: + -ignore-not-found=false Treat "404 Not Found" as a successful delete. ``` ## cluster.module.vm.add diff --git a/govc/cluster/module/rm.go b/govc/cluster/module/rm.go index 7938c1ec3..f222fa3b3 100644 --- a/govc/cluster/module/rm.go +++ b/govc/cluster/module/rm.go @@ -17,8 +17,11 @@ limitations under the License. package module import ( + "bufio" "context" "flag" + "os" + "strings" "github.com/vmware/govmomi/govc/cli" "github.com/vmware/govmomi/govc/flags" @@ -27,6 +30,7 @@ import ( type rm struct { *flags.ClientFlag + ignoreNotFound bool } func init() { @@ -36,6 +40,8 @@ func init() { func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) { cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) cmd.ClientFlag.Register(ctx, f) + + f.BoolVar(&cmd.ignoreNotFound, "ignore-not-found", false, "Treat \"404 Not Found\" as a successful delete.") } func (cmd *rm) Usage() string { @@ -45,8 +51,11 @@ func (cmd *rm) Usage() string { func (cmd *rm) Description() string { return `Delete cluster module ID. +If ID is "-", read a list from stdin. + Examples: - govc cluster.module.rm module_id` + govc cluster.module.rm module_id + govc cluster.module.rm - < input-file.txt` } func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error { @@ -60,6 +69,31 @@ func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error { if err != nil { return err } + m := cluster.NewManager(c) + + if moduleID == "-" { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + moduleID := scanner.Text() + if moduleID == "" { + continue + } + if err := cmd.deleteModule(ctx, m, moduleID); err != nil { + return err + } + } + return nil + } - return cluster.NewManager(c).DeleteModule(ctx, moduleID) + return cmd.deleteModule(ctx, m, moduleID) +} + +func (cmd *rm) deleteModule(ctx context.Context, m *cluster.Manager, moduleID string) error { + if err := m.DeleteModule(ctx, moduleID); err != nil { + if cmd.ignoreNotFound && strings.HasSuffix(err.Error(), "404 Not Found") { + return nil + } + return err + } + return nil } diff --git a/govc/test/cluster.bats b/govc/test/cluster.bats index 6102a6139..de96a021e 100755 --- a/govc/test/cluster.bats +++ b/govc/test/cluster.bats @@ -326,6 +326,30 @@ _EOF_ run govc cluster.module.ls -id $id assert_failure + + run govc cluster.module.rm "does_not_exist" + assert_failure + + run govc cluster.module.rm --ignore-not-found "does_not_exist" + assert_success + + run govc cluster.module.create -cluster DC0_C0 + assert_success + + id="$output" + + run govc cluster.module.create -cluster DC0_C0 + assert_success + + id2="$output" + + run govc cluster.module.rm --ignore-not-found "-" <<_EOF_ +$id +does_not_exist +$id2 +_EOF_ + assert_success + } @test "cluster.mv" {