diff --git a/cli/src/commands/config.ts b/cli/src/commands/config.ts index 8ed0db2..76b4996 100644 --- a/cli/src/commands/config.ts +++ b/cli/src/commands/config.ts @@ -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 @@ -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}`) } @@ -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, diff --git a/cli/src/index.ts b/cli/src/index.ts index cb1e7e6..86134e3 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -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' @@ -38,6 +40,8 @@ interface OptionValues { node_url: string wallet: string password: string + address: string + metadatas: Metadata[] } const commandOptions: OptionValues = program.opts() as OptionValues