@cloud-cli/env
Version:
Storage for app environment variables
76 lines (75 loc) • 1.79 kB
JavaScript
import { getStorage } from '@cloud-cli/cli';
const appNotSpecifiedError = new Error('App not specified');
const keyNotSpecifiedError = new Error('Key not specified');
const { get, set, remove, getAll } = getStorage('env');
const readName = (options) => {
options.app = options.app || options.name || options._?.[0];
};
function show(options) {
readName(options);
const { app } = options;
if (!app) {
throw appNotSpecifiedError;
}
return list({ app });
}
function list(filters = {}) {
let all = getAll();
if (filters.app) {
all = all.filter((e) => e.app === filters.app);
}
if (filters.key) {
all = all.filter((e) => e.key === filters.key);
}
return all;
}
function apps() {
const rows = getAll();
const apps = rows.map((entry) => entry.app);
return [...new Set(apps)];
}
function setVar(options) {
readName(options);
const { key, value, app } = options;
if (!app) {
throw appNotSpecifiedError;
}
if (!key) {
throw keyNotSpecifiedError;
}
const entry = { app, key, value };
set(computeId(app, key), entry);
return entry;
}
function removeVar(options) {
readName(options);
const { app, key } = options;
const found = getVar({ app, key });
if (found) {
remove(computeId(app, key));
return true;
}
return false;
}
function getVar(options) {
readName(options);
const { app, key } = options;
if (!app) {
throw appNotSpecifiedError;
}
if (!key) {
throw keyNotSpecifiedError;
}
return get(computeId(app, key));
}
function computeId(app, key) {
return `${app}.${key}`;
}
export default {
get: getVar,
set: setVar,
remove: removeVar,
show,
apps,
list,
};