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

     1  /*
     2  Copyright (c) 2022-2022 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  	"bytes"
    21  	"context"
    22  	"flag"
    23  	"io"
    24  	"os"
    25  	"path/filepath"
    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 create struct {
    33  	*flags.ClientFlag
    34  }
    35  
    36  func init() {
    37  	cli.Register("library.trust.create", &create{})
    38  }
    39  
    40  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    42  	cmd.ClientFlag.Register(ctx, f)
    43  }
    44  
    45  func (cmd *create) Usage() string {
    46  	return "FILE"
    47  }
    48  
    49  func (cmd *create) Description() string {
    50  	return `Add a certificate to content library trust store.
    51  
    52  If FILE name is "-", read certificate from stdin.
    53  
    54  Examples:
    55    govc library.trust.create cert.pem
    56    govc about.cert -show -u wp-content-int.vmware.com | govc library.trust.create -`
    57  }
    58  
    59  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    60  	c, err := cmd.RestClient()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	var cert string
    66  
    67  	name := f.Arg(0)
    68  	if name == "-" || name == "" {
    69  		var buf bytes.Buffer
    70  		if _, err := io.Copy(&buf, os.Stdin); err != nil {
    71  			return err
    72  		}
    73  		cert = buf.String()
    74  	} else {
    75  		b, err := os.ReadFile(filepath.Clean(name))
    76  		if err != nil {
    77  			return err
    78  		}
    79  		cert = string(b)
    80  	}
    81  
    82  	return library.NewManager(c).CreateTrustedCertificate(ctx, cert)
    83  }