mydata-cli
Version:
A CLI tool for interacting with MyData API and managing data. Supports login, data retrieval, and more. Built with Node.js.
84 lines (74 loc) โข 2.25 kB
JavaScript
import { Command } from "commander";
import { getConfig, setConfig, getToken, saveToken } from "../utils/config.js";
import fs from "fs-extra";
import os from "os";
import path from "path";
const config = new Command("config").description("Manage CLI config");
const configPath = path.join(os.homedir(), ".mycli-config.json");
// Set key-value
config
.command("set")
.description("Set a config value")
.argument("<key>", "Config key")
.argument("<value>", "Config value")
.action((key, value) => {
if (key === "token") {
saveToken(value);
} else {
setConfig(key, value);
}
console.log(`โ
Set ${key} = ${value}`);
});
// Get value
config
.command("get")
.description("Get a config value")
.argument("<key>", "Config key")
.action((key) => {
const config = getConfig();
if (key === "token") {
const token = getToken();
if (token) console.log(`๐ token = ${token}`);
else console.log("โ ๏ธ Token not set");
} else {
if (config[key]) console.log(`๐ฆ ${key} = ${config[key]}`);
else console.log(`โ ๏ธ ${key} not set`);
}
});
// Delete key
config
.command("delete")
.description("Delete a config key")
.argument("<key>", "Config key")
.action((key) => {
if (!fs.existsSync(configPath)) {
console.log("โ ๏ธ No config found.");
return;
}
const config = fs.readJsonSync(configPath);
if (config[key]) {
delete config[key];
fs.writeJsonSync(configPath, config, { spaces: 2 });
console.log(`๐๏ธ Deleted ${key}`);
} else {
console.log(`โ ๏ธ ${key} not found`);
}
});
// List all configs
config
.command("list")
.description("List all config values")
.action(() => {
if (!fs.existsSync(configPath)) {
console.log("โ ๏ธ No config found.");
return;
}
const config = fs.readJsonSync(configPath);
console.log("๐งพ Current CLI config:");
Object.entries(config).forEach(([k, v]) => {
const safe = k === "token" ? "****" : v;
console.log(` ${k}: ${safe}`);
});
console.log(`๐ Stored at: ${configPath}`);
});
export default config;