-
Notifications
You must be signed in to change notification settings - Fork 916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
govc: Add -file flag to cluster.module.rm to delete a list of cluster modules read from a file #3194
govc: Add -file flag to cluster.module.rm to delete a list of cluster modules read from a file #3194
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
} | ||
chrischdi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if we should instead export a function at the library to match the error via a typecast or similar. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meant to add, we have an issue open for this: #2521 |
||
return nil | ||
} | ||
return err | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider whether you want another flag to continue processing modules on error, or to ignore "unknown module" errors.
Noting because if you end up with a failure somewhere in the 10k, you cannot simply resubmit the file but have to edit it to remove all modules that have already been deleted.
Not blocking, but a nice refinement. If needing to test this the simulator has module support, see this line