-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
58 lines (48 loc) · 1.81 KB
/
config.ts
File metadata and controls
58 lines (48 loc) · 1.81 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
export const ANTHROPIC_KEY = "anthropic_api_key"
export const OPENAI_KEY = "openai_key"
export function readConfig(configKey?: string) {
const configPath = createConfigPath()
const config = JSON.parse(Deno.readTextFileSync(configPath))
return configKey ? config[configKey] : config
}
function writeConfig(config: Record<string, string>) {
const configPath = createConfigPath()
Deno.writeTextFileSync(configPath, JSON.stringify(config, null, 2))
}
function resetConfig() {
const configPath = createConfigPath()
Deno.removeSync(configPath)
}
function createConfigPath() {
const homeDir = Deno.env.get("HOME") || Deno.env.get("USERPROFILE")
const configPath = `${homeDir}/.curlgen/config.json`
return configPath
}
export function handleConfig(flags: Record<string, string | boolean | unknown> & { _: (string | number)[] }) {
if (globalThis.isVerbose) {
console.log(flags)
}
const subSubCommand = flags._[1]?.toString()
if (subSubCommand === "read") {
const configKey = (flags._[2]?.toString() || flags.configKey) as string
const config = readConfig(configKey)
console.log(config)
} else if (subSubCommand === "write") {
const configKey = (flags._[2]?.toString() || flags.configKey) as string
const configValue = (flags._[3]?.toString() || flags.configValue) as string
const config = readConfig()
config[configKey] = configValue
writeConfig(config)
} else if (subSubCommand === "reset") {
resetConfig()
}
}
export function getConfigHelp(): string {
return `
Usage: curlgen config <subcommand> [configKey] [configValue]
Subcommands:
read [configKey] Read configuration
write [configKey] [configValue] Write configuration
reset Reset configuration
`
}