@graphenedata/cli 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli.ts CHANGED
@@ -5,17 +5,17 @@ import {printDiagnostics, printTable} from './printer.ts'
5
5
  import {analyze, getDiagnostics, loadWorkspace, toSql, type Query} from '../lang/core.ts'
6
6
  import fs from 'fs-extra'
7
7
  import path from 'path'
8
- import {loadConfig} from '../lang/config.ts'
8
+ import dotenv from 'dotenv'
9
+ import {config, loadConfig} from '../lang/config.ts'
9
10
  import {runServeInBackground, stopGrapheneIfRunning} from './background.ts'
10
- import {getConnection} from './connections/index.ts'
11
11
  import {check} from './check.ts'
12
+ import {getConnection, runQuery} from './connections/index.ts'
13
+ import {loginPkce} from './auth.ts'
12
14
 
15
+ dotenv.config({quiet: true})
13
16
  const program = new Command()
14
17
 
15
- program
16
- .name('graphene')
17
- .description('Graphene CLI')
18
- .version('1.0.0')
18
+ program.name('graphene').description('Graphene CLI').version('1.0.0')
19
19
 
20
20
  program.hook('preAction', async () => {
21
21
  if (process.env.CLI_DELAY) { // useful if you want to attach a debugger
@@ -46,31 +46,60 @@ program
46
46
  let queries = analyze(gsql)
47
47
  if (!validQuery(queries)) return
48
48
  let sql = toSql(queries[0])
49
- let connection = await getConnection()
50
- let res = await connection.runQuery(sql)
49
+ let res = await runQuery(sql)
51
50
  printTable(res.rows)
52
51
  })
53
52
 
53
+ program.command('schema')
54
+ .description('Inspect database tables or describe a table')
55
+ .argument('[schema | table]', 'Optional schema or table name to describe')
56
+ .action(async (tableArg: string) => {
57
+ let connection = await getConnection()
58
+ let datasets = await connection.listDatasets()
59
+
60
+ // if there's no arg and more than one dataset, just list the datasets
61
+ if (!tableArg && datasets.length > 1) {
62
+ return console.log(`Datasets available:\n${datasets.join('\n')}`)
63
+ }
64
+
65
+ // figure out if you're wanting to list tables in a schema/dataset
66
+ let dsToList: string | null = null
67
+ if (datasets.includes(tableArg)) dsToList = tableArg // you gave the name of a dataset
68
+ else if (!tableArg && datasets.length == 1) dsToList = datasets[0] // only one dataset, and no args
69
+ else if (!tableArg && config.namespace) dsToList = config.namespace // default namespace configured
70
+ else if (!tableArg && config.dialect == 'duckdb') dsToList = '<default>'
71
+
72
+ if (dsToList) {
73
+ let tables = await connection.listTables(dsToList)
74
+ return console.log(`Tables${dsToList ? ` in ${dsToList}` : ''}:\n${tables.join('\n')}`)
75
+ }
76
+
77
+ // otherwise, assume you're wanting to see tables
78
+ let cols = await connection.describeTable(tableArg)
79
+ if (!cols.length) return console.log(`Table ${tableArg} not found`)
80
+ console.log(`table ${tableArg} (`)
81
+ cols.forEach(col => console.log(` ${col.name} ${col.dataType}`))
82
+ console.log(')')
83
+ })
84
+
54
85
  program
55
86
  .command('serve')
56
87
  .description('Run the local server')
57
- .option('--fg', 'Run the server in the foreground')
58
- .action(async (options: {fg?: boolean}) => {
59
- if (options.fg || process.env.DEBUG) {
60
- let mod = await import('./serve2.ts') // load dynamically, so we're not pulling in a bunch of deps we might not need
61
- await mod.serve2()
62
- } else {
88
+ .option('--bg', 'Run the server in the background')
89
+ .action(async (options: {bg?: boolean}) => {
90
+ await stopGrapheneIfRunning()
91
+ if (options.bg) {
63
92
  await runServeInBackground()
64
93
  process.exit(0)
94
+ } else {
95
+ let mod = await import('./serve2.ts') // load dynamically, so we're not pulling in a bunch of deps we might not need
96
+ await mod.serve2()
65
97
  }
66
98
  })
67
99
 
68
- program
69
- .command('stop')
100
+ program.command('stop')
70
101
  .description('Stop the local server')
71
- .action(async () => {
72
- await stopGrapheneIfRunning(process.cwd())
73
- })
102
+ .action(async () => { await stopGrapheneIfRunning() })
74
103
 
75
104
  program
76
105
  .command('check')
@@ -82,6 +111,14 @@ program
82
111
  process.exit(res ? 0 : 1) // import to call `exit`, bc if we started the server in the background, just returning won't actually exit the process.
83
112
  })
84
113
 
114
+ program.command('login')
115
+ .description('Log in to Graphene Cloud')
116
+ .action(async () => {
117
+ await loginPkce()
118
+ console.log('Successfully logged in')
119
+ process.exit(0)
120
+ })
121
+
85
122
  program.parse(process.argv)
86
123
 
87
124
  async function readInput (arg): Promise<string> {