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(kadena-cli): add devnet commands to kadena-cli #1242

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/tough-rabbits-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kadena/kadena-cli': patch
---

add devnet commands to kadena-cli
3 changes: 3 additions & 0 deletions packages/tools/kadena-cli/src/config/commands/configInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const createConfigInitCommand: (
await import('../../networks/init.js');
console.log(chalk.green('Configured default networks.'));

await import('../../devnet/init.js');
console.log(chalk.green('Configured default devnets.'));

console.log(chalk.green('Configuration complete!'));
},
);
23 changes: 23 additions & 0 deletions packages/tools/kadena-cli/src/constants/devnets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { IDevnetsCreateOptions } from '../devnet/utils/devnetHelpers.js';

export interface IDefaultDevnetOptions {
[key: string]: IDevnetsCreateOptions;
}

/**
* @const devnetDefaults
* Provides the default devnet configurations.
*/
export const devnetDefaults: IDefaultDevnetOptions = {
devnet: {
name: 'devnet',
port: 8080,
useVolume: false,
mountPactFolder: '',
version: 'latest',
},
};

export const defaultDevnetsPath: string = `${process.cwd()}/.kadena/devnets`;
export const standardDevnets: string[] = ['devnet'];
export const defaultDevnet: string = 'devnet';
52 changes: 52 additions & 0 deletions packages/tools/kadena-cli/src/devnet/commands/devnetCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { defaultDevnetsPath } from '../../constants/devnets.js';
import { ensureFileExists } from '../../utils/filesystem.js';
import { writeDevnet } from '../utils/devnetHelpers.js';

import debug from 'debug';
import path from 'path';

import chalk from 'chalk';
import { devnetOverwritePrompt } from '../../prompts/devnet.js';
import { createExternalPrompt } from '../../prompts/generic.js';
import { createCommand } from '../../utils/createCommand.js';
import { globalOptions } from '../../utils/globalOptions.js';

export const createDevnetCommand = createCommand(
'create',
'Create devnet',
[
globalOptions.devnetName(),
globalOptions.devnetPort(),
globalOptions.devnetUseVolume(),
globalOptions.devnetMountPactFolder(),
globalOptions.devnetVersion(),
],
async (config) => {
debug('devnet-create:action')({ config });

const filePath = path.join(defaultDevnetsPath, `${config.name}.yaml`);

if (ensureFileExists(filePath)) {
const externalPrompt = createExternalPrompt({
devnetOverwritePrompt,
});
const overwrite = await externalPrompt.devnetOverwritePrompt();
if (overwrite === 'no') {
console.log(
chalk.yellow(
`\nThe existing devnet configuration "${config.name}" will not be updated.\n`,
),
);
return;
}
}

writeDevnet(config);

console.log(
chalk.green(
`\nThe devnet configuration "${config.name}" has been saved.\n`,
),
);
},
);
73 changes: 73 additions & 0 deletions packages/tools/kadena-cli/src/devnet/commands/devnetDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import debug from 'debug';
import { devnetDeletePrompt } from '../../prompts/devnet.js';
import { globalOptions } from '../../utils/globalOptions.js';
import {
getDevnetConfiguration,
removeDevnetConfiguration,
} from '../utils/devnetHelpers.js';

import chalk from 'chalk';
import { createExternalPrompt } from '../../prompts/generic.js';
import { createCommand } from '../../utils/createCommand.js';
import {
dockerVolumeName,
isDockerInstalled,
removeDevnet,
removeVolume,
} from '../utils/docker.js';

export const deleteDevnetCommand = createCommand(
'delete',
'Delete devnet',
[globalOptions.devnetSelect()],
async (config) => {
debug('devnet-delete:action')({ config });

const externalPrompt = createExternalPrompt({
devnetDeletePrompt,
});
const deleteDevnet = await externalPrompt.devnetDeletePrompt();

if (deleteDevnet === 'no') {
console.log(
chalk.yellow(
`\nThe devnet configuration "${config.name}" will not be deleted.\n`,
),
);
return;
}

if (!isDockerInstalled()) {
console.log(
chalk.red(
'Stopping devnet requires Docker. Please install Docker and try again.',
),
);
return;
}

removeDevnet(config.name);
console.log(chalk.green(`Removed devnet container: ${config.name}`));

const configuration = getDevnetConfiguration(config.name);

if (configuration?.useVolume) {
removeVolume(config.name);
console.log(
chalk.green(`Removed volume: ${dockerVolumeName(config.name)}`),
);
}

console.log(
chalk.green(
`Successfully removed devnet container for configuration: ${config.name}`,
),
);

removeDevnetConfiguration(config);

console.log(
chalk.green(`Successfully removed devnet configuration: ${config.name}`),
);
},
);
14 changes: 14 additions & 0 deletions packages/tools/kadena-cli/src/devnet/commands/devnetList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import debug from 'debug';
import { createCommand } from '../../utils/createCommand.js';
import { displayDevnetsConfig } from '../utils/devnetDisplay.js';

export const listDevnetsCommand = createCommand(
'list',
'List all available devnets',
[],
async (config) => {
debug('devnet-list:action')({ config });

await displayDevnetsConfig();
},
);
45 changes: 45 additions & 0 deletions packages/tools/kadena-cli/src/devnet/commands/devnetManage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import debug from 'debug';
import { devnetOverwritePrompt } from '../../prompts/devnet.js';
import { globalOptions } from '../../utils/globalOptions.js';
import { writeDevnet } from '../utils/devnetHelpers.js';

import chalk from 'chalk';
import { createExternalPrompt } from '../../prompts/generic.js';
import { createCommand } from '../../utils/createCommand.js';

export const manageDevnetsCommand = createCommand(
'manage',
'Manage devnets',
[
globalOptions.devnetSelect(),
globalOptions.devnetPort(),
globalOptions.devnetUseVolume(),
globalOptions.devnetMountPactFolder(),
globalOptions.devnetVersion(),
],
async (config) => {
debug('devnet-manage:action')({ config });

const externalPrompt = createExternalPrompt({
devnetOverwritePrompt,
});
const overwrite = await externalPrompt.devnetOverwritePrompt();

if (overwrite === 'no') {
console.log(
chalk.yellow(
`\nThe devnet configuration "${config.name}" will not be updated.\n`,
),
);
return;
}

writeDevnet(config);

console.log(
chalk.green(
`\nThe devnet configuration "${config.name}" has been updated.\n`,
),
);
},
);
32 changes: 32 additions & 0 deletions packages/tools/kadena-cli/src/devnet/commands/devnetRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import debug from 'debug';
import { globalOptions } from '../../utils/globalOptions.js';

import chalk from 'chalk';
import { createCommand } from '../../utils/createCommand.js';
import { isDockerInstalled, runDevnet } from '../utils/docker.js';

export const runDevnetCommand = createCommand(
'run',
'Run devnet',
[globalOptions.devnet()],
async (config) => {
debug('devnet-run:action')({ config });

if (!isDockerInstalled()) {
console.log(
chalk.red(
'Running devnet requires Docker. Please install Docker and try again.',
),
);
return;
}

runDevnet(config.devnetConfig);

console.log(
chalk.green(
`\nThe devnet configuration "${config.devnet}" is running.\n`,
),
);
},
);
32 changes: 32 additions & 0 deletions packages/tools/kadena-cli/src/devnet/commands/devnetStop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import debug from 'debug';
import { globalOptions } from '../../utils/globalOptions.js';

import chalk from 'chalk';
import { createCommand } from '../../utils/createCommand.js';
import { isDockerInstalled, stopDevnet } from '../utils/docker.js';

export const stopDevnetCommand = createCommand(
'stop',
'Stop devnet',
[globalOptions.devnetSelect()],
async (config) => {
debug('devnet-stop:action')({ config });

if (!isDockerInstalled()) {
console.log(
chalk.red(
'Stopping devnet requires Docker. Please install Docker and try again.',
),
);
return;
}

stopDevnet(config.name);

console.log(
chalk.green(
`\nThe devnet configuration "${config.name}" has been stopped.\n`,
),
);
},
);
26 changes: 26 additions & 0 deletions packages/tools/kadena-cli/src/devnet/commands/devnetUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import chalk from 'chalk';
import debug from 'debug';
import { createCommand } from '../../utils/createCommand.js';
import { globalOptions } from '../../utils/globalOptions.js';
import { isDockerInstalled, updateDevnet } from '../utils/docker.js';

export const updateDevnetCommand = createCommand(
'update',
'Update the Docker image of a given devnet container image',
[globalOptions.devnetVersion()],
async (config) => {
debug('devnet-update:action')({ config });

// Abort if Docker is not installed
if (!isDockerInstalled()) {
console.log(
chalk.red(
'Updating devnet requires Docker. Please install Docker and try again.',
),
);
return;
}

updateDevnet(config.version || 'latest');
},
);
9 changes: 0 additions & 9 deletions packages/tools/kadena-cli/src/devnet/commands/start.ts

This file was deleted.

20 changes: 16 additions & 4 deletions packages/tools/kadena-cli/src/devnet/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { devnetStartCommand } from './commands/start.js';
import { createDevnetCommand } from './commands/devnetCreate.js';
import { deleteDevnetCommand } from './commands/devnetDelete.js';
import { listDevnetsCommand } from './commands/devnetList.js';
import { manageDevnetsCommand } from './commands/devnetManage.js';
import { runDevnetCommand } from './commands/devnetRun.js';
import { stopDevnetCommand } from './commands/devnetStop.js';
import { updateDevnetCommand } from './commands/devnetUpdate.js';

import type { Command } from 'commander';

const SUBCOMMAND_ROOT: 'devnet' = 'devnet';

export function devnetCommandFactory(program: Command, version: string): void {
const devnetProgram = program
const devnetsProgram = program
.command(SUBCOMMAND_ROOT)
.description(`Tool for starting, stopping and managing the local devnet`);
.description(`Tool to create and manage devnets`);

devnetStartCommand(devnetProgram, version);
listDevnetsCommand(devnetsProgram, version);
manageDevnetsCommand(devnetsProgram, version);
createDevnetCommand(devnetsProgram, version);
deleteDevnetCommand(devnetsProgram, version);
runDevnetCommand(devnetsProgram, version);
stopDevnetCommand(devnetsProgram, version);
updateDevnetCommand(devnetsProgram, version);
}
4 changes: 4 additions & 0 deletions packages/tools/kadena-cli/src/devnet/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { devnetDefaults } from '../constants/devnets.js';
import { writeDevnet } from './utils/devnetHelpers.js';

writeDevnet(devnetDefaults.devnet);
Loading
Loading