本文整理匯總了Golang中flag.FlagSet.Fault方法的典型用法代碼示例。如果您正苦於以下問題:Golang FlagSet.Fault方法的具體用法?Golang FlagSet.Fault怎麽用?Golang FlagSet.Fault使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類flag.FlagSet
的用法示例。
在下文中一共展示了FlagSet.Fault方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Run
func (cmd *destroy) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() == 0 {
return flag.ErrHelp
}
finder, err := cmd.Finder()
if err != nil {
return err
}
for _, arg := range f.Args() {
vapps, err := finder.VirtualAppList(context.TODO(), arg)
if err != nil {
if _, ok := err.(*find.NotFoundError); ok {
// Ignore if vapp cannot be found
continue
}
return err
}
for _, vapp := range vapps {
powerOff := func() error {
task, err := vapp.PowerOffVApp_Task(context.TODO(), false)
if err != nil {
return err
}
err = task.Wait(context.TODO())
if err != nil {
// it's safe to ignore if the vapp is already powered off
if f, ok := err.(types.HasFault); ok {
switch f.Fault().(type) {
case *types.InvalidPowerState:
return nil
}
}
return err
}
return nil
}
if err := powerOff(); err != nil {
return err
}
destroy := func() error {
task, err := vapp.Destroy(context.TODO())
if err != nil {
return err
}
err = task.Wait(context.TODO())
if err != nil {
return err
}
return nil
}
if err := destroy(); err != nil {
return err
}
}
}
return nil
}
示例2: Run
func (cmd *add) Run(f *flag.FlagSet) error {
var ctx = context.Background()
var parent *object.Folder
client, err := cmd.Client()
if err != nil {
return err
}
if cmd.parent == "" {
dc, err := cmd.Datacenter()
if err != nil {
return err
}
folders, err := dc.Folders(ctx)
if err != nil {
return err
}
parent = folders.HostFolder
} else {
finder, err := cmd.Finder()
if err != nil {
return err
}
mo, err := finder.ManagedObjectList(ctx, cmd.parent)
if err != nil {
return err
}
if len(mo) == 0 {
return errors.New("parent does not resolve to object")
}
if len(mo) > 1 {
return errors.New("parent resolves to more than one object")
}
ref := mo[0].Object.Reference()
if ref.Type != "Folder" {
return errors.New("parent does not resolve to folder")
}
parent = object.NewFolder(client, ref)
}
req := types.AddStandaloneHost_Task{
This: parent.Reference(),
Spec: types.HostConnectSpec{
HostName: cmd.host,
UserName: cmd.username,
Password: cmd.password,
SslThumbprint: cmd.fingerprint,
},
AddConnected: cmd.connect,
}
res, err := methods.AddStandaloneHost_Task(ctx, client, &req)
if err != nil {
return err
}
task := object.NewTask(client, res.Returnval)
_, err = task.WaitForResult(ctx, nil)
if err != nil {
f, ok := err.(types.HasFault)
if !ok {
return err
}
switch fault := f.Fault().(type) {
case *types.SSLVerifyFault:
// Add fingerprint to error message
return fmt.Errorf("%s Fingerprint is %s.", err.Error(), fault.Thumbprint)
default:
return err
}
}
return nil
}