Skip to content

Commit

Permalink
Add -O json option to calculatepackageid
Browse files Browse the repository at this point in the history
Signed-off-by: Tatsuya Sato <tatsuya.sato.so@hitachi.com>
  • Loading branch information
satota2 authored and denyeart committed Nov 2, 2021
1 parent ee18f9d commit b1c9d43
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
26 changes: 24 additions & 2 deletions internal/peer/lifecycle/chaincode/calculatepackageid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -64,6 +72,7 @@ func CalculatePackageIDCmd(p *PackageIDCalculator) *cobra.Command {
"peerAddresses",
"tlsRootCertFiles",
"connectionProfile",
"output",
}
attachFlags(calculatePackageIDCmd, flagList)

Expand Down Expand Up @@ -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,
}
}
19 changes: 19 additions & 0 deletions internal/peer/lifecycle/chaincode/calculatepackageid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit b1c9d43

Please sign in to comment.