diff --git a/cli/src/lib/conn.ts b/cli/src/lib/conn.ts index 9b95b3c1c..b60b07cfc 100644 --- a/cli/src/lib/conn.ts +++ b/cli/src/lib/conn.ts @@ -8,7 +8,7 @@ import * as Debug from 'debug' const conn = (options: ConnectOptions) => { const { debug, saveOptions, loadOptions } = options - loadOptions && (options = handleLoadOptions('conn', loadOptions)) + loadOptions && (options = handleLoadOptions('conn', loadOptions, options)) saveOptions && handleSaveOptions('conn', options) @@ -57,7 +57,7 @@ const conn = (options: ConnectOptions) => { const benchConn = async (options: BenchConnectOptions) => { const { saveOptions, loadOptions } = options - loadOptions && (options = handleLoadOptions('benchConn', loadOptions)) + loadOptions && (options = handleLoadOptions('benchConn', loadOptions, options)) saveOptions && handleSaveOptions('benchConn', options) diff --git a/cli/src/lib/pub.ts b/cli/src/lib/pub.ts index 82334ab19..1511a18c3 100644 --- a/cli/src/lib/pub.ts +++ b/cli/src/lib/pub.ts @@ -211,7 +211,7 @@ const handleFileRead = (filePath: string) => { const pub = (options: PublishOptions) => { const { debug, saveOptions, loadOptions } = options - loadOptions && (options = handleLoadOptions('pub', loadOptions)) + loadOptions && (options = handleLoadOptions('pub', loadOptions, options)) saveOptions && handleSaveOptions('pub', options) @@ -252,14 +252,14 @@ const multiPub = async (commandType: CommandType, options: BenchPublishOptions | let simulator: Simulator = {} as Simulator if (commandType === 'simulate') { - options = loadOptions ? handleLoadOptions('simulate', loadOptions) : options + options = loadOptions ? handleLoadOptions('simulate', loadOptions, options as SimulatePubOptions) : options saveOptions && handleSaveOptions('simulate', options) const simulateOptions = options as SimulatePubOptions checkScenarioExists(simulateOptions.scenario, simulateOptions.file) simulator = loadSimulator(simulateOptions.scenario, simulateOptions.file) } else { - options = loadOptions ? handleLoadOptions('benchPub', loadOptions) : options + options = loadOptions ? handleLoadOptions('benchPub', loadOptions, options as BenchPublishOptions) : options saveOptions && handleSaveOptions('benchPub', options) } diff --git a/cli/src/lib/sub.ts b/cli/src/lib/sub.ts index f740888b3..ee968c731 100644 --- a/cli/src/lib/sub.ts +++ b/cli/src/lib/sub.ts @@ -79,7 +79,7 @@ const handleDefaultBinaryFile = (format: FormatType | undefined, filePath?: stri const sub = (options: SubscribeOptions) => { const { loadOptions, saveOptions } = options - loadOptions && (options = handleLoadOptions('sub', loadOptions)) + loadOptions && (options = handleLoadOptions('sub', loadOptions, options)) saveOptions && handleSaveOptions('sub', options) @@ -232,7 +232,7 @@ const sub = (options: SubscribeOptions) => { const benchSub = async (options: BenchSubscribeOptions) => { const { saveOptions, loadOptions } = options - loadOptions && (options = handleLoadOptions('benchSub', loadOptions)) + loadOptions && (options = handleLoadOptions('benchSub', loadOptions, options)) saveOptions && handleSaveOptions('benchSub', options) diff --git a/cli/src/types/global.d.ts b/cli/src/types/global.d.ts index 7afb5db73..c657163cc 100644 --- a/cli/src/types/global.d.ts +++ b/cli/src/types/global.d.ts @@ -1,6 +1,15 @@ declare global { type CommandType = 'conn' | 'pub' | 'sub' | 'benchConn' | 'benchPub' | 'benchSub' | 'simulate' + type OptionsType = + | ConnectOptions + | PublishOptions + | SubscribeOptions + | BenchConnectOptions + | BenchPublishOptions + | BenchSubscribeOptions + | SimulatePubOptions + type MQTTVersion = 3 | 4 | 5 type Protocol = 'mqtt' | 'mqtts' | 'ws' | 'wss' diff --git a/cli/src/utils/options.ts b/cli/src/utils/options.ts index 43b3c446c..3f0356fb7 100644 --- a/cli/src/utils/options.ts +++ b/cli/src/utils/options.ts @@ -80,21 +80,37 @@ const handleSaveOptions = ( * @param savePath - The path to the configuration file. * @returns The options for the specified command type. */ -function handleLoadOptions(commandType: 'conn', savePath: boolean | string): ConnectOptions -function handleLoadOptions(commandType: 'pub', savePath: boolean | string): PublishOptions -function handleLoadOptions(commandType: 'sub', savePath: boolean | string): SubscribeOptions -function handleLoadOptions(commandType: 'benchConn', savePath: boolean | string): BenchConnectOptions -function handleLoadOptions(commandType: 'benchPub', savePath: boolean | string): BenchPublishOptions -function handleLoadOptions(commandType: 'benchSub', savePath: boolean | string): BenchSubscribeOptions -function handleLoadOptions(commandType: 'simulate', savePath: boolean | string): SimulatePubOptions -function handleLoadOptions(commandType: CommandType, savePath: boolean | string) { +function handleLoadOptions(commandType: 'conn', savePath: boolean | string, opts: ConnectOptions): ConnectOptions +function handleLoadOptions(commandType: 'pub', savePath: boolean | string, opts: PublishOptions): PublishOptions +function handleLoadOptions(commandType: 'sub', savePath: boolean | string, opts: SubscribeOptions): SubscribeOptions +function handleLoadOptions( + commandType: 'benchConn', + savePath: boolean | string, + opts: BenchConnectOptions, +): BenchConnectOptions +function handleLoadOptions( + commandType: 'benchPub', + savePath: boolean | string, + opts: BenchPublishOptions, +): BenchPublishOptions +function handleLoadOptions( + commandType: 'benchSub', + savePath: boolean | string, + opts: BenchSubscribeOptions, +): BenchSubscribeOptions +function handleLoadOptions( + commandType: 'simulate', + savePath: boolean | string, + opts: SimulatePubOptions, +): SimulatePubOptions +function handleLoadOptions(commandType: CommandType, savePath: boolean | string, opts: OptionsType) { try { const filePath = processPath(savePath, defaultPath) if (fileExists(filePath)) { const data = readFile(filePath).toString() const config = parseYamlOrJson(data, isYaml(filePath)) validateOptions(commandType, filePath, config) - return config[commandType] + return { ...config[commandType], ...opts } } else { logWrapper.fail(`Configuration file ${filePath} not found`) process.exit(1)