Skip to content

Commit

Permalink
Support for config file specification in any directory as specified i…
Browse files Browse the repository at this point in the history
…n the config parameter value (over and above the default ~/.stm location)
  • Loading branch information
gvensan committed Oct 11, 2024
1 parent b410535 commit b4610d8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solace-community/stm",
"version": "0.0.59",
"version": "0.0.60",
"description": "Solace Try-Me Command Line Tool",
"repository": {
"type": "git",
Expand Down
38 changes: 37 additions & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,18 @@ export const saveConfig = (data: any) => {

export const loadConfig = (configFile: string) => {
try {
const localFilePath = processPath(`${configFile}`)
const filePath = processPath(`${defaultStmHome}/${configFile}`)
if (fileExists(filePath)) {
Logger.info(`loading configuration '${configFile}'`)
const config = readFile(filePath)
// TODO: validateConfig(config)
return config;
} else if (fileExists(localFilePath)) {
Logger.info(`loading configuration '${configFile}'`)
const config = readFile(filePath)
// TODO: validateConfig(config)
return config;
} else {
Logger.logDetailedError(`configuration not found`, `${decoratePath(configFile)}`)
Logger.logError('exiting...')
Expand Down Expand Up @@ -322,8 +328,10 @@ export const loadCommandFromConfig = (cmd: string, options: MessageClientOptions

var commandName = options.name ? options.name : cmd
const filePath = processPath(`${defaultStmHome}/${options.config as string}`)
const localFilePath = processPath(`${options.config as string}`)
var config = null;
if (fileExists(filePath)) {
const config = readFile(filePath)
config = readFile(filePath)
Logger.info(`loading '${commandName}' command from configuration '${chalk.cyanBright(filePath)}'`)
if (!config[group][commandName]) {
Logger.logError(`could not find '${commandName}' command`)
Expand All @@ -349,6 +357,34 @@ export const loadCommandFromConfig = (cmd: string, options: MessageClientOptions
configOptions[clientKeys[i]] = config[group][commandName][clientKeys[i]];
}

return configOptions
} else if (fileExists(localFilePath)) {
config = readFile(localFilePath)
Logger.info(`loading '${commandName}' command from configuration '${chalk.cyanBright(localFilePath)}'`)
if (!config[group][commandName]) {
Logger.logError(`could not find '${commandName}' command`)
// commandName = cmd
Logger.logError('exiting...')
process.exit(1)
}
if (config[group][commandName].command !== cmd) {
Logger.logDetailedError(`expected '${commandName}' command, but found '${config[group][commandName].command}'`, `specify a valid command name`)
Logger.logError('exiting...')
process.exit(1)
}
// TODO: validateConfig(config)

const configOptions:any = {}
const connectionKeys = Object.keys(group === 'manage' ? defaultManageConnectionConfig : defaultMessageConnectionConfig);
for (var i=0; i<connectionKeys.length; i++) {
configOptions[connectionKeys[i]] = config[group][group === 'manage' ? 'sempconnection' : 'connection'][connectionKeys[i]];
}
const defaultConfig = getDefaultConfig(cmd);
const clientKeys = Object.keys(defaultConfig);
for (var i=0; i<clientKeys.length; i++) {
configOptions[clientKeys[i]] = config[group][commandName][clientKeys[i]];
}

return configOptions
} else {
if (options.config === defaultConfigFile) {
Expand Down

0 comments on commit b4610d8

Please sign in to comment.