Skip to content

Commit

Permalink
fix(cli): enable --dev flag for dev mode (#644)
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <lenin.mehedy@swirldslabs.com>
  • Loading branch information
leninmehedy authored Jan 9, 2024
1 parent 8a458d8 commit 617b948
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 35 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/zxc-fsnetman-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ permissions:
jobs:
test:
name: ${{ inputs.custom-job-label || 'fsnetman Test' }}
runs-on: [self-hosted, Linux, medium, ephemeral]
runs-on: [ self-hosted, Linux, medium, ephemeral ]
steps:
- name: Get related changed files
id: changed-files
Expand Down Expand Up @@ -125,7 +125,6 @@ jobs:
working-directory: fullstack-network-manager
if: ${{ steps.check-changed-files.outputs.run-tests && !cancelled() && !failure() }}
run: |
export DEV_MODE=true
npm i
npm link
fsnetman init -d ../charts
Expand Down
8 changes: 0 additions & 8 deletions fullstack-network-manager/fsnetman.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#!/usr/bin/env node
import * as fnm from './src/index.mjs'

// Check the value of the DEV_MODE environment variable, ignoring case
const devMode = process.env.DEV_MODE ? process.env.DEV_MODE.toLowerCase() === 'true' : false

// Disable stack traces if DEV_MODE is false
if (!devMode) {
Error.stackTraceLimit = 0
}

fnm.main(process.argv)
10 changes: 10 additions & 0 deletions fullstack-network-manager/src/commands/flags.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export function setCommandFlags (y, ...commandFlags) {
})
}

export const devMode = {
name: 'dev',
definition: {
describe: 'Enable developer mode',
default: false,
type: 'boolean'
}
}

// list of common flags across commands. command specific flags are defined in the command's module.
export const clusterName = {
name: 'cluster-name',
Expand Down Expand Up @@ -267,6 +276,7 @@ export const enableHederaExplorerTls = {
}

export const allFlags = [
devMode,
clusterName,
namespace,
deployMirrorNode,
Expand Down
3 changes: 2 additions & 1 deletion fullstack-network-manager/src/commands/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InitCommand } from './init.mjs'
import { ChartCommand } from './chart.mjs'
import { NodeCommand } from './node.mjs'
import { RelayCommand } from './relay.mjs'
import * as flags from './flags.mjs'

/*
* Return a list of Yargs command builder to be exposed through CLI
Expand All @@ -25,4 +26,4 @@ function Initialize (opts) {
}

// Expose components from the command module
export { Initialize }
export { Initialize, flags }
24 changes: 21 additions & 3 deletions fullstack-network-manager/src/commands/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ export class NodeCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)

// get existing choices
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')

const config = {
namespace: await prompts.promptNamespaceArg(task, argv.namespace),
namespace: await prompts.promptSelectNamespaceArg(task, namespace, namespaces),
nodeIds: await prompts.promptNodeIdsArg(task, argv.nodeIds),
releaseTag: await prompts.promptReleaseTag(task, argv.releaseTag),
cacheDir: await prompts.promptCacheDir(task, argv.cacheDir),
Expand Down Expand Up @@ -209,8 +215,14 @@ export class NodeCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)

// get existing choices
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')

ctx.config = {
namespace: await prompts.promptNamespaceArg(task, argv.namespace),
namespace: await prompts.promptSelectNamespaceArg(task, namespace, namespaces),
nodeIds: await prompts.promptNodeIdsArg(task, argv.nodeIds)
}
}
Expand Down Expand Up @@ -282,8 +294,14 @@ export class NodeCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)

// get existing choices
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')

ctx.config = {
namespace: await prompts.promptNamespaceArg(task, argv.namespace),
namespace: await prompts.promptSelectNamespaceArg(task, namespace, namespaces),
nodeIds: await prompts.promptNodeIdsArg(task, argv.nodeIds)
}
}
Expand Down
3 changes: 3 additions & 0 deletions fullstack-network-manager/src/core/config_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export class ConfigManager {
config = JSON.parse(configJSON.toString())
}

this.logger.debug('Setup cached config', { cachedConfig: config })

this.logger.setDevMode(this.flagValue(config, flags.devMode))
return config
} catch (e) {
throw new FullstackTestingError(`failed to load config: ${e.message}`, e)
Expand Down
37 changes: 19 additions & 18 deletions fullstack-network-manager/src/core/logging.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,11 @@ export const Logger = class {
/**
* Create a new logger
* @param level logging level as supported by winston library:
* {
* emerg: 0,
* alert: 1,
* crit: 2,
* error: 3,
* warning: 4,
* notice: 5,
* info: 6,
* debug: 7
* }
* @constructor
*/
constructor (level) {
constructor (level = 'debug') {
this.nextTraceId()
this.devMode = false

this.winstonLogger = winston.createLogger({
level,
Expand All @@ -76,6 +67,15 @@ export const Logger = class {
})
}

setDevMode (devMode) {
this.debug(`setting dev mode: ${devMode}`)
this.devMode = devMode
}

setLevel (level) {
this.winstonLogger.setLevel(level)
}

nextTraceId () {
this.traceId = uuidv4()
}
Expand All @@ -94,24 +94,25 @@ export const Logger = class {
}

showUserError (err) {
this.error(err.message, err)

console.log(chalk.red('ERROR: '))
console.log(err.stack)

const stack = [{ message: err.message, stacktrace: err.stack }]
if (err.cause) {
let depth = 0
let cause = err.cause
while (cause !== undefined && depth < 10) {
if (cause.stack) {
console.log(chalk.red('Caused by:'))
console.log(cause.stack)
stack.push({ message: cause.message, stacktrace: cause.stack })
}

cause = cause.cause
depth += 1
}
}

if (this.devMode) {
console.log(stack)
}

this.error(err.message, { error: err.message, stacktrace: stack })
}

error (msg, ...args) {
Expand Down
15 changes: 14 additions & 1 deletion fullstack-network-manager/src/core/shell_runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,23 @@ export class ShellRunner {
errOutput.forEach(m => self.logger.showUser(chalk.red(m)))
}

self.logger.error(`Error executing: '${cmd}'`, {
commandExitCode: code,
commandExitSignal: signal,
commandOutput: output,
errOutput,
error: { message: err.message, stack: err.stack }
})

reject(err)
}

self.logger.debug(`Finished executing: '${cmd}'`, { commandExitCode: code, commandExitSignal: signal, commandOutput: output })
self.logger.debug(`Finished executing: '${cmd}'`, {
commandExitCode: code,
commandExitSignal: signal,
commandOutput: output,
errOutput
})
resolve(output)
})
})
Expand Down
4 changes: 3 additions & 1 deletion fullstack-network-manager/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import { flags } from './commands/index.mjs'
import * as commands from './commands/index.mjs'
import * as core from './core/index.mjs'
import { ChartManager, ConfigManager, DependencyManager } from './core/index.mjs'
import 'dotenv/config'

export function main (argv) {
const logger = core.logging.NewLogger('debug')
const logger = core.logging.NewLogger()
const kind = new core.Kind(logger)
const helm = new core.Helm(logger)
const kubectl = new core.Kubectl(logger)
Expand Down Expand Up @@ -36,6 +37,7 @@ export function main (argv) {
.alias('v', 'version')
.command(commands.Initialize(opts))
.strict()
.option(flags.devMode.name, flags.devMode.definition)
.wrap(120)
.demand(1, 'Select a command')
.parse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('ShellRunner', () => {
expect(loggerSpy).toHaveBeenNthCalledWith(2, 'Finished executing: \'ls -l\'', {
commandExitCode: expect.any(Number),
commandExitSignal: null,
commandOutput: expect.any(Array)
commandOutput: expect.any(Array),
errOutput: expect.any(Array)
})
expect(readableSpy).toHaveBeenCalledWith('data', expect.anything())
expect(childProcessSpy).toHaveBeenCalledWith('exit', expect.anything())
Expand Down

0 comments on commit 617b948

Please sign in to comment.