From b1c9d43911608d2d3c716f5615bf227571746188 Mon Sep 17 00:00:00 2001 From: Tatsuya Sato Date: Fri, 29 Oct 2021 08:21:49 +0000 Subject: [PATCH] Add `-O json` option to `calculatepackageid` Signed-off-by: Tatsuya Sato --- .../lifecycle/chaincode/calculatepackageid.go | 26 +++++++++++++++++-- .../chaincode/calculatepackageid_test.go | 19 ++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/internal/peer/lifecycle/chaincode/calculatepackageid.go b/internal/peer/lifecycle/chaincode/calculatepackageid.go index 75b19c4835a..0bb2af0e93d 100644 --- a/internal/peer/lifecycle/chaincode/calculatepackageid.go +++ b/internal/peer/lifecycle/chaincode/calculatepackageid.go @@ -7,9 +7,11 @@ SPDX-License-Identifier: Apache-2.0 package chaincode import ( + "encoding/json" "fmt" "io" "os" + "strings" "github.com/hyperledger/fabric/core/chaincode/persistence" "github.com/pkg/errors" @@ -28,7 +30,13 @@ type PackageIDCalculator struct { // CalculatePackageIDInput holds the input parameters for calculating // the package ID of a packaged chaincode type CalculatePackageIDInput struct { - PackageFile string + PackageFile string + OutputFormat string +} + +// CalculatePackageIDOutput holds the JSON output format +type CalculatePackageIDOutput struct { + PackageID string `json:"package_id"` } // Validate checks that the required parameters are provided @@ -64,6 +72,7 @@ func CalculatePackageIDCmd(p *PackageIDCalculator) *cobra.Command { "peerAddresses", "tlsRootCertFiles", "connectionProfile", + "output", } attachFlags(calculatePackageIDCmd, flagList) @@ -103,12 +112,25 @@ func (p *PackageIDCalculator) PackageID() error { packageID := persistence.PackageID(metadata.Label, pkgBytes) + if strings.ToLower(p.Input.OutputFormat) == "json" { + output := CalculatePackageIDOutput{ + PackageID: packageID, + } + outputJson, err := json.MarshalIndent(&output, "", "\t") + if err != nil { + return errors.WithMessage(err, "failed to marshal output") + } + fmt.Fprintf(p.Writer, "%s\n", string(outputJson)) + return nil + } + fmt.Fprintf(p.Writer, "%s\n", packageID) return nil } func (p *PackageIDCalculator) setInput(packageFile string) { p.Input = &CalculatePackageIDInput{ - PackageFile: packageFile, + PackageFile: packageFile, + OutputFormat: output, } } diff --git a/internal/peer/lifecycle/chaincode/calculatepackageid_test.go b/internal/peer/lifecycle/chaincode/calculatepackageid_test.go index ae33f5799bb..a08df7ef832 100644 --- a/internal/peer/lifecycle/chaincode/calculatepackageid_test.go +++ b/internal/peer/lifecycle/chaincode/calculatepackageid_test.go @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0 package chaincode_test import ( + "encoding/json" + "fmt" "io/ioutil" "github.com/hyperledger/fabric/internal/peer/lifecycle/chaincode" @@ -86,6 +88,23 @@ var _ = Describe("CalculatePackageID", func() { Expect(err).To(MatchError(ContainSubstring("could not parse as a chaincode install package"))) }) }) + + Context("when JSON-formatted output is requested", func() { + BeforeEach(func() { + packageIDCalculator.Input.OutputFormat = "json" + }) + + It("calculates the package IDs for chaincodes and writes the output as JSON", func() { + err := packageIDCalculator.PackageID() + Expect(err).NotTo(HaveOccurred()) + expectedOutput := &chaincode.CalculatePackageIDOutput{ + PackageID: "Real-Label:fb3edf9621c5e3d864079d8c9764205f4db09d7021cfa4124aa79f4edcc2f64a", + } + json, err := json.MarshalIndent(expectedOutput, "", "\t") + Expect(err).NotTo(HaveOccurred()) + Eventually(packageIDCalculator.Writer).Should(gbytes.Say(fmt.Sprintf(`\Q%s\E`, string(json)))) + }) + }) }) Describe("CalculatePackageIDCmd", func() {