Skip to content

Commit

Permalink
Add metadata handling to config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-senechal committed Jan 13, 2025
1 parent d9355a8 commit 4258501
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cli/src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PublicApiUrl } from '@massalabs/massa-web3'
import { Address, PublicApiUrl } from '@massalabs/massa-web3'
import { OptionValues } from 'commander'
import { readFileSync } from 'fs'

import { Metadata } from '../lib/website/models/Metadata'

export const DEFAULT_CHUNK_SIZE = 64000
const DEFAULT_NODE_URL = PublicApiUrl.Buildnet

Expand All @@ -11,12 +13,25 @@ interface Config {
node_url: string
chunk_size: number
secret_key: string
address: string
metadatas: { [key: string]: string }
}

export function parseConfigFile(filePath: string): Config {
const fileContent = readFileSync(filePath, 'utf-8')
try {
return JSON.parse(fileContent)
const config = JSON.parse(fileContent) as Config

// If address is provided, make sure it's valid
if (config.address) {
try {
Address.fromString(config.address)
} catch (error) {
throw new Error(`Invalid address in config file: ${error}`)
}
}

return config
} catch (error) {
throw new Error(`Failed to parse file: ${error}`)
}
Expand All @@ -35,9 +50,19 @@ export function mergeConfigAndOptions(
commandOptions.node_url || configOptions.node_url || DEFAULT_NODE_URL,
chunk_size: configOptions.chunk_size || DEFAULT_CHUNK_SIZE,
secret_key: configOptions.secret_key || '',
address: configOptions.address || '',
metadatas: configOptions.metadatas
? makeMetadataArray(configOptions.metadatas)
: [],
}
}

function makeMetadataArray(metadatas: { [key: string]: string }): Metadata[] {
return Object.entries(metadatas).map(
([key, value]) => new Metadata(key, value)
)
}

export function setDefaultValues(commandOptions: OptionValues): OptionValues {
return {
node_url: commandOptions.node_url || DEFAULT_NODE_URL,
Expand Down
4 changes: 4 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
setDefaultValues,
} from './commands/config'

import { Metadata } from './lib/website/models/Metadata'

const version = process.env.VERSION || 'dev'
const defaultConfigPath = 'deweb_cli_config.json'

Expand All @@ -38,6 +40,8 @@ interface OptionValues {
node_url: string
wallet: string
password: string
address: string
metadatas: Metadata[]
}

const commandOptions: OptionValues = program.opts() as OptionValues
Expand Down

0 comments on commit 4258501

Please sign in to comment.