@@ -38,59 +38,43 @@ type container struct {
38
38
Args []string
39
39
}
40
40
41
- // chaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
42
- // INVOKE form prints the transaction ID on STDOUT, and the QUERY form prints
43
- // the query result on STDOUT. A command-line flag (-r, --raw) determines
44
- // whether the query result is output as raw bytes, or as a printable string.
45
- // The printable form is optionally (-x, --hex) a hexadecimal representation
46
- // of the query response. If the query response is NIL, nothing is output.
47
- func chaincodeInvokeOrQuery (cmd * cobra.Command , args []string , invoke bool ) (err error ) {
48
-
49
- if err = checkChaincodeCmdParams (cmd ); err != nil {
50
- return
41
+ func getChaincodeSpecification (cmd * cobra.Command ) (* pb.ChaincodeSpec , error ) {
42
+ spec := & pb.ChaincodeSpec {}
43
+ if err := checkChaincodeCmdParams (cmd ); err != nil {
44
+ return spec , err
51
45
}
52
46
53
- if chaincodeName == "" {
54
- err = errors .New ("Name not given for invoke/query" )
55
- return
56
- }
57
-
58
- devopsClient , err := common .GetDevopsClient (cmd )
59
- if err != nil {
60
- err = fmt .Errorf ("Error building %s: %s" , chainFuncName , err )
61
- return
62
- }
63
47
// Build the spec
64
- input := & pb.ChaincodeInput {}
65
48
inputc := container {}
66
- if err = json .Unmarshal ([]byte (chaincodeCtorJSON ), & inputc ); err != nil {
67
- err = fmt .Errorf ("Chaincode argument error: %s" , err )
68
- return
49
+ if err := json .Unmarshal ([]byte (chaincodeCtorJSON ), & inputc ); err != nil {
50
+ return spec , fmt .Errorf ("Chaincode argument error: %s" , err )
69
51
}
70
- input = & pb.ChaincodeInput {Args : shim .ToChaincodeArgs (inputc .Args ... )}
52
+ input := & pb.ChaincodeInput {Args : shim .ToChaincodeArgs (inputc .Args ... )}
53
+
71
54
var attributes []string
72
- if err = json .Unmarshal ([]byte (chaincodeAttributesJSON ), & attributes ); err != nil {
73
- err = fmt .Errorf ("Chaincode argument error: %s" , err )
74
- return
55
+ if err := json .Unmarshal ([]byte (chaincodeAttributesJSON ), & attributes ); err != nil {
56
+ return spec , fmt .Errorf ("Chaincode argument error: %s" , err )
75
57
}
76
58
77
59
chaincodeLang = strings .ToUpper (chaincodeLang )
78
- spec := & pb.ChaincodeSpec {Type : pb .ChaincodeSpec_Type (pb .ChaincodeSpec_Type_value [chaincodeLang ]),
79
- ChaincodeID : & pb.ChaincodeID {Name : chaincodeName }, CtorMsg : input , Attributes : attributes }
80
-
60
+ spec = & pb.ChaincodeSpec {
61
+ Type : pb .ChaincodeSpec_Type (pb .ChaincodeSpec_Type_value [chaincodeLang ]),
62
+ ChaincodeID : & pb.ChaincodeID {Path : chaincodePath , Name : chaincodeName },
63
+ CtorMsg : input ,
64
+ Attributes : attributes ,
65
+ }
81
66
// If security is enabled, add client login token
82
67
if core .SecurityEnabled () {
83
68
if chaincodeUsr == common .UndefinedParamValue {
84
- err = errors .New ("Must supply username for chaincode when security is enabled" )
85
- return
69
+ return spec , errors .New ("Must supply username for chaincode when security is enabled" )
86
70
}
87
71
88
72
// Retrieve the CLI data storage path
89
73
// Returns /var/openchain/production/client/
90
74
localStore := util .GetCliFilePath ()
91
75
92
76
// Check if the user is logged in before sending transaction
93
- if _ , err = os .Stat (localStore + "loginToken_" + chaincodeUsr ); err == nil {
77
+ if _ , err : = os .Stat (localStore + "loginToken_" + chaincodeUsr ); err == nil {
94
78
logger .Infof ("Local user '%s' is already logged in. Retrieving login token.\n " , chaincodeUsr )
95
79
96
80
// Read in the login token
@@ -110,8 +94,7 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
110
94
} else {
111
95
// Check if the token is not there and fail
112
96
if os .IsNotExist (err ) {
113
- err = fmt .Errorf ("User '%s' not logged in. Use the 'login' command to obtain a security token." , chaincodeUsr )
114
- return
97
+ return spec , fmt .Errorf ("User '%s' not logged in. Use the 'login' command to obtain a security token." , chaincodeUsr )
115
98
}
116
99
// Unexpected error
117
100
panic (fmt .Errorf ("Fatal error when checking for client login token: %s\n " , err ))
@@ -124,6 +107,25 @@ func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err
124
107
panic (errors .New ("Privacy cannot be enabled as requested because security is disabled" ))
125
108
}
126
109
}
110
+ return spec , nil
111
+ }
112
+
113
+ // chaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
114
+ // INVOKE form prints the transaction ID on STDOUT, and the QUERY form prints
115
+ // the query result on STDOUT. A command-line flag (-r, --raw) determines
116
+ // whether the query result is output as raw bytes, or as a printable string.
117
+ // The printable form is optionally (-x, --hex) a hexadecimal representation
118
+ // of the query response. If the query response is NIL, nothing is output.
119
+ func chaincodeInvokeOrQuery (cmd * cobra.Command , args []string , invoke bool ) (err error ) {
120
+ spec , err := getChaincodeSpecification (cmd )
121
+ if err != nil {
122
+ return err
123
+ }
124
+
125
+ devopsClient , err := common .GetDevopsClient (cmd )
126
+ if err != nil {
127
+ return fmt .Errorf ("Error building %s: %s" , chainFuncName , err )
128
+ }
127
129
128
130
// Build the ChaincodeInvocationSpec message
129
131
invocation := & pb.ChaincodeInvocationSpec {ChaincodeSpec : spec }
0 commit comments