Skip to content

Commit

Permalink
fix: refactor config manager to contain the config
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <lenin.mehedy@swirldslabs.com>
  • Loading branch information
leninmehedy committed Jan 17, 2024
1 parent 2eddad2 commit d51cdfa
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 92 deletions.
30 changes: 15 additions & 15 deletions fullstack-network-manager/src/commands/chart.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ export class ChartCommand extends BaseCommand {
}

async prepareConfig (task, argv) {
const cachedConfig = await this.configManager.setupConfig(argv)
const namespace = this.configManager.flagValue(cachedConfig, flags.namespace)
const chartDir = this.configManager.flagValue(cachedConfig, flags.chartDirectory)
const valuesFile = this.configManager.flagValue(cachedConfig, flags.valuesFile)
const deployMirrorNode = this.configManager.flagValue(cachedConfig, flags.deployMirrorNode)
const deployExplorer = this.configManager.flagValue(cachedConfig, flags.deployHederaExplorer)
const enableTls = this.configManager.flagValue(cachedConfig, flags.enableTls)
const tlsClusterIssuerName = this.configManager.flagValue(cachedConfig, flags.tlsClusterIssuerName)
const tlsClusterIssuerNamespace = this.configManager.flagValue(cachedConfig, flags.tlsClusterIssuerNamespace)
const enableHederaExplorerTls = this.configManager.flagValue(cachedConfig, flags.enableHederaExplorerTls)
const acmeClusterIssuer = this.configManager.flagValue(cachedConfig, flags.acmeClusterIssuer)
const selfSignedClusterIssuer = this.configManager.flagValue(cachedConfig, flags.selfSignedClusterIssuer)
this.configManager.load(argv)
const namespace = this.configManager.flagValue(flags.namespace)
const chartDir = this.configManager.flagValue(flags.chartDirectory)
const valuesFile = this.configManager.flagValue(flags.valuesFile)
const deployMirrorNode = this.configManager.flagValue(flags.deployMirrorNode)
const deployExplorer = this.configManager.flagValue(flags.deployHederaExplorer)
const enableTls = this.configManager.flagValue(flags.enableTls)
const tlsClusterIssuerName = this.configManager.flagValue(flags.tlsClusterIssuerName)
const tlsClusterIssuerNamespace = this.configManager.flagValue(flags.tlsClusterIssuerNamespace)
const enableHederaExplorerTls = this.configManager.flagValue(flags.enableHederaExplorerTls)
const acmeClusterIssuer = this.configManager.flagValue(flags.acmeClusterIssuer)
const selfSignedClusterIssuer = this.configManager.flagValue(flags.selfSignedClusterIssuer)

// prompt if values are missing and create a config object
const config = {
Expand All @@ -85,7 +85,7 @@ export class ChartCommand extends BaseCommand {
acmeClusterIssuer: await prompts.promptAcmeClusterIssuer(task, acmeClusterIssuer),
selfSignedClusterIssuer: await prompts.promptSelfSignedClusterIssuer(task, selfSignedClusterIssuer),
timeout: '900s',
version: cachedConfig.version
version: this.configManager.getVersion()
}

// compute values
Expand Down Expand Up @@ -153,8 +153,8 @@ export class ChartCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)
self.configManager.load(argv)
const namespace = self.configManager.flagValue(flags.namespace)
ctx.config = {
namespace: await prompts.promptNamespaceArg(task, namespace)
}
Expand Down
35 changes: 17 additions & 18 deletions fullstack-network-manager/src/commands/cluster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ export class ClusterCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
self.configManager.load(argv)

// get existing choices
ctx.clusters = await self.clusterManager.getClusters()

// extract config values
const clusterName = self.configManager.flagValue(cachedConfig, flags.clusterName)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)
const clusterName = self.configManager.flagValue(flags.clusterName)
const namespace = self.configManager.flagValue(flags.namespace)

ctx.config = {
clusterName: await prompts.promptClusterNameArg(task, clusterName),
Expand Down Expand Up @@ -167,8 +167,8 @@ export class ClusterCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
const clusterName = self.configManager.flagValue(cachedConfig, flags.clusterName)
self.configManager.load(argv)
const clusterName = self.configManager.flagValue(flags.clusterName)

// get existing choices
ctx.clusters = await self.clusterManager.getClusters()
Expand Down Expand Up @@ -217,18 +217,17 @@ export class ClusterCommand extends BaseCommand {
throw new FullstackTestingError('kubectl context is not set, set context by running: kubectl config use-context <context-name>')
}

const cachedConfig = await self.configManager.setupConfig(argv)
self.logger.debug('Setup cached config', { cachedConfig, argv })
self.configManager.load(argv)

// extract config values
const clusterName = self.configManager.flagValue(cachedConfig, flags.clusterName)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)
const chartDir = self.configManager.flagValue(cachedConfig, flags.chartDirectory)
const deployPrometheusStack = self.configManager.flagValue(cachedConfig, flags.deployPrometheusStack)
const deployMinio = self.configManager.flagValue(cachedConfig, flags.deployMinio)
const deployEnvoyGateway = self.configManager.flagValue(cachedConfig, flags.deployEnvoyGateway)
const deployCertManager = self.configManager.flagValue(cachedConfig, flags.deployCertManager)
const deployCertManagerCrds = self.configManager.flagValue(cachedConfig, flags.deployCertManagerCrds)
const clusterName = self.configManager.flagValue(flags.clusterName)
const namespace = self.configManager.flagValue(flags.namespace)
const chartDir = self.configManager.flagValue(flags.chartDirectory)
const deployPrometheusStack = self.configManager.flagValue(flags.deployPrometheusStack)
const deployMinio = self.configManager.flagValue(flags.deployMinio)
const deployEnvoyGateway = self.configManager.flagValue(flags.deployEnvoyGateway)
const deployCertManager = self.configManager.flagValue(flags.deployCertManager)
const deployCertManagerCrds = self.configManager.flagValue(flags.deployCertManagerCrds)

// get existing choices
const clusters = await self.clusterManager.getClusters()
Expand Down Expand Up @@ -321,9 +320,9 @@ export class ClusterCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
const clusterName = self.configManager.flagValue(cachedConfig, flags.clusterName)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)
self.configManager.load(argv)
const clusterName = self.configManager.flagValue(flags.clusterName)
const namespace = self.configManager.flagValue(flags.namespace)

// get existing choices
const clusters = await self.clusterManager.getClusters()
Expand Down
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 @@ -43,6 +43,15 @@ export const namespace = {
}
}

export const kubeContext = {
name: 'kube-context',
definition: {
describe: 'Kube context',
default: '',
type: 'string'
}
}

export const deployMirrorNode = {
name: 'mirror-node',
definition: {
Expand Down Expand Up @@ -279,6 +288,7 @@ export const allFlags = [
devMode,
clusterName,
namespace,
kubeContext,
deployMirrorNode,
deployHederaExplorer,
deployJsonRpcRelay,
Expand Down
2 changes: 1 addition & 1 deletion fullstack-network-manager/src/commands/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class InitCommand extends BaseCommand {
{
title: 'Setup config manager',
task: async (ctx, _) => {
ctx.config = await this.configManager.setupConfig(argv, true)
ctx.config = this.configManager.load(argv, true)
}
},
{
Expand Down
12 changes: 6 additions & 6 deletions fullstack-network-manager/src/commands/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ 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)
self.configManager.load(argv)
const namespace = self.configManager.flagValue(flags.namespace)

// get existing choices
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')
Expand Down Expand Up @@ -215,8 +215,8 @@ 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)
self.configManager.load(argv)
const namespace = self.configManager.flagValue(flags.namespace)

// get existing choices
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')
Expand Down Expand Up @@ -294,8 +294,8 @@ 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)
self.configManager.load(argv)
const namespace = self.configManager.flagValue(flags.namespace)

// get existing choices
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')
Expand Down
28 changes: 13 additions & 15 deletions fullstack-network-manager/src/commands/relay.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,19 @@ export class RelayCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
self.logger.debug('Setup cached config', { cachedConfig, argv })
self.configManager.load(argv)

// extract config values
const valuesFile = self.configManager.flagValue(cachedConfig, flags.valuesFile)
const nodeIds = self.configManager.flagValue(cachedConfig, flags.nodeIDs)
const chainId = self.configManager.flagValue(cachedConfig, flags.chainId)
const releaseTag = self.configManager.flagValue(cachedConfig, flags.releaseTag)
const replicaCount = self.configManager.flagValue(cachedConfig, flags.replicaCount)
const operatorId = self.configManager.flagValue(cachedConfig, flags.operatorId)
const operatorKey = self.configManager.flagValue(cachedConfig, flags.operatorKey)
const valuesFile = self.configManager.flagValue(flags.valuesFile)
const nodeIds = self.configManager.flagValue(flags.nodeIDs)
const chainId = self.configManager.flagValue(flags.chainId)
const releaseTag = self.configManager.flagValue(flags.releaseTag)
const replicaCount = self.configManager.flagValue(flags.replicaCount)
const operatorId = self.configManager.flagValue(flags.operatorId)
const operatorKey = self.configManager.flagValue(flags.operatorKey)

const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)
const chartDir = self.configManager.flagValue(cachedConfig, flags.chartDirectory)
const namespace = self.configManager.flagValue(flags.namespace)
const chartDir = self.configManager.flagValue(flags.chartDirectory)

// prompt if inputs are empty and set it in the context
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')
Expand Down Expand Up @@ -164,12 +163,11 @@ export class RelayCommand extends BaseCommand {
{
title: 'Initialize',
task: async (ctx, task) => {
const cachedConfig = await self.configManager.setupConfig(argv)
self.logger.debug('Setup cached config', { cachedConfig, argv })
self.configManager.load(argv)

// extract config values
const nodeIds = self.configManager.flagValue(cachedConfig, flags.nodeIDs)
const namespace = self.configManager.flagValue(cachedConfig, flags.namespace)
const nodeIds = self.configManager.flagValue(flags.nodeIDs)
const namespace = self.configManager.flagValue(flags.namespace)

// prompt if inputs are empty and set it in the context
const namespaces = await self.kubectl.getNamespace('--no-headers', '-o name')
Expand Down
104 changes: 67 additions & 37 deletions fullstack-network-manager/src/core/config_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,20 @@ export class ConfigManager {
if (!logger || !(logger instanceof Logger)) throw new MissingArgumentError('An instance of core/Logger is required')

this.logger = logger
this.config = this.load()
}

/**
* load package.json
* @returns {any}
*/
loadPackageJSON () {
try {
const raw = fs.readFileSync(`${CUR_FILE_DIR}/../../package.json`)
return JSON.parse(raw.toString())
} catch (e) {
throw new FullstackTestingError('failed to load package.json', e)
}
}

hasFlag (config, flag) {
return config.flags[flag.name] !== undefined
}

flagValue (config, flag) {
if (this.hasFlag(config, flag)) {
return config.flags[flag.name]
}

return ''
}

/**
* Load and store config
* Load and cache config on disk
*
* It overwrites previous config values using opts and store in the config file if any value has been changed.
* It overwrites previous config values using argv and store in the cached config file if any value has been changed.
*
* @param opts object containing various config related fields (e.g. argv)
* @param argv object containing various config related fields (e.g. argv)
* @param reset if we should reset old values
* @param flagList list of flags to be processed
* @returns {Promise<unknown>}
* @returns {*} config object
*/
async setupConfig (opts, reset = false, flagList = flags.allFlags) {
load (argv = {}, reset = false, flagList = flags.allFlags) {
const self = this

try {
Expand All @@ -73,10 +49,10 @@ export class ConfigManager {
config.version = packageJSON.version

// extract flags from argv
if (opts) {
if (argv) {
flagList.forEach(flag => {
if (opts && opts[flag.name] !== undefined) {
let val = opts[flag.name]
if (argv && argv[flag.name] !== undefined) {
let val = argv[flag.name]
if (val && flag.name === flags.chartDirectory.name) {
val = paths.resolve(val)
}
Expand All @@ -91,8 +67,8 @@ export class ConfigManager {
})

// store last command that was run
if (opts._) {
config.lastCommand = opts._
if (argv._) {
config.lastCommand = argv._
}
}

Expand All @@ -106,12 +82,66 @@ export class ConfigManager {
config = JSON.parse(configJSON.toString())
}

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

this.logger.setDevMode(this.flagValue(config, flags.devMode))
return config
// set dev mode for logger if necessary
this.logger.setDevMode(this.flagValue(flags.devMode))

return this.config
} catch (e) {
throw new FullstackTestingError(`failed to load config: ${e.message}`, e)
}
}

/**
* load package.json
* @returns {any}
*/
loadPackageJSON () {
try {
const raw = fs.readFileSync(`${CUR_FILE_DIR}/../../package.json`)
return JSON.parse(raw.toString())
} catch (e) {
throw new FullstackTestingError('failed to load package.json', e)
}
}

/**
* Return the value of the given flag
*
* @param flag flag object
* @return {*|string}
*/
flagValue (flag) {
if (this.config.flags[flag.name] !== undefined) {
return this.config.flags[flag.name]
}

return ''
}

/**
* Get package version
* @return {*}
*/
getVersion () {
return this.config.version
}

/**
* Get last updated at timestamp
* @return {string}
*/
getUpdatedAt () {
return this.config.updatedAt
}

/**
* Get last command
* @return {*}
*/
getLastCommand () {
return this.config.lastCommand
}
}
Loading

0 comments on commit d51cdfa

Please sign in to comment.