|
| 1 | +import { assertChain, chains, type Chain } from "@wormhole-foundation/sdk"; |
| 2 | +import * as yargs from "yargs"; |
| 3 | +import fs from "fs"; |
| 4 | +import { ensureNttRoot } from "."; |
| 5 | +import chalk from "chalk"; |
| 6 | + |
| 7 | +// We support project-local and global configuration. |
| 8 | +// The configuration is stored in JSON files in $HOME/.ntt-cli/config.json (global) and .ntt-cli/config.json (local). |
| 9 | +// These can further be overridden by environment variables of the form CHAIN_KEY=value. |
| 10 | +type Scope = "global" | "local"; |
| 11 | + |
| 12 | +type Config = { |
| 13 | + chains: Partial<{ |
| 14 | + [C in Chain]: ChainConfig; |
| 15 | + }> |
| 16 | +} |
| 17 | + |
| 18 | +type ChainConfig = Partial<typeof configTemplate>; |
| 19 | + |
| 20 | +// TODO: per-network configuration? (i.e. mainnet, testnet, etc) |
| 21 | +const configTemplate = { |
| 22 | + scan_api_key: "", |
| 23 | +}; |
| 24 | + |
| 25 | +function assertChainConfigKey(key: string): asserts key is keyof ChainConfig { |
| 26 | + const validKeys = Object.keys(configTemplate); |
| 27 | + if (!validKeys.includes(key)) { |
| 28 | + throw new Error(`Invalid key: ${key}`); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +const options = { |
| 33 | + chain: { |
| 34 | + describe: "Chain", |
| 35 | + type: "string", |
| 36 | + choices: chains, |
| 37 | + demandOption: true, |
| 38 | + }, |
| 39 | + key: { |
| 40 | + describe: "Key", |
| 41 | + type: "string", |
| 42 | + choices: Object.keys(configTemplate), |
| 43 | + demandOption: true, |
| 44 | + }, |
| 45 | + value: { |
| 46 | + describe: "Value", |
| 47 | + type: "string", |
| 48 | + demandOption: true, |
| 49 | + }, |
| 50 | + local: { |
| 51 | + describe: "Use local configuration", |
| 52 | + type: "boolean", |
| 53 | + default: false, |
| 54 | + }, |
| 55 | + global: { |
| 56 | + describe: "Use global configuration", |
| 57 | + type: "boolean", |
| 58 | + default: true, |
| 59 | + } |
| 60 | +} as const; |
| 61 | +export const command = (args: yargs.Argv<{}>) => args |
| 62 | + .command("set-chain <chain> <key> <value>", |
| 63 | + "set a configuration value for a chain", |
| 64 | + (yargs) => yargs |
| 65 | + .positional("chain", options.chain) |
| 66 | + .positional("key", options.key) |
| 67 | + .positional("value", options.value) |
| 68 | + .option("local", options.local) |
| 69 | + .option("global", options.global), |
| 70 | + (argv) => { |
| 71 | + const scope = resolveScope(argv.local, argv.global); |
| 72 | + assertChain(argv.chain); |
| 73 | + assertChainConfigKey(argv.key); |
| 74 | + setChainConfig(scope, argv.chain, argv.key, argv.value); |
| 75 | + }) |
| 76 | + .command("unset-chain <chain> <key>", |
| 77 | + "unset a configuration value for a chain", |
| 78 | + (yargs) => yargs |
| 79 | + .positional("chain", options.chain) |
| 80 | + .positional("key", options.key) |
| 81 | + .option("local", options.local) |
| 82 | + .option("global", options.global), |
| 83 | + (argv) => { |
| 84 | + const scope = resolveScope(argv.local, argv.global); |
| 85 | + assertChainConfigKey(argv.key); |
| 86 | + assertChain(argv.chain); |
| 87 | + setChainConfig(scope, argv.chain, argv.key, undefined); |
| 88 | + }) |
| 89 | + .command("get-chain <chain> <key>", |
| 90 | + "get a configuration value", |
| 91 | + (yargs) => yargs |
| 92 | + .positional("chain", options.chain) |
| 93 | + .positional("key", options.key) |
| 94 | + .option("local", options.local) |
| 95 | + .option("global", options.global), |
| 96 | + (argv) => { |
| 97 | + const scope = resolveScope(argv.local, argv.global); |
| 98 | + assertChainConfigKey(argv.key); |
| 99 | + assertChain(argv.chain); |
| 100 | + const val = getChainConfig(argv.scope as Scope, argv.chain, argv.key); |
| 101 | + if (!val) { |
| 102 | + console.error("undefined"); |
| 103 | + } else { |
| 104 | + console.log(val); |
| 105 | + } |
| 106 | + }) |
| 107 | + .demandCommand() |
| 108 | + |
| 109 | +function findOrCreateConfigFile(scope: Scope): string { |
| 110 | + // if scope is global, touch $HOME/.ntt-cli/config.json |
| 111 | + // if scope is local, touch .ntt-cli/config.json. In the latter case, make sure we're in an ntt project (call ensureNttRoot()) |
| 112 | + |
| 113 | + // if the file doesn't exist, write an empty object |
| 114 | + let configDir; |
| 115 | + |
| 116 | + switch (scope) { |
| 117 | + case "global": |
| 118 | + if (!process.env.HOME) { |
| 119 | + throw new Error("Could not determine home directory"); |
| 120 | + } |
| 121 | + configDir = `${process.env.HOME}/.ntt-cli`; |
| 122 | + break; |
| 123 | + case "local": |
| 124 | + ensureNttRoot(); |
| 125 | + configDir = ".ntt-cli"; |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + const emptyConfig: Config = { |
| 130 | + chains: {}, |
| 131 | + }; |
| 132 | + |
| 133 | + if (!fs.existsSync(configDir)) { |
| 134 | + fs.mkdirSync(configDir); |
| 135 | + } |
| 136 | + const configFile = `${configDir}/config.json`; |
| 137 | + if (!fs.existsSync(configFile)) { |
| 138 | + fs.writeFileSync(configFile, JSON.stringify(emptyConfig, null, 2)); |
| 139 | + } |
| 140 | + return configFile; |
| 141 | +} |
| 142 | + |
| 143 | +function setChainConfig(scope: Scope, chain: Chain, key: keyof ChainConfig, value: string | undefined) { |
| 144 | + const configFile = findOrCreateConfigFile(scope); |
| 145 | + const config = JSON.parse(fs.readFileSync(configFile, "utf-8")) as Config; |
| 146 | + if (!config.chains[chain]) { |
| 147 | + config.chains[chain] = {}; |
| 148 | + } |
| 149 | + config.chains[chain]![key] = value; |
| 150 | + fs.writeFileSync(configFile, JSON.stringify(config, null, 2)); |
| 151 | +} |
| 152 | + |
| 153 | +function getChainConfig(scope: Scope, chain: Chain, key: keyof ChainConfig): string | undefined { |
| 154 | + const configFile = findOrCreateConfigFile(scope); |
| 155 | + const config = JSON.parse(fs.readFileSync(configFile, "utf-8")) as Config; |
| 156 | + return config.chains[chain]?.[key]; |
| 157 | +} |
| 158 | + |
| 159 | +function envVarName(chain: Chain, key: keyof ChainConfig): string { |
| 160 | + return `${chain.toUpperCase()}_${key.toUpperCase()}`; |
| 161 | +} |
| 162 | + |
| 163 | +export function get( |
| 164 | + chain: Chain, |
| 165 | + key: keyof ChainConfig, |
| 166 | + { reportError = false } |
| 167 | +): string | undefined { |
| 168 | + const varName = envVarName(chain, key); |
| 169 | + const env = process.env[varName]; |
| 170 | + if (env) { |
| 171 | + console.info(chalk.yellow(`Using ${varName} for ${chain} ${key}`)); |
| 172 | + return env; |
| 173 | + } |
| 174 | + const local = getChainConfig("local", chain, key); |
| 175 | + if (local) { |
| 176 | + console.info(chalk.yellow(`Using local configuration for ${chain} ${key} (in .ntt-cli/config.json)`)); |
| 177 | + return local; |
| 178 | + } |
| 179 | + const global = getChainConfig("global", chain, key); |
| 180 | + if (global) { |
| 181 | + console.info(chalk.yellow(`Using global configuration for ${chain} ${key} (in $HOME/.ntt-cli/config.json)`)); |
| 182 | + return global; |
| 183 | + } |
| 184 | + if (reportError) { |
| 185 | + console.error(`Could not find configuration for ${chain} ${key}`); |
| 186 | + console.error(`Please set it using 'ntt config set-chain ${chain} ${key} <value>' or by setting the environment variable ${varName}`); |
| 187 | + } |
| 188 | +} |
| 189 | +function resolveScope(local: boolean, global: boolean) { |
| 190 | + if (local && global) { |
| 191 | + throw new Error("Cannot specify both --local and --global"); |
| 192 | + } |
| 193 | + if (local) { |
| 194 | + return "local"; |
| 195 | + } |
| 196 | + if (global) { |
| 197 | + return "global"; |
| 198 | + } |
| 199 | + throw new Error("Must specify either --local or --global"); |
| 200 | +} |
0 commit comments