|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package cmd |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/hyperledger-labs/fabric-builder-k8s/internal/builder" |
| 11 | + "github.com/hyperledger-labs/fabric-builder-k8s/internal/log" |
| 12 | + "github.com/hyperledger-labs/fabric-builder-k8s/internal/util" |
| 13 | +) |
| 14 | + |
| 15 | +func Build() int { |
| 16 | + const ( |
| 17 | + expectedArgsLength = 4 |
| 18 | + chaincodeSourceDirectoryArg = 1 |
| 19 | + chaincodeMetadataDirectoryArg = 2 |
| 20 | + buildOutputDirectoryArg = 3 |
| 21 | + ) |
| 22 | + |
| 23 | + debug, _ := strconv.ParseBool(util.GetOptionalEnv(util.DebugVariable, "false")) |
| 24 | + ctx := log.NewCmdContext(context.Background(), debug) |
| 25 | + logger := log.New(ctx) |
| 26 | + |
| 27 | + if len(os.Args) != expectedArgsLength { |
| 28 | + logger.Println( |
| 29 | + "Expected CHAINCODE_SOURCE_DIR, CHAINCODE_METADATA_DIR and BUILD_OUTPUT_DIR arguments", |
| 30 | + ) |
| 31 | + |
| 32 | + return 1 |
| 33 | + } |
| 34 | + |
| 35 | + chaincodeSourceDirectory := os.Args[chaincodeSourceDirectoryArg] |
| 36 | + chaincodeMetadataDirectory := os.Args[chaincodeMetadataDirectoryArg] |
| 37 | + buildOutputDirectory := os.Args[buildOutputDirectoryArg] |
| 38 | + |
| 39 | + logger.Debugf("Chaincode source directory: %s", chaincodeSourceDirectory) |
| 40 | + logger.Debugf("Chaincode metadata directory: %s", chaincodeMetadataDirectory) |
| 41 | + logger.Debugf("Build output directory: %s", buildOutputDirectory) |
| 42 | + |
| 43 | + build := &builder.Build{ |
| 44 | + ChaincodeSourceDirectory: chaincodeSourceDirectory, |
| 45 | + ChaincodeMetadataDirectory: chaincodeMetadataDirectory, |
| 46 | + BuildOutputDirectory: buildOutputDirectory, |
| 47 | + } |
| 48 | + |
| 49 | + if err := build.Run(ctx); err != nil { |
| 50 | + logger.Printf("Error building chaincode: %+v", err) |
| 51 | + |
| 52 | + return 1 |
| 53 | + } |
| 54 | + |
| 55 | + return 0 |
| 56 | +} |
0 commit comments