forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-add.ts
More file actions
61 lines (53 loc) · 1.45 KB
/
auth-add.ts
File metadata and controls
61 lines (53 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { defineCommand } from "citty"
import consola from "consola"
import { addAccountEntry } from "./lib/accounts-loader"
import { ensurePaths } from "./lib/paths"
import { state } from "./lib/state"
import { runDeviceFlow } from "./lib/token"
import { detectAccountInfo } from "./services/github/detect-account-info"
interface RunAuthAddOptions {
name?: string
verbose: boolean
}
export async function runAuthAdd(options: RunAuthAddOptions): Promise<void> {
if (options.verbose) {
consola.level = 5
}
await ensurePaths()
consola.info("Starting GitHub Device Flow authentication…")
const token = await runDeviceFlow()
const info = await detectAccountInfo(token)
const name = options.name ?? info.login
state.accountType = info.accountType
consola.info(`Detected GitHub user: ${info.login} (${info.accountType})`)
await addAccountEntry({
name,
github_token: token,
account_type: info.accountType,
})
}
export const authAdd = defineCommand({
meta: {
name: "add",
description: "Add a new GitHub account via Device Flow OAuth",
},
args: {
name: {
alias: "n",
type: "string",
description: "Account name (defaults to GitHub username if not provided)",
},
verbose: {
alias: "v",
type: "boolean",
default: false,
description: "Enable verbose logging",
},
},
run({ args }) {
return runAuthAdd({
name: args.name,
verbose: args.verbose,
})
},
})