Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: localnet subcommand #2

Merged
merged 5 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/secrets_scanner.yaml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this @jpcenteno ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why I appear as a co-author of that commit.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Leaked Secrets Scan
on: [pull_request]
jobs:
TruffleHog:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3
with:
fetch-depth: 0
- name: TruffleHog OSS
uses: trufflesecurity/trufflehog@0c66d30c1f4075cee1aada2e1ab46dabb1b0071a
with:
path: ./
base: ${{ github.event.repository.default_branch }}
head: HEAD
extra_args: --debug --only-verified
7 changes: 6 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const figlet_1 = __importDefault(require("figlet"));
const create_1 = __importDefault(require("./create"));
const deposit_1 = __importDefault(require("./deposit"));
const withdraw_1 = __importDefault(require("./withdraw"));
const availableOptions = ['create', 'deposit', 'withdraw'];
const localnet_1 = __importDefault(require("./localnet"));
const availableOptions = ['create', 'deposit', 'withdraw', 'localnet'];
// second argument should be the selected option
const option = process.argv[2];
if (!availableOptions.includes(option)) {
Expand All @@ -33,4 +34,8 @@ switch (option) {
case 'withdraw':
(0, withdraw_1.default)();
break;
case 'localnet':
const subcommandName = process.argv[3] || undefined;
(0, localnet_1.default)(subcommandName);
break;
}
1 change: 1 addition & 0 deletions bin/localnet.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function (operationName: string | undefined): Promise<void>;
113 changes: 113 additions & 0 deletions bin/localnet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
/**
* Runs CLI commands
* @param {*} command String command to run
*/
const runCommand = (command) => {
try {
// runs given command and prints its output to console
execSync(`${command}`, { stdio: 'inherit' });
}
catch (error) {
console.error('Failed to run command: ', error);
return false;
}
return true;
};
function localSetupRepoPath() {
const defaultXDGStateHome = path.join(process.env.HOME, ".local", "state");
const xdgStateHome = process.env["XDG_STATE_HOME"] || defaultXDGStateHome;
return path.join(xdgStateHome, "zksync-cli", "local-setup");
}
function localSetupRepoExists() {
return fs.existsSync(localSetupRepoPath());
}
function cloneLocalSetupRepo() {
const repoParentDir = path.join(localSetupRepoPath(), "..");
runCommand(`mkdir -p "${repoParentDir}"`);
runCommand(`cd "${repoParentDir}" && git clone https://github.com/matter-labs/local-setup.git`);
}
function createLocalSetupStartInBackgroundScript() {
runCommand(`cd "${localSetupRepoPath()}" && sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh && chmod +x start-background.sh`);
}
function handleStartOperation() {
const repoPath = localSetupRepoPath();
if (!localSetupRepoExists()) {
cloneLocalSetupRepo();
}
createLocalSetupStartInBackgroundScript();
runCommand(`cd "${localSetupRepoPath()}" && ./start-background.sh`);
return 0;
}
function handleDownOperation() {
runCommand(`cd "${localSetupRepoPath()}" && docker-compose down`);
return 0;
}
function handleLogsOperation() {
runCommand(`cd "${localSetupRepoPath()}" && docker-compose logs --follow`);
return 0;
}
function handleClearOperation() {
runCommand(`cd "${localSetupRepoPath()}" && ./clear.sh`);
return 0;
}
function handleHelpOperation() {
console.log("USAGE: zksync-cli localnet <operation>");
console.log("");
console.log("Manage local L1 and L2 chains");
console.log("");
console.log("Available operations");
console.log(' start -- Start L1 and L2 localnets');
console.log(' down -- Stop L1 and L2 localnets');
console.log(' clear -- Reset the localnet state');
console.log(' logs -- Display logs');
console.log(' help -- Display this message and quit');
console.log(' wallets -- Display seeded wallet keys');
return 0;
}
function handleUndefinedOperation() {
console.error("No operation provided");
handleHelpOperation();
return 1;
}
function handleWalletsOperation() {
const rawJSON = fs.readFileSync(path.join(localSetupRepoPath(), "rich-wallets.json"));
const wallets = JSON.parse(rawJSON);
console.log(wallets);
return 0;
}
function handleInvalidOperation(operationName) {
const validOperationNames = Array.from(operationHandlers.keys());
console.error('Invalid operation: ', operationName);
handleHelpOperation();
return 1;
}
const operationHandlers = new Map([
['start', handleStartOperation],
['down', handleDownOperation],
['logs', handleLogsOperation],
['help', handleHelpOperation],
['wallets', handleWalletsOperation],
['clear', handleClearOperation],
[undefined, handleUndefinedOperation],
]);
function default_1(operationName) {
return __awaiter(this, void 0, void 0, function* () {
const handler = operationHandlers.get(operationName) || (() => handleInvalidOperation(operationName));
process.exit(handler());
});
}
exports.default = default_1;
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import figlet from 'figlet';
import create from './create';
import deposit from './deposit';
import withdraw from './withdraw';
import localnet from './localnet';

const availableOptions: string[] = ['create', 'deposit', 'withdraw'];
const availableOptions: string[] = ['create', 'deposit', 'withdraw', 'localnet'];

// second argument should be the selected option
const option: string = process.argv[2];
Expand Down Expand Up @@ -43,4 +44,8 @@ switch (option) {
case 'withdraw':
withdraw();
break;
case 'localnet':
const subcommandName = process.argv[3] || undefined;
localnet(subcommandName);
break;
}
117 changes: 117 additions & 0 deletions src/localnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');

/**
* Runs CLI commands
* @param {*} command String command to run
*/
const runCommand = (command: string) => {
try {
// runs given command and prints its output to console
execSync(`${command}`, { stdio: 'inherit' });
} catch (error) {
console.error('Failed to run command: ', error);
return false;
}
return true;
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that we have the same function in src/create.ts. Would it be a good idea to abstract it?


function localSetupRepoPath(): string {
const defaultXDGStateHome = path.join(process.env.HOME!, ".local", "state");
const xdgStateHome = process.env["XDG_STATE_HOME"] || defaultXDGStateHome;
return path.join(xdgStateHome, "zksync-cli", "local-setup");
}

function localSetupRepoExists(): boolean {
return fs.existsSync(localSetupRepoPath());
}

function cloneLocalSetupRepo() {
const repoParentDir = path.join(localSetupRepoPath(), "..");
runCommand(`mkdir -p "${repoParentDir}"`);
runCommand(`cd "${repoParentDir}" && git clone https://github.com/matter-labs/local-setup.git`);
}

function createLocalSetupStartInBackgroundScript() {
runCommand(`cd "${localSetupRepoPath()}" && sed 's/^docker-compose up$/docker-compose up --detach/' start.sh > start-background.sh && chmod +x start-background.sh`);
}

function handleStartOperation(): number {
const repoPath = localSetupRepoPath();

if (!localSetupRepoExists()) {
cloneLocalSetupRepo();
}

createLocalSetupStartInBackgroundScript();

runCommand(`cd "${localSetupRepoPath()}" && ./start-background.sh`);

return 0;
}

function handleDownOperation(): number {
runCommand(`cd "${localSetupRepoPath()}" && docker-compose down`);
return 0;
}

function handleLogsOperation(): number {
runCommand(`cd "${localSetupRepoPath()}" && docker-compose logs --follow`);
return 0;
}

function handleClearOperation(): number {
runCommand(`cd "${localSetupRepoPath()}" && ./clear.sh`);
return 0;
}

function handleHelpOperation(): number {
console.log("USAGE: zksync-cli localnet <operation>");
console.log("");
console.log("Manage local L1 and L2 chains");
console.log("");
console.log("Available operations");
console.log(' start -- Start L1 and L2 localnets');
console.log(' down -- Stop L1 and L2 localnets');
console.log(' clear -- Reset the localnet state');
console.log(' logs -- Display logs');
console.log(' help -- Display this message and quit');
console.log(' wallets -- Display seeded wallet keys');
return 0;
}

function handleUndefinedOperation(): number {
console.error("No operation provided");
handleHelpOperation();
return 1;
}

function handleWalletsOperation(): number {
const rawJSON = fs.readFileSync(path.join(localSetupRepoPath(), "rich-wallets.json"));
const wallets = JSON.parse(rawJSON);
console.log(wallets);
return 0;
}

function handleInvalidOperation(operationName: string): number {
const validOperationNames = Array.from(operationHandlers.keys());
console.error('Invalid operation: ', operationName);
handleHelpOperation();
return 1;
}

const operationHandlers = new Map<string | undefined, () => number>([
['start', handleStartOperation],
['down', handleDownOperation],
['logs', handleLogsOperation],
['help', handleHelpOperation],
['wallets', handleWalletsOperation],
['clear', handleClearOperation],
[undefined, handleUndefinedOperation],
]);

export default async function (operationName: string | undefined) {
const handler = operationHandlers.get(operationName) || (() => handleInvalidOperation(operationName!));
process.exit(handler());
}