|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 | 3 | import { GistDatabase } from './GistDatabase'
|
| 4 | +import { program } from 'commander' |
4 | 5 |
|
5 |
| -if (!process.env.GIST_TOKEN) { |
6 |
| - console.error('GIST_TOKEN is required') |
7 |
| - process.exit(1) |
| 6 | +interface CommanderOptions { |
| 7 | + create: boolean |
| 8 | + description: string |
| 9 | + destroy: string |
| 10 | + public: boolean |
| 11 | + token: string |
8 | 12 | }
|
9 | 13 |
|
10 | 14 | const main = async () => {
|
11 |
| - console.log('Creating database...') |
| 15 | + program |
| 16 | + .name('gist-database') |
| 17 | + .description('Transform gist into a key/value datastore.') |
| 18 | + .option('-c --create', 'Create a new gist database.') |
| 19 | + .option('-p --public', 'Make the gist public.', false) |
| 20 | + .option('-de --description <description>', 'Description of the gist.', '') |
| 21 | + .option( |
| 22 | + '-des --destroy <destroy>', |
| 23 | + 'Destroy a gist database. Provide the gist id of the database.' |
| 24 | + ) |
| 25 | + .requiredOption( |
| 26 | + '-t --token <token>', |
| 27 | + 'Gist token. Required for all operations.' |
| 28 | + ) |
| 29 | + try { |
| 30 | + program.parse(process.argv) |
12 | 31 |
|
13 |
| - const isPublic = process.argv[2] === 'public' |
| 32 | + const options: CommanderOptions = program.opts() |
14 | 33 |
|
15 |
| - const res = await GistDatabase.createDatabaseRoot({ |
16 |
| - token: process.env.GIST_TOKEN, |
17 |
| - public: isPublic, |
18 |
| - description: process.argv[3] |
19 |
| - }) |
| 34 | + if (options.create) { |
| 35 | + console.log('Creating database...') |
| 36 | + const res = await GistDatabase.createDatabaseRoot({ |
| 37 | + token: options.token, |
| 38 | + public: options.public, |
| 39 | + description: options.description |
| 40 | + }) |
| 41 | + console.log('Database created!') |
| 42 | + console.log({ |
| 43 | + id: res.id, |
| 44 | + rawUrl: res.url, |
| 45 | + url: `https://gist.github.com/${res.id}`, |
| 46 | + public: options.public, |
| 47 | + description: options.description |
| 48 | + }) |
| 49 | + } else if (options.destroy) { |
| 50 | + console.log('Destroying database...') |
| 51 | + const db = new GistDatabase({ |
| 52 | + token: options.token, |
| 53 | + id: options.destroy |
| 54 | + }) |
| 55 | + await db.destroy() |
| 56 | + console.log('Database destroyed!') |
| 57 | + } |
| 58 | + } catch (err) { |
| 59 | + console.error(err) |
| 60 | + process.exit(1) |
| 61 | + } |
20 | 62 |
|
21 |
| - console.log('Database created') |
22 |
| - |
23 |
| - console.log({ |
24 |
| - id: res.id, |
25 |
| - rawUrl: res.url, |
26 |
| - url: `https://gist.github.com/${res.id}`, |
27 |
| - public: isPublic, |
28 |
| - description: process.argv[3] |
29 |
| - }) |
| 63 | + process.exit() |
30 | 64 | }
|
31 | 65 |
|
32 | 66 | main()
|
0 commit comments