let { getUrl, getUrls, addUrl, removeUrl } = await lib('urls');
let urlsChoices = () =>
getUrls().map(({ name, url, id }) => ({
name: `${name} (${url})`,
value: id,
}));
let useUrl = async () => {
let id = await arg('Use url:', urlsChoices());
let { url } = await getUrl(id);
const matches = url.match(/\{(.*?)\}/g);
if (matches && matches.length) {
const params = {};
for (let key of matches) {
key.replace('{', '').replace('}', '');
params[key] = await arg(`Enter value for param ${key}`);
}
url = Object.entries(params).reduce((acc, [key, value]) => {
return acc.replace(new RegExp(`${key}`, 'g'), value);
}, url);
}
exec(`open ${url}`);
};
let add = async () => {
const name = await arg('Enter a URL name:');
const url = await arg('Enter a URL address:');
addUrl(name, url);
await add();
};
let remove = async () => {
let id = await arg('Remove url:', urlsChoices());
removeUrl(id);
await remove();
};
onTab('Use', useUrl);
onTab('Add', add);
onTab('Remove', remove);