Skip to content

Commit

Permalink
gracefully handle no configFile
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Sep 27, 2024
1 parent 3467415 commit afdcd24
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cli/src/commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const uploadCommand = new Command('upload')
DEFAULT_CHUNK_SIZE.toString()
)
.action(async (websiteDirPath, options, command) => {
let globalOptions = command.parent?.opts()
let globalOptions = command.optsWithGlobals()

// throw if global options are not defined
if (!globalOptions) {
Expand All @@ -54,7 +54,8 @@ export const uploadCommand = new Command('upload')
)

// set chunksize from options or config
const chunkSize = parseInt(options.chunkSize) || config.chunk_size
const chunkSize =
parseInt(options.chunkSize) || (globalOptions.chunk_size as number)

const ctx: UploadCtx = {
provider: provider,
Expand Down
14 changes: 8 additions & 6 deletions cli/src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const KEY_ENV_NAME = 'SECRET_KEY'
*/
export async function makeProviderFromNodeURLAndSecret(
globalOptions: OptionValues,
config: Config
config: Config | null
): Promise<Web3Provider> {
if (!globalOptions.node_url) {
throw new Error('node_url is not defined. Please use --node_url to set one')
Expand All @@ -42,6 +42,7 @@ export async function makeProviderFromNodeURLAndSecret(
}

try {
if (!config?.secret_key) throw new Error()
keyPair = await KeyPair.fromPrivateKey(config.secret_key)
console.info('Initializing provider with SECRET_KEY from config file\n')
provider = Web3Provider.fromRPCUrl(
Expand Down Expand Up @@ -129,7 +130,8 @@ interface Config {
secret_key: string
}

export function parseConfigFile(filePath: string): Config {
export function parseConfigFile(filePath: string): Config | null {
if (!existsSync(filePath)) return null
const fileContent = readFileSync(filePath, 'utf-8')
try {
const parsedData = JSON.parse(fileContent)
Expand All @@ -141,13 +143,13 @@ export function parseConfigFile(filePath: string): Config {

export function setConfigGlobalOptions(
globalOptions: OptionValues,
config: Config
config: Config | null
): OptionValues {
//TODO: check if this should be done before
if (!existsSync(globalOptions.config)) return globalOptions
if (!config) return globalOptions

const wallet = globalOptions.wallet || config.wallet_config.wallet
const password = globalOptions.password || config.wallet_config.password
const node_url = globalOptions.node_url || config.node_url
return { wallet, password, node_url }
const chunk_size = globalOptions.chunk_size || config.chunk_size
return { wallet, password, node_url, chunk_size }
}

0 comments on commit afdcd24

Please sign in to comment.