Skip to content

Commit

Permalink
Merge "[FAB-9258] Create interface to help with unit-tests"
Browse files Browse the repository at this point in the history
  • Loading branch information
hacera-jonathan authored and Gerrit Code Review committed Apr 22, 2018
2 parents 0183722 + 6b16ad8 commit 6e186ea
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 22 deletions.
12 changes: 6 additions & 6 deletions cmd/fabric-ca-client/affiliation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *ClientCmd) newListAffiliationCommand() *cobra.Command {
Long: "List affiliations visible to caller",
PreRunE: func(cmd *cobra.Command, args []string) error {
log.Level = log.LevelWarning
err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *ClientCmd) newRemoveAffiliationCommand() *cobra.Command {
func (c *ClientCmd) runListAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runListAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (c *ClientCmd) runListAffiliation(cmd *cobra.Command, args []string) error
func (c *ClientCmd) runAddAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runAddAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand All @@ -168,7 +168,7 @@ func (c *ClientCmd) runAddAffiliation(cmd *cobra.Command, args []string) error {
func (c *ClientCmd) runModifyAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runModifyAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand All @@ -193,7 +193,7 @@ func (c *ClientCmd) runModifyAffiliation(cmd *cobra.Command, args []string) erro
func (c *ClientCmd) runRemoveAffiliation(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runRemoveAffiliation: %+v", c.dynamicAffiliation)

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand All @@ -219,7 +219,7 @@ func (c *ClientCmd) affiliationPreRunE(cmd *cobra.Command, args []string) error
return err
}

err = c.configInit()
err = c.ConfigInit()
if err != nil {
return err
}
Expand Down
22 changes: 20 additions & 2 deletions cmd/fabric-ca-client/clientcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ const (
gencsr = "gencsr"
)

// Command interface initializes client command and loads an identity
type Command interface {
ConfigInit() error
LoadMyIdentity() (*lib.Identity, error)
GetClientCfg() *lib.ClientConfig
GetViper() *viper.Viper
}

type crlArgs struct {
// Genenerate CRL with all the certificates that were revoked after this timestamp
RevokedAfter string `help:"Generate CRL with certificates that were revoked after this UTC timestamp (in RFC3339 format)"`
Expand Down Expand Up @@ -247,8 +255,8 @@ func (c *ClientCmd) requiresUser() bool {
return c.name != gencsr
}

// Loads the client's identity
func (c *ClientCmd) loadMyIdentity() (*lib.Identity, error) {
// LoadMyIdentity loads the client's identity
func (c *ClientCmd) LoadMyIdentity() (*lib.Identity, error) {
client := &lib.Client{
HomeDir: filepath.Dir(c.cfgFileName),
Config: c.clientCfg,
Expand All @@ -261,3 +269,13 @@ func (c *ClientCmd) loadMyIdentity() (*lib.Identity, error) {

return id, nil
}

// GetClientCfg returns client configuration
func (c *ClientCmd) GetClientCfg() *lib.ClientConfig {
return c.clientCfg
}

// GetViper returns the viper instance
func (c *ClientCmd) GetViper() *viper.Viper {
return c.myViper
}
3 changes: 2 additions & 1 deletion cmd/fabric-ca-client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ bccsp:
`
)

func (c *ClientCmd) configInit() error {
// ConfigInit initializes the configuration for the fabric-ca-client command
func (c *ClientCmd) ConfigInit() error {
var err error

if c.debug {
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *ClientCmd) newEnrollCommand() *cobra.Command {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}

err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/gencrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *ClientCmd) newGenCRLCommand() *cobra.Command {
if len(args) > 0 {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}
err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/gencsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *ClientCmd) newGenCsrCommand() *cobra.Command {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}

err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/getcacert.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *ClientCmd) newGetCACertCommand() *cobra.Command {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}

err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/fabric-ca-client/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *ClientCmd) newListIdentityCommand() *cobra.Command {
Long: "List identities visible to caller",
PreRunE: func(cmd *cobra.Command, args []string) error {
log.Level = log.LevelWarning
err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (c *ClientCmd) newRemoveIdentityCommand() *cobra.Command {
func (c *ClientCmd) runListIdentity(cmd *cobra.Command, args []string) error {
log.Debug("Entered runListIdentity")

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func (c *ClientCmd) runAddIdentity(cmd *cobra.Command, args []string) error {
return errors.Errorf("Can't use 'json' flag in conjunction with other flags")
}

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (c *ClientCmd) runModifyIdentity(cmd *cobra.Command, args []string) error {

req := &api.ModifyIdentityRequest{}

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *ClientCmd) runModifyIdentity(cmd *cobra.Command, args []string) error {
func (c *ClientCmd) runRemoveIdentity(cmd *cobra.Command, args []string) error {
log.Debugf("Entered runRemoveIdentity: %+v", c.dynamicIdentity)

id, err := c.loadMyIdentity()
id, err := c.LoadMyIdentity()
if err != nil {
return err
}
Expand All @@ -250,7 +250,7 @@ func (c *ClientCmd) identityPreRunE(cmd *cobra.Command, args []string) error {
return err
}

err = c.configInit()
err = c.ConfigInit()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/reenroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *ClientCmd) newReenrollCommand() *cobra.Command {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}

err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *ClientCmd) newRegisterCommand() *cobra.Command {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}

err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/fabric-ca-client/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *ClientCmd) newRevokeCommand() *cobra.Command {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}

err := c.configInit()
err := c.ConfigInit()
if err != nil {
return err
}
Expand Down

0 comments on commit 6e186ea

Please sign in to comment.