gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/gencsr.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 command
    18  
    19  import (
    20  	"path/filepath"
    21  
    22  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    23  	"github.com/pkg/errors"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func (c *ClientCmd) newGenCsrCommand() *cobra.Command {
    28  	// initCmd represents the init command
    29  	gencsrCmd := &cobra.Command{
    30  		Use:   "gencsr",
    31  		Short: "Generate a CSR",
    32  		Long:  "Generate a Certificate Signing Request for an identity",
    33  		PreRunE: func(cmd *cobra.Command, args []string) error {
    34  			if len(args) > 0 {
    35  				return errors.Errorf(extraArgsError, args, cmd.UsageString())
    36  			}
    37  
    38  			err := c.ConfigInit()
    39  			if err != nil {
    40  				return err
    41  			}
    42  
    43  			log.Debugf("Client configuration settings: %+v", c.clientCfg)
    44  
    45  			return nil
    46  		},
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			err := c.runGenCSR(cmd)
    49  			if err != nil {
    50  				return err
    51  			}
    52  			return nil
    53  		},
    54  	}
    55  	gencsrCmd.Flags().StringVar(&c.csrCommonName, "csr.cn", "", "The common name for the certificate signing request")
    56  	return gencsrCmd
    57  }
    58  
    59  // The gencsr main logic
    60  func (c *ClientCmd) runGenCSR(cmd *cobra.Command) error {
    61  	log.Debug("Entered runGenCSR")
    62  
    63  	if c.csrCommonName != "" {
    64  		c.clientCfg.CSR.CN = c.csrCommonName
    65  	}
    66  
    67  	err := c.clientCfg.GenCSR(filepath.Dir(c.cfgFileName))
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }