gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/register.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  	"fmt"
    21  	"path/filepath"
    22  
    23  	"gitee.com/zhaochuninhefei/fabric-ca-gm/lib"
    24  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    25  	"github.com/pkg/errors"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  func (c *ClientCmd) newRegisterCommand() *cobra.Command {
    30  	registerCmd := &cobra.Command{
    31  		Use:   "register",
    32  		Short: "Register an identity",
    33  		Long:  "Register an identity with Fabric CA server",
    34  		// PreRunE block for this command will check to make sure enrollment
    35  		// information exists before running the command
    36  		PreRunE: func(cmd *cobra.Command, args []string) error {
    37  			if len(args) > 0 {
    38  				return errors.Errorf(extraArgsError, args, cmd.UsageString())
    39  			}
    40  
    41  			err := c.ConfigInit()
    42  			if err != nil {
    43  				return err
    44  			}
    45  
    46  			log.Debugf("Client configuration settings: %+v", c.clientCfg)
    47  
    48  			return nil
    49  		},
    50  		RunE: func(cmd *cobra.Command, args []string) error {
    51  			err := c.runRegister()
    52  			if err != nil {
    53  				return err
    54  			}
    55  
    56  			return nil
    57  		},
    58  	}
    59  	return registerCmd
    60  }
    61  
    62  // The client register main logic
    63  func (c *ClientCmd) runRegister() error {
    64  	log.Debug("Entered runRegister")
    65  
    66  	client := lib.Client{
    67  		HomeDir: filepath.Dir(c.cfgFileName),
    68  		Config:  c.clientCfg,
    69  	}
    70  
    71  	id, err := client.LoadMyIdentity()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	c.clientCfg.ID.CAName = c.clientCfg.CAName
    77  	resp, err := id.Register(&c.clientCfg.ID)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	fmt.Printf("Password: %s\n", resp.Secret)
    83  
    84  	return nil
    85  }