forked from matter-labs/zksync-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ca6b76
chore(security): add workflow for leaked secrets monitoring
otani88 eeb45ec
Implement localnet subcommand
jpcenteno 68a67ad
Use import instead of require to get type definitons.
jpcenteno a4c8d71
Cleanup
jpcenteno 1a09eae
Point local-setup repo to our branch. Interface with docker-compose d…
jpcenteno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default function (operationName: string | undefined): Promise<void>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this @jpcenteno ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know
There was a problem hiding this comment.
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.