Skip to content

Commit

Permalink
improve(cli): enhance handleLoadOptions to accept existing options
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Asuka committed Nov 15, 2024
1 parent 3b3258c commit 53e2d80
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cli/src/lib/conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/lib/sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
34 changes: 25 additions & 9 deletions cli/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 53e2d80

Please sign in to comment.