Skip to content

Commit 1a7c2ab

Browse files
authored
Enhance chaincode query commands with output format option (#272)
- Added an `output` flag to the `queryapproved`, `querycommitted`, and `queryinstalled` commands, allowing users to specify the output format as either "table" or "json". - Implemented JSON output formatting for the queried chaincode data, improving the usability and flexibility of the commands. These changes enhance the user experience by providing more options for displaying query results.
1 parent 068d414 commit 1a7c2ab

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

kubectl-hlf/cmd/chaincode/queryapproved.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package chaincode
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
7+
68
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
79
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
810
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
911
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
1012
"github.com/olekukonko/tablewriter"
1113
"github.com/spf13/cobra"
12-
"io"
1314
)
1415

1516
type queryApprovedCmd struct {
@@ -18,6 +19,7 @@ type queryApprovedCmd struct {
1819
userName string
1920
channelName string
2021
chaincodeName string
22+
output string
2123
}
2224

2325
func (c *queryApprovedCmd) validate() error {
@@ -59,6 +61,17 @@ func (c *queryApprovedCmd) run(out io.Writer) error {
5961
},
6062
resmgmt.WithTargetEndpoints(peerName),
6163
)
64+
if err != nil {
65+
return err
66+
}
67+
if c.output == "json" {
68+
b, err := json.MarshalIndent(chaincode, "", " ")
69+
if err != nil {
70+
return err
71+
}
72+
_, err = out.Write(b)
73+
return err
74+
}
6275
signaturePolicyBytes, err := json.Marshal(chaincode.SignaturePolicy)
6376
if err != nil {
6477
return err
@@ -99,6 +112,7 @@ func newQueryApprovedCMD(out io.Writer, errOut io.Writer) *cobra.Command {
99112
persistentFlags.StringVarP(&c.configPath, "config", "", "", "Configuration file for the SDK")
100113
persistentFlags.StringVarP(&c.channelName, "channel", "C", "", "Channel name")
101114
persistentFlags.StringVarP(&c.chaincodeName, "chaincode", "c", "", "Chaincode label")
115+
persistentFlags.StringVarP(&c.output, "output", "o", "table", "Output format, can be table or json")
102116
cmd.MarkPersistentFlagRequired("user")
103117
cmd.MarkPersistentFlagRequired("peer")
104118
cmd.MarkPersistentFlagRequired("config")

kubectl-hlf/cmd/chaincode/querycommitted.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package chaincode
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
7+
68
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
79
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
810
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
911
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
1012
"github.com/olekukonko/tablewriter"
1113
log "github.com/sirupsen/logrus"
1214
"github.com/spf13/cobra"
13-
"io"
1415
)
1516

1617
type queryCommittedCmd struct {
@@ -19,6 +20,7 @@ type queryCommittedCmd struct {
1920
userName string
2021
channelName string
2122
chaincodeName string
23+
output string
2224
}
2325

2426
func (c *queryCommittedCmd) validate() error {
@@ -67,6 +69,14 @@ func (c *queryCommittedCmd) run(out io.Writer) error {
6769
log.Infof("No chaincode found")
6870
return nil
6971
}
72+
if c.output == "json" {
73+
b, err := json.MarshalIndent(chaincodes, "", " ")
74+
if err != nil {
75+
return err
76+
}
77+
_, err = out.Write(b)
78+
return err
79+
}
7080
var data [][]string
7181
for _, chaincode := range chaincodes {
7282
approvalJson, err := json.Marshal(chaincode.Approvals)
@@ -121,6 +131,7 @@ func newQueryCommittedCMD(out io.Writer, errOut io.Writer) *cobra.Command {
121131
persistentFlags.StringVarP(&c.configPath, "config", "", "", "Configuration file for the SDK")
122132
persistentFlags.StringVarP(&c.channelName, "channel", "C", "", "Channel name")
123133
persistentFlags.StringVarP(&c.chaincodeName, "chaincode", "c", "", "Chaincode label")
134+
persistentFlags.StringVarP(&c.output, "output", "o", "table", "Output format, can be table or json")
124135
cmd.MarkPersistentFlagRequired("user")
125136
cmd.MarkPersistentFlagRequired("peer")
126137
cmd.MarkPersistentFlagRequired("config")

kubectl-hlf/cmd/chaincode/queryinstalled.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ package chaincode
22

33
import (
44
"encoding/json"
5+
"io"
6+
57
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
68
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
79
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
810
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
911
"github.com/olekukonko/tablewriter"
1012
log "github.com/sirupsen/logrus"
1113
"github.com/spf13/cobra"
12-
"io"
1314
)
1415

1516
type queryInstalledCmd struct {
1617
configPath string
1718
peer string
1819
userName string
1920
mspID string
21+
output string
2022
}
2123

2224
func (c *queryInstalledCmd) validate() error {
@@ -66,6 +68,14 @@ func (c *queryInstalledCmd) run(out io.Writer) error {
6668
log.Infof("No chaincodes installed")
6769
return nil
6870
}
71+
if c.output == "json" {
72+
b, err := json.MarshalIndent(chaincodes, "", " ")
73+
if err != nil {
74+
return err
75+
}
76+
_, err = out.Write(b)
77+
return err
78+
}
6979
var data [][]string
7080
for _, chaincode := range chaincodes {
7181
referencesJson, err := json.Marshal(chaincode.References)
@@ -109,6 +119,7 @@ func newChaincodeQueryInstalledCMD(out io.Writer, errOut io.Writer) *cobra.Comma
109119
persistentFlags.StringVarP(&c.userName, "user", "", "", "User name for the transaction")
110120
persistentFlags.StringVarP(&c.configPath, "config", "", "", "Configuration file for the SDK")
111121
persistentFlags.StringVarP(&c.mspID, "mspID", "", "", "MSP ID of the peer")
122+
persistentFlags.StringVarP(&c.output, "output", "o", "table", "Output format, can be table or json")
112123
cmd.MarkPersistentFlagRequired("user")
113124
cmd.MarkPersistentFlagRequired("peer")
114125
cmd.MarkPersistentFlagRequired("config")

0 commit comments

Comments
 (0)