Skip to content

Commit

Permalink
init config.ts & clean fn's
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Oct 2, 2024
1 parent 09a153b commit a26597d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
33 changes: 33 additions & 0 deletions cli/src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { OptionValues } from 'commander'
import { readFileSync } from 'fs'

interface Config {
wallet_password: string
wallet_path: string
node_url: string
chunk_size: number
secret_key: string
}

export function parseConfigFile(filePath: string): Config {
const fileContent = readFileSync(filePath, 'utf-8')
try {
return JSON.parse(fileContent)
} catch (error) {
throw new Error(`Failed to parse file: ${error}`)
}
}

export function mergeConfigAndOptions(
commandOptions: OptionValues,
configOptions: Config
): OptionValues {
if (!configOptions) return commandOptions

const wallet = commandOptions.wallet || configOptions.wallet_path
const password = commandOptions.password || configOptions.wallet_password
const node_url = commandOptions.node_url || configOptions.node_url
const chunk_size = commandOptions.chunk_size || configOptions.chunk_size

return { wallet, password, node_url, chunk_size }
}
1 change: 0 additions & 1 deletion cli/src/commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const uploadCommand = new Command('upload')
)
.action(async (websiteDirPath, options, command) => {
const globalOptions = command.optsWithGlobals()
console.log('globalOptions:', globalOptions)

// throw if global options are not defined
if (!globalOptions) {
Expand Down
37 changes: 0 additions & 37 deletions cli/src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,40 +114,3 @@ export function validateAddress(address: string) {
process.exit(1)
}
}

interface WalletConfig {
wallet: string
password: string
}

interface Config {
wallet_config: WalletConfig
node_url: string
chunk_size: number
secret_key: string
}

export function parseConfigFile(filePath: string): Config {
const fileContent = readFileSync(filePath, 'utf-8')
try {
const parsedData = JSON.parse(fileContent)
return parsedData
} catch (error) {
throw new Error(`Failed to parse file: ${error}`)
}
}

export function setProgramOptions(
commandOptions: OptionValues,
configOptions: Config
): OptionValues {
if (!configOptions) return commandOptions

const wallet = commandOptions.wallet || configOptions.wallet_config.wallet
const password =
commandOptions.password || configOptions.wallet_config.password
const node_url = commandOptions.node_url || configOptions.node_url
const chunk_size = commandOptions.chunk_size || configOptions.chunk_size

return { wallet, password, node_url, chunk_size }
}
5 changes: 3 additions & 2 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { deleteCommand } from './commands/delete'
import { listFilesCommand } from './commands/list'
import { showFileCommand } from './commands/showFile'
import { uploadCommand } from './commands/upload'
import { parseConfigFile, setProgramOptions } from './commands/utils'

import { existsSync } from 'fs'
import { mergeConfigAndOptions, parseConfigFile } from './commands/config'

const version = process.env.VERSION || 'dev'
const defaultConfigPath = 'deweb_cli_config.json'
Expand Down Expand Up @@ -41,7 +42,7 @@ if (existsSync(commandOptions.config)) {
const configOptions = parseConfigFile(commandOptions.config)

// commandOptions get priority over configOptions
const programOptions = setProgramOptions(commandOptions, configOptions)
const programOptions = mergeConfigAndOptions(commandOptions, configOptions)
for (const [key, value] of Object.entries(programOptions)) {
program.setOptionValue(key, value)
}
Expand Down

0 comments on commit a26597d

Please sign in to comment.