-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from ytkg/add-config-command
Add config command
- Loading branch information
Showing
14 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Command, HelpCommand } from "../deps.ts"; | ||
import { SetCommand } from "./config/set.ts"; | ||
import { GetCommand } from "./config/get.ts"; | ||
import { ListCommand } from "./config/list.ts"; | ||
|
||
export class ConfigCommand extends Command { | ||
constructor() { | ||
super(); | ||
this.description("Display or change configuration settings for cg.") | ||
.default("help") | ||
.command("help", new HelpCommand().hidden()) | ||
.command("set", new SetCommand()) | ||
.command("get", new GetCommand()) | ||
.command("list", new ListCommand()) | ||
.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Command } from "../../deps.ts"; | ||
import { loadConfig } from "../../lib/load_config.ts"; | ||
|
||
export class GetCommand extends Command { | ||
constructor() { | ||
super(); | ||
|
||
this.description("Print the value of a given configuration key") | ||
.arguments("<key:string>") | ||
.action(async (_, key) => { | ||
const config = await loadConfig(); | ||
|
||
console.log(config[key]); | ||
}) | ||
.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { assertEquals } from "../../dev_deps.ts"; | ||
import { GetCommand } from "./get.ts"; | ||
|
||
const command = new GetCommand().help({ colors: false }).helpOption(false); | ||
|
||
Deno.test("help command", () => { | ||
const output = command.getHelp(); | ||
|
||
assertEquals( | ||
output, | ||
` | ||
Usage: COMMAND <key> | ||
Description: | ||
Print the value of a given configuration key | ||
`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Command } from "../../deps.ts"; | ||
import { loadConfig } from "../../lib/load_config.ts"; | ||
|
||
export class ListCommand extends Command { | ||
constructor() { | ||
super(); | ||
this.description("Print a list of configuration keys and values") | ||
.action(async () => { | ||
const config = await loadConfig(); | ||
|
||
for (const key in config) { | ||
console.log(`${key} = ${config[key]}`); | ||
} | ||
}) | ||
.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { assertEquals } from "../../dev_deps.ts"; | ||
import { ListCommand } from "./list.ts"; | ||
|
||
const command = new ListCommand().help({ colors: false }).helpOption(false); | ||
|
||
Deno.test("help command", () => { | ||
const output = command.getHelp(); | ||
|
||
assertEquals( | ||
output, | ||
` | ||
Usage: COMMAND | ||
Description: | ||
Print a list of configuration keys and values | ||
`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Command, EnumType } from "../../deps.ts"; | ||
import { loadConfig } from "../../lib/load_config.ts"; | ||
import { saveConfig } from "../../lib/save_config.ts"; | ||
|
||
export class SetCommand extends Command { | ||
constructor() { | ||
super(); | ||
this.description("Update configuration with a value for the given key") | ||
.type("config", new EnumType(["api_key"])) | ||
.arguments("<key:config> <value:string>") | ||
.action(async (_, key, value) => { | ||
const config = await loadConfig(); | ||
|
||
config[key] = value; | ||
|
||
await saveConfig(config); | ||
}) | ||
.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { assertEquals } from "../../dev_deps.ts"; | ||
import { SetCommand } from "./set.ts"; | ||
|
||
const command = new SetCommand().help({ colors: false }).helpOption(false); | ||
|
||
Deno.test("help command", () => { | ||
const output = command.getHelp(); | ||
|
||
assertEquals( | ||
output, | ||
` | ||
Usage: COMMAND <key> <value> | ||
Description: | ||
Update configuration with a value for the given key | ||
`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { assertEquals } from "../dev_deps.ts"; | ||
import { ConfigCommand } from "./config.ts"; | ||
|
||
const command = new ConfigCommand().help({ colors: false }).helpOption(false); | ||
|
||
Deno.test("help command", () => { | ||
const output = command.getHelp(); | ||
|
||
assertEquals( | ||
output, | ||
` | ||
Usage: COMMAND | ||
Description: | ||
Display or change configuration settings for cg. | ||
Commands: | ||
set <key> <value> - Update configuration with a value for the given key | ||
get <key> - Print the value of a given configuration key | ||
list - Print a list of configuration keys and values | ||
`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
export { | ||
Command, | ||
EnumType, | ||
HelpCommand, | ||
ValidationError, | ||
} from "https://deno.land/x/[email protected]/command/mod.ts"; | ||
export { | ||
DenoLandProvider, | ||
UpgradeCommand, | ||
} from "https://deno.land/x/[email protected]/command/upgrade/mod.ts"; | ||
export { OpenAI } from "https://deno.land/x/[email protected]/mod.ts"; | ||
export { parse, stringify } from "https://deno.land/[email protected]/toml/mod.ts"; | ||
export { exists } from "https://deno.land/[email protected]/fs/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { exists, parse } from "../deps.ts"; | ||
|
||
type Config = { | ||
api_key?: string; | ||
[key: string]: string | undefined; | ||
}; | ||
|
||
export const loadConfig = async (): Promise<Config> => { | ||
const filePath = `${Deno.env.get("HOME")}/.config/commit_genius/config.toml`; | ||
const fileExists = await exists(filePath); | ||
|
||
if (!fileExists) { | ||
return {}; | ||
} | ||
|
||
const file = await Deno.readTextFile(filePath); | ||
const config = parse(file) as Config; | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { stringify } from "../deps.ts"; | ||
|
||
export const saveConfig = async ( | ||
config: Record<string, unknown>, | ||
): Promise<void> => { | ||
const dirPath = `${Deno.env.get("HOME")}/.config/commit_genius`; | ||
const filePath = `${dirPath}/config.toml`; | ||
|
||
await Deno.mkdir(dirPath, { recursive: true }); | ||
await Deno.writeTextFile(filePath, stringify(config)); | ||
}; |