Skip to content

Commit

Permalink
Create api-config.json in init command and add --contracts flag to ov…
Browse files Browse the repository at this point in the history
…erwrite the file
  • Loading branch information
Felipe Fuerback committed Mar 12, 2024
1 parent 6d034b5 commit 18352f7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
8 changes: 5 additions & 3 deletions chain-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ npm install -g @gala-chain/cli
$ galachain COMMAND
running command...
$ galachain (--version)
@gala-chain/cli/1.1.0 darwin-arm64 node-v16.20.2
@gala-chain/cli/1.1.0 linux-x64 node-v18.17.0
$ galachain --help [COMMAND]
USAGE
$ galachain COMMAND
Expand Down Expand Up @@ -395,14 +395,15 @@ Start the chaincode in dev-mode and browser-api.
```
USAGE
$ galachain network-up -C <value> -t curator|partner -n <value> [--json] [--log-level debug|info|warn|error]
[-d <value>] [-r <value>] [-e <value>] [-w]
[-d <value>] [-r <value>] [-e <value>] [-w] [-o <value>]
FLAGS
-C, --channel=<value>... (required) Channel name.
-d, --chaincodeDir=<value>... [default: .] Root directory of chaincode source, relative to fabloRoot. By default '.'
is used.
-e, --envConfig=<value> Path to .env file to be used for chaincodes.
-n, --chaincodeName=<value>... (required) Chaincode name.
-o, --contracts=<value> Contract names in a JSON format.
-r, --fabloRoot=<value> [default: ./test-network] Root directory of target network. Should not be the same as
chaincodeDir and should not be a child of chaincodeDir. By default './test-network' is
used.
Expand Down Expand Up @@ -462,14 +463,15 @@ Start the chaincode in dev-mode and browser-api.
```
USAGE
$ galachain network:up -C <value> -t curator|partner -n <value> [--json] [--log-level debug|info|warn|error]
[-d <value>] [-r <value>] [-e <value>] [-w]
[-d <value>] [-r <value>] [-e <value>] [-w] [-o <value>]
FLAGS
-C, --channel=<value>... (required) Channel name.
-d, --chaincodeDir=<value>... [default: .] Root directory of chaincode source, relative to fabloRoot. By default '.'
is used.
-e, --envConfig=<value> Path to .env file to be used for chaincodes.
-n, --chaincodeName=<value>... (required) Chaincode name.
-o, --contracts=<value> Contract names in a JSON format.
-r, --fabloRoot=<value> [default: ./test-network] Root directory of target network. Should not be the same as
chaincodeDir and should not be a child of chaincodeDir. By default './test-network' is
used.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion chain-cli/chaincode-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"format": "prettier --config .prettierrc 'src/**/*.ts' 'e2e/**/*.ts' --write",
"prepublishOnly": "npm run format && npm run build && npm run lint && npm run test",
"network:start": "galachain network:up -C=product-channel -t=curator -n=basic-product -d=./ --envConfig=./.dev-env --watch",
"network:up": "galachain network:up -C=product-channel -t=curator -n=basic-product -d=./ --envConfig=./.dev-env",
"network:up": "galachain network:up -C=product-channel -t=curator -n=basic-product -d=./ --envConfig=./.dev-env --contracts=$(node lib/src/cli.js get-contract-names | tail -n 1)",
"network:prune": "galachain network:prune",
"network:recreate": "npm run network:prune && rm -rf ./test-network && npm run network:start",
"test": "jest",
Expand Down
7 changes: 7 additions & 0 deletions chain-cli/oclif.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,13 @@
"char": "w",
"description": "Enable watch mode (live chaincode reload).",
"allowNo": false
},
"contracts": {
"name": "contracts",
"type": "option",
"char": "o",
"description": "Contract names in a JSON format.",
"multiple": false
}
},
"args": {}
Expand Down
11 changes: 11 additions & 0 deletions chain-cli/src/commands/network-up/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import BaseCommand from "../../base-command";
import { getCPPs, getCPPsBrowserApi } from "../../connection-profile";
import { defaultFabloRoot } from "../../consts";
import { execSync, execSyncStdio } from "../../exec-sync";
import { overwriteApiConfig } from "../../galachain-utils";

const defaultChaincodeDir = ".";

Expand Down Expand Up @@ -87,13 +88,23 @@ export default class NetworkUp extends BaseCommand<typeof NetworkUp> {
watch: Flags.boolean({
char: "w",
description: "Enable watch mode (live chaincode reload)."
}),
contracts: Flags.string({
char: "o",
description: "Contract names in a JSON format."
})
};

async run(): Promise<void> {
const { flags } = await this.parse(NetworkUp);
customValidation(flags);

if (flags.contracts) {
// This feature supports only a single channel
console.log("Overwriting api-config.json with contracts: " + flags.contracts);
overwriteApiConfig(flags.contracts, flags.channel[0], flags.chaincodeName[0]);
}

const fabloRoot = path.resolve(flags.fabloRoot);

const localhostName = process.env.LOCALHOST_NAME ?? "localhost";
Expand Down
32 changes: 32 additions & 0 deletions chain-cli/src/galachain-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import axios from "axios";
import fs, { promises as fsPromises } from "fs";
import { nanoid } from "nanoid";
import { writeFile } from "node:fs/promises";
import path from "path";
import process from "process";

import { ServicePortal } from "./consts";
Expand Down Expand Up @@ -162,6 +163,37 @@ export async function getPrivateKey(keysFromArg: string | undefined) {
);
}

export async function overwriteApiConfig(contracts: string, channel: string, chaincodeName: string) {
const contractsJson = JSON.parse(contracts);

let contractJson = "";
contractsJson.forEach((contract: { contractName: string }) => {
contractJson =
contractJson +
`{
"pathFragment": "${contract.contractName.toLocaleLowerCase()}",
"chaincodeName": "${chaincodeName}",
"contractName": "${contract.contractName}"
},`;
});
// remove the last comma
contractJson = contractJson.slice(0, -1);

// write a new api-config.json file and overwrite the old one
const apiConfigPath = path.resolve(".", "api-config.json");
const apiConfigJson = `{
"channels": [
{
"pathFragment": "product",
"channelName": "${channel}",
"asLocalHost": true,
"contracts": [${contractJson}]
}
]
}`;
fs.writeFileSync(apiConfigPath, JSON.stringify(JSON.parse(apiConfigJson), null, 2));
}

function getPrivateKeyFromFile(): string | undefined {
try {
return fs.readFileSync(
Expand Down

0 comments on commit 18352f7

Please sign in to comment.