github.com/vmware/govmomi@v0.37.2/govc/library/probe.go (about)

     1  /*
     2  Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package library
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"text/tabwriter"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  	"github.com/vmware/govmomi/vapi/library"
    30  )
    31  
    32  type probe struct {
    33  	*flags.ClientFlag
    34  	*flags.OutputFlag
    35  
    36  	fail bool
    37  }
    38  
    39  func init() {
    40  	cli.Register("library.probe", &probe{}, true)
    41  }
    42  
    43  func (cmd *probe) Register(ctx context.Context, f *flag.FlagSet) {
    44  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    45  	cmd.ClientFlag.Register(ctx, f)
    46  
    47  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    48  	cmd.OutputFlag.Register(ctx, f)
    49  
    50  	f.BoolVar(&cmd.fail, "f", false, "Fail if probe status is not success")
    51  }
    52  
    53  func (cmd *probe) Usage() string {
    54  	return "URI"
    55  }
    56  
    57  func (cmd *probe) Description() string {
    58  	return `Probes the source endpoint URI with https or http schemes.
    59  
    60  Examples:
    61    govc library.probe https://example.com/file.ova`
    62  }
    63  
    64  type probeResult struct {
    65  	*library.ProbeResult
    66  }
    67  
    68  func (r *probeResult) Write(w io.Writer) error {
    69  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    70  
    71  	fmt.Fprintf(tw, "Status:\t%s\n", r.Status)
    72  	thumbprint := r.SSLThumbprint
    73  	if thumbprint == "" {
    74  		thumbprint = "-"
    75  	}
    76  	fmt.Fprintf(tw, "Thumbprint:\t%s\n", thumbprint)
    77  	for _, e := range r.ErrorMessages {
    78  		fmt.Fprintf(tw, "%s:\t%s\n", e.ID, e.Error())
    79  	}
    80  
    81  	return tw.Flush()
    82  }
    83  
    84  func (cmd *probe) Process(ctx context.Context) error {
    85  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    86  		return err
    87  	}
    88  	return cmd.OutputFlag.Process(ctx)
    89  }
    90  
    91  func (cmd *probe) Run(ctx context.Context, f *flag.FlagSet) error {
    92  	if f.NArg() != 1 {
    93  		return flag.ErrHelp
    94  	}
    95  
    96  	c, err := cmd.RestClient()
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	m := library.NewManager(c)
   102  
   103  	p, err := m.ProbeTransferEndpoint(ctx, library.TransferEndpoint{URI: f.Arg(0)})
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	if cmd.fail && p.Status != "SUCCESS" {
   109  		cmd.Out = os.Stderr
   110  		// using same exit code as curl -f:
   111  		defer os.Exit(22)
   112  	}
   113  
   114  	return cmd.WriteResult(&probeResult{p})
   115  }