github.com/vmware/govmomi@v0.51.0/cli/library/probe.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package library 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "io" 12 "os" 13 "text/tabwriter" 14 15 "github.com/vmware/govmomi/cli" 16 "github.com/vmware/govmomi/cli/flags" 17 "github.com/vmware/govmomi/vapi/library" 18 ) 19 20 type probe struct { 21 *flags.ClientFlag 22 *flags.OutputFlag 23 24 fail bool 25 } 26 27 func init() { 28 cli.Register("library.probe", &probe{}, true) 29 } 30 31 func (cmd *probe) Register(ctx context.Context, f *flag.FlagSet) { 32 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 33 cmd.ClientFlag.Register(ctx, f) 34 35 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 36 cmd.OutputFlag.Register(ctx, f) 37 38 f.BoolVar(&cmd.fail, "f", false, "Fail if probe status is not success") 39 } 40 41 func (cmd *probe) Usage() string { 42 return "URI" 43 } 44 45 func (cmd *probe) Description() string { 46 return `Probes the source endpoint URI with https or http schemes. 47 48 Examples: 49 govc library.probe https://example.com/file.ova` 50 } 51 52 type probeResult struct { 53 *library.ProbeResult 54 } 55 56 func (r *probeResult) Write(w io.Writer) error { 57 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 58 59 fmt.Fprintf(tw, "Status:\t%s\n", r.Status) 60 thumbprint := r.SSLThumbprint 61 if thumbprint == "" { 62 thumbprint = "-" 63 } 64 fmt.Fprintf(tw, "Thumbprint:\t%s\n", thumbprint) 65 for _, e := range r.ErrorMessages { 66 fmt.Fprintf(tw, "%s:\t%s\n", e.ID, e.Error()) 67 } 68 69 return tw.Flush() 70 } 71 72 func (cmd *probe) Process(ctx context.Context) error { 73 if err := cmd.ClientFlag.Process(ctx); err != nil { 74 return err 75 } 76 return cmd.OutputFlag.Process(ctx) 77 } 78 79 func (cmd *probe) Run(ctx context.Context, f *flag.FlagSet) error { 80 if f.NArg() != 1 { 81 return flag.ErrHelp 82 } 83 84 c, err := cmd.RestClient() 85 if err != nil { 86 return err 87 } 88 89 m := library.NewManager(c) 90 91 p, err := m.ProbeTransferEndpoint(ctx, library.TransferEndpoint{URI: f.Arg(0)}) 92 if err != nil { 93 return err 94 } 95 96 if cmd.fail && p.Status != "SUCCESS" { 97 cmd.Out = os.Stderr 98 // using same exit code as curl -f: 99 defer os.Exit(22) 100 } 101 102 return cmd.WriteResult(&probeResult{p}) 103 }