Skip to content

Commit c6da44c

Browse files
authored
Merge pull request #6 from phukon/next
Release v4.0.0
2 parents f72b8a5 + 377cc34 commit c6da44c

13 files changed

+395
-370
lines changed

README.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,53 @@
66

77
Simplify PGP key setup and signing commits on Linux and Windows.
88

9-
## 📦 Usage
9+
## 📦 Installation
1010

1111
```bash
12+
# Using npx (recommended)
1213
npx gitkeykit
13-
```
14-
or
15-
```bash
14+
15+
# Or install globally
1616
npm install -g gitkeykit
1717
```
1818

19-
## Features
19+
## 🚀 Usage
20+
21+
### Basic Setup
22+
```bash
23+
# Start the interactive setup
24+
gitkeykit
2025

21-
- **Effortless PGP Key Management**: Create or import PGP keys with ease to secure your Git commits.
22-
- **Cross-Platform Compatibility**: Works seamlessly on both Linux and Windows machines, ensuring a consistent experience across environments.
23-
- **Git and GPG Configuration**: Automatically configure Git and GPG settings for seamless integration with your workflow.
24-
- **Secure Passphrase Entry**: Enhance security with pinentry-mode loopback, ensuring passphrases are entered securely.
25-
- **Fast and Efficient Operation**: Enjoy a lightning-fast CLI tool that gets the job done quickly and efficiently.
26+
# Import existing PGP key
27+
gitkeykit import my_key.txt
28+
29+
# Reset configurations
30+
gitkeykit --reset
31+
32+
# Show version number
33+
gitkeykit --version
34+
35+
# Display help information and available commands
36+
gitkeykit --help
37+
```
2638

39+
### Command Options
40+
- `--reset` Reset Git and GPG configurations
41+
- `--help` Show help information
42+
- `--version` Show version number
43+
- `--import <key_path.txt>` Import and configure PGP key from file
2744

45+
## ✨ Features
2846

29-
#### Options:
30-
`--reset` Reset Git and GPG configurations
47+
- **Interactive Setup**: Guided process for creating or importing PGP keys
48+
- **Cross-Platform**: Works seamlessly on both Linux and Windows
49+
- **Secure Configuration**:
50+
- Automatic Git signing setup
51+
- GPG agent configuration
52+
- Secure passphrase handling
53+
- **Error Handling**: Clear error messages and recovery options
54+
- **Backup & Reset**: Automatic backup of existing configurations with reset capability
3155

32-
#### Commands:
33-
`import <key_path.txt>` Import and set configuration with the provided PGP key
3456

35-
Examples:
36-
`gitkeykit import my_key.txt` Import and set configuration with 'my_key.txt'
37-
`gitkeykit --reset` Reset all configurations`
57+
## 🤝 Contributing
58+
Contributions are welcome! Please feel free to submit a Pull Request.

bin/index.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
#!/usr/bin/env node
22
import arg from "arg";
33
import chalk from "chalk";
4-
import { fileURLToPath } from 'url';
4+
import { fileURLToPath } from "url";
5+
import { dirname, join } from "path";
6+
import { readFileSync } from "fs";
7+
import boxen from "boxen";
58
import { start } from "../src/commands/start";
69
import { reset } from "../src/commands/reset";
710
import { importKey } from "../src/commands/import";
11+
import { GitKeyKitError, GitKeyKitCodes } from "../src/gitkeykitCodes";
812
import createLogger from "../src/utils/logger";
9-
import boxen from 'boxen';
10-
import { GitKeyKitCodes } from "../src/gitkeykitCodes";
11-
import { dirname, join } from "path";
12-
import { readFileSync } from "fs";
1313

14-
process.on("SIGINT", () => process.exit(GitKeyKitCodes.SUCCESS));
15-
process.on("SIGTERM", () => process.exit(GitKeyKitCodes.SUCCESS));
14+
const logger = createLogger("bin");
1615

1716
const __filename = fileURLToPath(import.meta.url);
1817
const __dirname = dirname(__filename);
19-
const packageJson = JSON.parse(
20-
readFileSync(join(__dirname, '../package.json'), 'utf8')
21-
);
18+
const packageJson = JSON.parse(readFileSync(join(__dirname, "../package.json"), "utf8"));
2219
const { version } = packageJson;
2320

24-
const logger = createLogger("bin");
25-
2621
function usage() {
2722
console.log("\n");
28-
console.log(chalk.blueBright(boxen('GitKeyKit - Simplify PGP key🔑 setup and signing commits on Linux and Windows machines.', {padding: 1, borderStyle: 'round'})));
23+
console.log(chalk.blueBright(boxen("GitKeyKit - Simplify PGP key🔑 setup and signing commits on Linux and Windows machines.", { padding: 1, borderStyle: "round" })));
2924
console.log(chalk.whiteBright("Usage: gitkeykit\n"));
3025
console.log(chalk.whiteBright("Options:"));
3126
console.log(chalk.blueBright("--reset\t\t\tReset Git and GPG configurations"));
@@ -43,85 +38,90 @@ function usage() {
4338
console.log("\n");
4439
}
4540

46-
async function handleImport(keyPath: string): Promise<number> {
41+
async function handleImport(keyPath: string): Promise<void> {
4742
try {
4843
await importKey(keyPath);
4944
logger.log(`Imported key from ${keyPath}`);
5045
await start();
51-
return GitKeyKitCodes.SUCCESS;
5246
} catch (error) {
53-
console.error(`Error importing key from ${keyPath}:`, error);
54-
return GitKeyKitCodes.ERR_KEY_IMPORT;
47+
if (error instanceof GitKeyKitError) {
48+
throw error;
49+
}
50+
throw new GitKeyKitError(`Failed to import key from ${keyPath}`, GitKeyKitCodes.KEY_IMPORT_ERROR, error);
5551
}
5652
}
5753

58-
async function handleReset(): Promise<number> {
54+
async function handleReset(): Promise<void> {
5955
try {
60-
reset();
61-
return GitKeyKitCodes.SUCCESS;
62-
} catch (error: any) {
63-
logger.warning((error as Error).message);
64-
console.log();
65-
usage();
66-
return GitKeyKitCodes.ERR_GIT_CONFIG_RESET;
56+
await reset();
57+
} catch (error) {
58+
if (error instanceof GitKeyKitError) {
59+
throw error;
60+
}
61+
throw new GitKeyKitError("Failed to reset configurations", GitKeyKitCodes.GIT_CONFIG_RESET_ERROR, error);
6762
}
6863
}
6964

70-
async function main(): Promise<number> {
65+
async function main(): Promise<void> {
7166
try {
7267
const args = arg({
7368
"--reset": Boolean,
7469
"--help": Boolean,
7570
"--import": String,
76-
"--version": Boolean
71+
"--version": Boolean,
7772
});
7873

7974
logger.debug("Received args", args);
8075

8176
if (Object.keys(args).length === 1) {
8277
await start();
83-
return GitKeyKitCodes.SUCCESS;
78+
return;
8479
}
8580

8681
if (args["--reset"]) {
87-
return handleReset();
82+
await handleReset();
83+
return;
8884
}
8985

9086
if (args["--help"]) {
9187
usage();
92-
return GitKeyKitCodes.SUCCESS;
88+
return;
9389
}
9490

9591
if (args["--import"]) {
9692
const keyPath = args["--import"];
97-
return handleImport(keyPath);
93+
await handleImport(keyPath);
94+
return;
9895
}
9996

10097
if (args["--version"]) {
10198
console.log(`v${version}`);
102-
return GitKeyKitCodes.SUCCESS;
99+
return;
103100
}
104101

105102
usage();
106-
return GitKeyKitCodes.ERR_INVALID_ARGS;
107-
} catch (error: any) {
108-
if (error?.code === 'ARG_UNKNOWN_OPTION') {
103+
} catch (error) {
104+
if (error instanceof arg.ArgError && error?.code === "ARG_UNKNOWN_OPTION") {
109105
logger.error(`Invalid argument: ${error.message}`);
110-
console.log('------');
106+
console.log("------");
111107
usage();
112-
return GitKeyKitCodes.ERR_INVALID_ARGS;
108+
process.exit(1);
109+
}
110+
111+
if (error instanceof GitKeyKitError) {
112+
logger.error(`Error: ${error.message} (${error.code})`);
113+
if (error.details) {
114+
logger.debug("Error details:", error.details);
115+
}
116+
process.exit(1);
113117
}
114-
115-
// Handle any other unexpected errors
116-
logger.error('An unexpected error occurred:', error);
117-
return GitKeyKitCodes.ERR_INVALID_ARGS;
118+
119+
logger.error("An unexpected error occurred:", error);
120+
process.exit(1);
118121
}
119122
}
120123

121-
// Execute and handle exit codes
122-
main()
123-
.then(exitCode => process.exit(exitCode))
124-
.catch(error => {
125-
console.error('Unexpected error:', error);
126-
process.exit(1);
127-
});
124+
main().catch((error) => {
125+
logger.error("Fatal error:", error);
126+
process.exit(1);
127+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gitkeykit",
3-
"version": "3.0.0",
3+
"version": "4.0.0-next.1",
44
"description": "Setup pgp keys and sign commits with ease on Linux and Windows machines.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/commands/import.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1+
import { readFileSync } from "fs";
12
import { execSync } from "child_process";
3+
import { GitKeyKitCodes, GitKeyKitError } from "../gitkeykitCodes";
4+
import createLogger from "../utils/logger";
25

3-
export function importKey(key: string): boolean {
6+
const logger = createLogger("commands:import");
7+
8+
/**
9+
* Imports a GPG key from a file
10+
* @param keyPath Path to the key file
11+
* @throws {GitKeyKitError} If key import fails
12+
*/
13+
export async function importKey(keyPath: string): Promise<void> {
414
try {
5-
execSync(`gpg --import ${key}`, { stdio: "inherit" });
6-
return true; // Indicate success
7-
} catch (error: any) {
8-
throw new Error(`Error importing key: ${(error as Error).message}`);
15+
let keyContent: string;
16+
try {
17+
keyContent = readFileSync(keyPath, "utf-8");
18+
} catch (error) {
19+
throw new GitKeyKitError(`Failed to read key file: ${keyPath}`, GitKeyKitCodes.KEY_IMPORT_ERROR, error);
20+
}
21+
22+
if (!keyContent.includes("-----BEGIN PGP PRIVATE KEY BLOCK-----")) {
23+
throw new GitKeyKitError("Invalid key file format: Missing PGP private key block", GitKeyKitCodes.KEY_IMPORT_ERROR);
24+
}
25+
26+
try {
27+
execSync("gpg --import", {
28+
input: keyContent,
29+
stdio: ["pipe", "inherit", "inherit"],
30+
});
31+
32+
logger.green("GPG key imported successfully");
33+
} catch (error) {
34+
throw new GitKeyKitError("Failed to import GPG key", GitKeyKitCodes.KEY_IMPORT_ERROR, error);
35+
}
36+
} catch (error) {
37+
if (error instanceof GitKeyKitError) {
38+
throw error;
39+
}
40+
throw new GitKeyKitError("Unexpected error during key import", GitKeyKitCodes.KEY_IMPORT_ERROR, error);
941
}
10-
}
42+
}

0 commit comments

Comments
 (0)