github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/lifecycle/chaincode/calculatepackageid.go (about) 1 /* 2 Copyright Hitachi, Ltd. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode 8 9 import ( 10 "encoding/json" 11 "fmt" 12 "io" 13 "os" 14 "strings" 15 16 "github.com/hechain20/hechain/core/chaincode/persistence" 17 "github.com/pkg/errors" 18 "github.com/spf13/cobra" 19 ) 20 21 // PackageIDCalculator holds the dependencies needed to calculate 22 // the package ID for a packaged chaincode 23 type PackageIDCalculator struct { 24 Command *cobra.Command 25 Input *CalculatePackageIDInput 26 Reader Reader 27 Writer io.Writer 28 } 29 30 // CalculatePackageIDInput holds the input parameters for calculating 31 // the package ID of a packaged chaincode 32 type CalculatePackageIDInput struct { 33 PackageFile string 34 OutputFormat string 35 } 36 37 // CalculatePackageIDOutput holds the JSON output format 38 type CalculatePackageIDOutput struct { 39 PackageID string `json:"package_id"` 40 } 41 42 // Validate checks that the required parameters are provided 43 func (i *CalculatePackageIDInput) Validate() error { 44 if i.PackageFile == "" { 45 return errors.New("chaincode install package must be provided") 46 } 47 48 return nil 49 } 50 51 // CalculatePackageIDCmd returns the cobra command for calculating 52 // the package ID for a packaged chaincode 53 func CalculatePackageIDCmd(p *PackageIDCalculator) *cobra.Command { 54 calculatePackageIDCmd := &cobra.Command{ 55 Use: "calculatepackageid packageFile", 56 Short: "Calculate the package ID for a chaincode.", 57 Long: "Calculate the package ID for a packaged chaincode.", 58 ValidArgs: []string{"1"}, 59 RunE: func(cmd *cobra.Command, args []string) error { 60 if p == nil { 61 p = &PackageIDCalculator{ 62 Reader: &persistence.FilesystemIO{}, 63 Writer: os.Stdout, 64 } 65 } 66 p.Command = cmd 67 68 return p.CalculatePackageID(args) 69 }, 70 } 71 flagList := []string{ 72 "peerAddresses", 73 "tlsRootCertFiles", 74 "connectionProfile", 75 "output", 76 } 77 attachFlags(calculatePackageIDCmd, flagList) 78 79 return calculatePackageIDCmd 80 } 81 82 // PackageIDCalculator calculates the package ID for a packaged chaincode. 83 func (p *PackageIDCalculator) CalculatePackageID(args []string) error { 84 if p.Command != nil { 85 // Parsing of the command line is done so silence cmd usage 86 p.Command.SilenceUsage = true 87 } 88 89 if len(args) != 1 { 90 return errors.New("invalid number of args. expected only the packaged chaincode file") 91 } 92 p.setInput(args[0]) 93 94 return p.PackageID() 95 } 96 97 // PackageID calculates the package ID for a packaged chaincode and print it. 98 func (p *PackageIDCalculator) PackageID() error { 99 err := p.Input.Validate() 100 if err != nil { 101 return err 102 } 103 pkgBytes, err := p.Reader.ReadFile(p.Input.PackageFile) 104 if err != nil { 105 return errors.WithMessagef(err, "failed to read chaincode package at '%s'", p.Input.PackageFile) 106 } 107 108 metadata, _, err := persistence.ParseChaincodePackage(pkgBytes) 109 if err != nil { 110 return errors.WithMessage(err, "could not parse as a chaincode install package") 111 } 112 113 packageID := persistence.PackageID(metadata.Label, pkgBytes) 114 115 if strings.ToLower(p.Input.OutputFormat) == "json" { 116 output := CalculatePackageIDOutput{ 117 PackageID: packageID, 118 } 119 outputJson, err := json.MarshalIndent(&output, "", "\t") 120 if err != nil { 121 return errors.WithMessage(err, "failed to marshal output") 122 } 123 fmt.Fprintf(p.Writer, "%s\n", string(outputJson)) 124 return nil 125 } 126 127 fmt.Fprintf(p.Writer, "%s\n", packageID) 128 return nil 129 } 130 131 func (p *PackageIDCalculator) setInput(packageFile string) { 132 p.Input = &CalculatePackageIDInput{ 133 PackageFile: packageFile, 134 OutputFormat: output, 135 } 136 }