-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kadena-cli): add devnet commands to kadena-cli
- Loading branch information
1 parent
c5a36e2
commit 5a705b3
Showing
18 changed files
with
918 additions
and
15 deletions.
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
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,23 @@ | ||
import { 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'; |
44 changes: 44 additions & 0 deletions
44
packages/tools/kadena-cli/src/devnet/commands/devnetCreate.ts
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,44 @@ | ||
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 { createCommand } from '../../utils/createCommand.js'; | ||
import { globalOptions } from '../../utils/globalOptions.js'; | ||
import chalk from 'chalk'; | ||
import { devnetOverwritePrompt } from '../../prompts/devnet.js'; | ||
import { createExternalPrompt } from '../../prompts/generic.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`)); | ||
}, | ||
); |
57 changes: 57 additions & 0 deletions
57
packages/tools/kadena-cli/src/devnet/commands/devnetDelete.ts
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,57 @@ | ||
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 { createCommand } from '../../utils/createCommand.js'; | ||
import { dockerVolumeName, isDockerInstalled, removeDevnet, removeVolume } from '../utils/docker.js'; | ||
import { createExternalPrompt } from '../../prompts/generic.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
14
packages/tools/kadena-cli/src/devnet/commands/devnetList.ts
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,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}); | ||
|
||
displayDevnetsConfig(); | ||
}, | ||
); |
37 changes: 37 additions & 0 deletions
37
packages/tools/kadena-cli/src/devnet/commands/devnetManage.ts
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,37 @@ | ||
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 { createCommand } from '../../utils/createCommand.js'; | ||
import { createExternalPrompt } from '../../prompts/generic.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`)); | ||
}, | ||
); |
28 changes: 28 additions & 0 deletions
28
packages/tools/kadena-cli/src/devnet/commands/devnetRun.ts
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,28 @@ | ||
import debug from 'debug'; | ||
import { globalOptions } from '../../utils/globalOptions.js'; | ||
|
||
import chalk from 'chalk'; | ||
import { createCommand } from '../../utils/createCommand.js'; | ||
import { isDockerInstalled, runDevnet, stopDevnet } 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`)); | ||
}, | ||
); |
28 changes: 28 additions & 0 deletions
28
packages/tools/kadena-cli/src/devnet/commands/devnetStop.ts
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,28 @@ | ||
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
26
packages/tools/kadena-cli/src/devnet/commands/devnetUpdate.ts
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,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'); | ||
}, | ||
); |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,28 @@ | ||
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 | ||
export function devnetCommandFactory( | ||
program: Command, | ||
version: string, | ||
): void { | ||
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); | ||
} |
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,4 @@ | ||
import { devnetDefaults } from '../constants/devnets.js'; | ||
import { writeDevnet } from './utils/devnetHelpers.js'; | ||
|
||
writeDevnet(devnetDefaults.devnet); |
Oops, something went wrong.