Files
feeddeck/supabase/functions/_cmd/tools/tools.ts
Rico Berger 1cb58e1e0f [core] Refactor Tools and add get-feed Tool (#105)
This commit refactors the existing tools, by moving the tools logic to a
new `tools.ts` file, so that the main `cmd.ts` file remains clear.

Besides that we also add a new tool `get-feed` which can be used to run
the `getFeed` function from the command line. The function is called
with a source and returns the generated source and items, as they are
saved in the database by the `add-source-v1` Supabase edge function.
2023-12-17 18:04:53 +01:00

81 lines
2.7 KiB
TypeScript

import { createClient } from '@supabase/supabase-js';
import { generateKey } from '../../_shared/utils/encrypt.ts';
import { log } from '../../_shared/utils/log.ts';
import { generateAppleSecretKey } from './apple-secret-key.ts';
import { getFeed } from '../../_shared/feed/feed.ts';
export const runTools = async (args: string[]): Promise<void> => {
/**
* The "tools generate-key" command can be invoked via the following command:
* deno run --no-lock --allow-net --allow-env --import-map=./supabase/functions/import_map.json ./supabase/functions/_cmd/cmd.ts tools generate-key
*/
if (
args.length === 2 && args[0] === 'tools' &&
args[1] === 'generate-key'
) {
const data = await generateKey();
log('info', 'Encryption key was generated', {
key: data.rawKey,
iv: data.iv,
});
return;
}
/**
* The "tools generate-apple-secret-key" command can be invoked via the
* following command:
* deno run --no-lock --allow-env --allow-read --import-map=./supabase/functions/import_map.json ./supabase/functions/_cmd/cmd.ts tools generate-apple-secret-key <KEY-ID> <TEAM-ID> <SERVICE-ID> <FILE>
*/
if (
args.length === 6 && args[0] === 'tools' &&
args[1] === 'generate-apple-secret-key'
) {
const data = await generateAppleSecretKey(
args[2],
args[3],
args[4],
args[5],
);
log('info', 'Encryption key was generated', {
kid: data.kid,
exp: new Date(data.exp * 1000).toString(),
jwt: data.jwt,
});
return;
}
/**
* The "tools get-feed" command can be invoked via the following command:
* deno run --no-lock --allow-env --allow-read --allow-net --import-map=./supabase/functions/import_map.json ./supabase/functions/_cmd/cmd.ts tools get-feed <SOURCE>
*
* The command gets a source and the items for a provided source. The provided
* source must contain the type and options to get a feed. All other required
* properties for a source are added by the function.
*
* Example:
* deno run --no-lock --allow-env --allow-read --allow-net --import-map=./supabase/functions/import_map.json ./supabase/functions/_cmd/cmd.ts tools get-feed '{"type": "reddit", "options": {"reddit": "/r/kubernetes"}}'
*/
if (
args.length === 3 && args[0] === 'tools' &&
args[1] === 'get-feed'
) {
const { source, items } = await getFeed(
createClient('http://localhost:54321', 'test123'),
undefined,
{ id: '', tier: 'free', createdAt: 0, updatedAt: 0 },
{
...JSON.parse(args[2]),
id: '',
columnId: 'mycolumn',
userId: 'myuser',
},
);
log('info', 'Add source', {
source: source,
items: items,
});
return;
}
};