open source is social v-it.org
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 sol pbc
3
4import { loadConfig, saveConfig, getScalars } from '../lib/config.js';
5import { formatError } from '../lib/error-format.js';
6
7function coerceValue(str) {
8 if (str === 'true') return true;
9 if (str === 'false') return false;
10 const n = Number(str);
11 if (str !== '' && !isNaN(n)) return n;
12 return str;
13}
14
15export default function register(program) {
16 program
17 .command('config')
18 .description('Read and write vit.json configuration')
19 .argument('[action]', 'list (default), set, or delete')
20 .argument('[key]', 'configuration key')
21 .argument('[value]', 'value to set')
22 .action(async (action, key, value) => {
23 try {
24 // Default to list when no action given
25 if (!action || action === 'list') {
26 const config = loadConfig();
27 const scalars = [...getScalars(config)];
28 if (scalars.length === 0) {
29 console.log("no configuration set. run 'vit login <handle>' to get started.");
30 return;
31 }
32 for (const [k, v] of scalars) {
33 console.log(`${k}=${v}`);
34 }
35 return;
36 }
37
38 if (action === 'set') {
39 if (!key || value === undefined) {
40 console.error('Usage: vit config set <key> <value>');
41 process.exitCode = 1;
42 return;
43 }
44 const config = loadConfig();
45 config[key] = coerceValue(value);
46 saveConfig(config);
47 return;
48 }
49
50 if (action === 'delete') {
51 if (!key) {
52 console.error('Usage: vit config delete <key>');
53 process.exitCode = 1;
54 return;
55 }
56 const config = loadConfig();
57 delete config[key];
58 saveConfig(config);
59 return;
60 }
61
62 console.error(`Unknown action: ${action}`);
63 process.exitCode = 1;
64 } catch (err) {
65 console.error(formatError(err, { verbose: false }));
66 process.exitCode = 1;
67 }
68 });
69}