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