Skip to content

Commit

Permalink
Merge pull request #22 from ytkg/add-config-command
Browse files Browse the repository at this point in the history
Add config command
  • Loading branch information
ytkg authored May 28, 2023
2 parents 2b220f3 + c5a5de9 commit 2a98e2c
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 3 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## Installation
### Install
```bash
$ deno install --allow-env --allow-run --allow-net --allow-read https://deno.land/x/commit_genius/cg.ts
$ deno install --allow-env --allow-run --allow-net --allow-read --allow-write https://deno.land/x/commit_genius/cg.ts
✅ Successfully installed cg

$ cg --version
Expand All @@ -22,11 +22,14 @@ $ cg upgrade
### Uninstall
```bash
$ deno uninstall cg
$ rm -rf ~/.config/commit_genius # if any config file
```

## Usage
```bash
$ export OPENAI_ACCESS_TOKEN=sk-HogehogeXXX
$ # or
$ cg config set api_key sk-HogehogeXXX

$ git add .
$ cg
Expand All @@ -36,5 +39,23 @@ Possible commit message suggestions:
- Refactored function Z for improved performance (機能Zのリファクタリングによるパフォーマンス改善)
```

### Configure
#### Update configuration with a value for the given key
```bash
$ cg config set api_key sk-HogehogeXXX
```

#### Print the value of a given configuration key
```bash
$ cg config get api_key
sk-HogehogeXXX
```

#### Print a list of configuration keys and values
```bash
$ cg config list
api_key = sk-HogehogeXXX
```

## License
The source code is licensed MIT.
17 changes: 17 additions & 0 deletions commands/config.ts
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();
}
}
17 changes: 17 additions & 0 deletions commands/config/get.ts
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();
}
}
19 changes: 19 additions & 0 deletions commands/config/get_test.ts
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
`,
);
});
17 changes: 17 additions & 0 deletions commands/config/list.ts
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();
}
}
19 changes: 19 additions & 0 deletions commands/config/list_test.ts
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
`,
);
});
20 changes: 20 additions & 0 deletions commands/config/set.ts
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();
}
}
19 changes: 19 additions & 0 deletions commands/config/set_test.ts
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
`,
);
});
25 changes: 25 additions & 0 deletions commands/config_test.ts
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
`,
);
});
15 changes: 13 additions & 2 deletions commands/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
} from "../deps.ts";
import { getDiffText } from "../lib/get_diff_text.ts";
import { getCommitMessageSuggestion } from "../lib/get_commit_message_suggestion.ts";
import { loadConfig } from "../lib/load_config.ts";
import { ConfigCommand } from "./config.ts";

export class MainCommand extends Command {
constructor() {
Expand All @@ -22,7 +24,9 @@ export class MainCommand extends Command {
default: "gpt-3.5-turbo" as const,
})
.action(async (options) => {
const apiKey = options.openaiAccessToken || options.openaiApiKey;
const config = await loadConfig();
const apiKey = options.openaiAccessToken || options.openaiApiKey ||
config.api_key;

if (apiKey === undefined) {
throw new ValidationError(
Expand All @@ -39,11 +43,18 @@ export class MainCommand extends Command {

console.log(commitMessageSuggestion);
})
.command("config", new ConfigCommand())
.command(
"upgrade",
new UpgradeCommand({
main: "cg.ts",
args: ["--allow-env", "--allow-run", "--allow-net", "--allow-read"],
args: [
"--allow-env",
"--allow-run",
"--allow-net",
"--allow-read",
"--allow-write",
],
provider: new DenoLandProvider({ name: "commit_genius" }),
}),
)
Expand Down
1 change: 1 addition & 0 deletions commands/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Deno.test("help command", () => {
Commands:
config - Display or change configuration settings for cg.
upgrade - Upgrade cg executable to latest or given version.
Environment variables:
Expand Down
3 changes: 3 additions & 0 deletions deps.ts
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";
20 changes: 20 additions & 0 deletions lib/load_config.ts
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;
};
11 changes: 11 additions & 0 deletions lib/save_config.ts
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));
};

0 comments on commit 2a98e2c

Please sign in to comment.