|
| 1 | +import {Command, Flags} from '@oclif/core' |
| 2 | +import chalk from 'chalk' |
| 3 | +import { select } from '@inquirer/prompts' |
| 4 | +import Docker from 'dockerode' |
| 5 | + |
| 6 | +export default class ContractsVerification extends Command { |
| 7 | + static override description = 'Set up contracts verification' |
| 8 | + |
| 9 | + static override examples = [ |
| 10 | + '<%= config.bin %> <%= command.id %>', |
| 11 | + '<%= config.bin %> <%= command.id %> --image-tag verify-8ff2948aa2b9cbd24a7644b060097765a6faee10', |
| 12 | + ] |
| 13 | + |
| 14 | + static override flags = { |
| 15 | + 'image-tag': Flags.string({ |
| 16 | + description: 'Specify the Docker image tag to use', |
| 17 | + required: false, |
| 18 | + }), |
| 19 | + } |
| 20 | + |
| 21 | + private async fetchDockerTags(): Promise<string[]> { |
| 22 | + try { |
| 23 | + const response = await fetch( |
| 24 | + 'https://registry.hub.docker.com/v2/repositories/scrolltech/scroll-stack-contracts/tags?page_size=100', |
| 25 | + ) |
| 26 | + if (!response.ok) { |
| 27 | + throw new Error(`HTTP error! status: ${response.status}`) |
| 28 | + } |
| 29 | + const data = await response.json() |
| 30 | + return data.results.map((tag: any) => tag.name).filter((tag: string) => tag.startsWith('verify')) |
| 31 | + } catch (error) { |
| 32 | + this.error(`Failed to fetch Docker tags: ${error}`) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private async getDockerImageTag(providedTag: string | undefined): Promise<string> { |
| 37 | + const defaultTag = 'verify-8ff2948aa2b9cbd24a7644b060097765a6faee10' |
| 38 | + |
| 39 | + if (!providedTag) { |
| 40 | + return defaultTag |
| 41 | + } |
| 42 | + |
| 43 | + const tags = await this.fetchDockerTags() |
| 44 | + |
| 45 | + if (providedTag.startsWith('gen-configs-v') && tags.includes(providedTag)) { |
| 46 | + return providedTag |
| 47 | + } else if (providedTag.startsWith('v') && tags.includes(`verify-${providedTag}`)) { |
| 48 | + return `verify-${providedTag}` |
| 49 | + } else if (/^\d+\.\d+\.\d+$/.test(providedTag) && tags.includes(`verify-v${providedTag}`)) { |
| 50 | + return `verify-v${providedTag}` |
| 51 | + } |
| 52 | + |
| 53 | + const selectedTag = await select({ |
| 54 | + message: 'Select a Docker image tag:', |
| 55 | + choices: tags.map((tag) => ({name: tag, value: tag})), |
| 56 | + }) |
| 57 | + |
| 58 | + return selectedTag |
| 59 | + } |
| 60 | + |
| 61 | + private async runDockerCommand(imageTag: string): Promise<void> { |
| 62 | + const docker = new Docker() |
| 63 | + const image = `scrolltech/scroll-stack-contracts:${imageTag}` |
| 64 | + |
| 65 | + try { |
| 66 | + this.log(chalk.cyan('Pulling Docker Image...')) |
| 67 | + // Pull the image if it doesn't exist locally |
| 68 | + const pullStream = await docker.pull(image) |
| 69 | + await new Promise((resolve, reject) => { |
| 70 | + docker.modem.followProgress(pullStream, (err, res) => { |
| 71 | + if (err) { |
| 72 | + reject(err) |
| 73 | + } else { |
| 74 | + this.log(chalk.green('Image pulled successfully')) |
| 75 | + resolve(res) |
| 76 | + } |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + this.log(chalk.cyan('Creating Docker Container...')) |
| 81 | + // Create and run the container |
| 82 | + const container = await docker.createContainer({ |
| 83 | + Image: image, |
| 84 | + Cmd: [], // Add any command if needed |
| 85 | + HostConfig: { |
| 86 | + Binds: [`${process.cwd()}:/contracts/volume`], |
| 87 | + }, |
| 88 | + }) |
| 89 | + |
| 90 | + this.log(chalk.cyan('Starting Container')) |
| 91 | + await container.start() |
| 92 | + |
| 93 | + // Wait for the container to finish and get the logs |
| 94 | + const stream = await container.logs({ |
| 95 | + follow: true, |
| 96 | + stdout: true, |
| 97 | + stderr: true, |
| 98 | + }) |
| 99 | + |
| 100 | + // Print the logs |
| 101 | + stream.pipe(process.stdout) |
| 102 | + |
| 103 | + // Wait for the container to finish |
| 104 | + await new Promise((resolve) => { |
| 105 | + container.wait((err, data) => { |
| 106 | + if (err) { |
| 107 | + this.error(`Container exited with error: ${err}`) |
| 108 | + } else if (data.StatusCode !== 0) { |
| 109 | + this.error(`Container exited with status code: ${data.StatusCode}`) |
| 110 | + } |
| 111 | + resolve(null) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + // Remove the container |
| 116 | + await container.remove() |
| 117 | + } catch (error) { |
| 118 | + this.error(`Failed to run Docker command: ${error}`) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public async run(): Promise<void> { |
| 123 | + this.log(chalk.blue('Running docker command to contracts verification...')) |
| 124 | + |
| 125 | + const {flags} = await this.parse(ContractsVerification) |
| 126 | + |
| 127 | + const imageTag = await this.getDockerImageTag(flags['image-tag']) |
| 128 | + this.log(chalk.blue(`Using Docker image tag: ${imageTag}`)) |
| 129 | + |
| 130 | + await this.runDockerCommand(imageTag) |
| 131 | + } |
| 132 | +} |
0 commit comments