github.com/vmware/govmomi@v0.43.0/govc/library/trust/info.go (about)

     1  /*
     2  Copyright (c) 2022-2024 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 trust
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"io"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/object"
    27  	"github.com/vmware/govmomi/vapi/library"
    28  )
    29  
    30  type info struct {
    31  	*flags.ClientFlag
    32  	*flags.OutputFlag
    33  }
    34  
    35  func init() {
    36  	cli.Register("library.trust.info", &info{})
    37  }
    38  
    39  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    41  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    42  	cmd.ClientFlag.Register(ctx, f)
    43  	cmd.OutputFlag.Register(ctx, f)
    44  }
    45  
    46  func (cmd *info) Process(ctx context.Context) error {
    47  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    48  		return err
    49  	}
    50  	return nil
    51  }
    52  
    53  func (cmd *info) Usage() string {
    54  	return "ID"
    55  }
    56  
    57  func (cmd *info) Description() string {
    58  	return `Display trusted certificate info.
    59  
    60  Examples:
    61    govc library.trust.info vmware_signed`
    62  }
    63  
    64  type infoResultsWriter struct {
    65  	TrustedCertificateInfo *library.TrustedCertificate `json:"info,omitempty"`
    66  }
    67  
    68  func (r infoResultsWriter) Write(w io.Writer) error {
    69  	var info object.HostCertificateInfo
    70  	_, err := info.FromPEM([]byte(r.TrustedCertificateInfo.Text))
    71  	if err != nil {
    72  		return err
    73  	}
    74  	return info.Write(w)
    75  }
    76  
    77  func (r infoResultsWriter) Dump() interface{} {
    78  	return r.TrustedCertificateInfo
    79  }
    80  
    81  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    82  	if f.NArg() != 1 {
    83  		return flag.ErrHelp
    84  	}
    85  
    86  	c, err := cmd.RestClient()
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	cert, err := library.NewManager(c).GetTrustedCertificate(ctx, f.Arg(0))
    92  	if err != nil {
    93  		return err
    94  	}
    95  	return cmd.WriteResult(&infoResultsWriter{cert})
    96  }