-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #6 from phukon/next
Release v4.0.0
- Loading branch information
Showing
13 changed files
with
395 additions
and
370 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
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,42 @@ | ||
import { readFileSync } from "fs"; | ||
import { execSync } from "child_process"; | ||
import { GitKeyKitCodes, GitKeyKitError } from "../gitkeykitCodes"; | ||
import createLogger from "../utils/logger"; | ||
|
||
export function importKey(key: string): boolean { | ||
const logger = createLogger("commands:import"); | ||
|
||
/** | ||
* Imports a GPG key from a file | ||
* @param keyPath Path to the key file | ||
* @throws {GitKeyKitError} If key import fails | ||
*/ | ||
export async function importKey(keyPath: string): Promise<void> { | ||
try { | ||
execSync(`gpg --import ${key}`, { stdio: "inherit" }); | ||
return true; // Indicate success | ||
} catch (error: any) { | ||
throw new Error(`Error importing key: ${(error as Error).message}`); | ||
let keyContent: string; | ||
try { | ||
keyContent = readFileSync(keyPath, "utf-8"); | ||
} catch (error) { | ||
throw new GitKeyKitError(`Failed to read key file: ${keyPath}`, GitKeyKitCodes.KEY_IMPORT_ERROR, error); | ||
} | ||
|
||
if (!keyContent.includes("-----BEGIN PGP PRIVATE KEY BLOCK-----")) { | ||
throw new GitKeyKitError("Invalid key file format: Missing PGP private key block", GitKeyKitCodes.KEY_IMPORT_ERROR); | ||
} | ||
|
||
try { | ||
execSync("gpg --import", { | ||
input: keyContent, | ||
stdio: ["pipe", "inherit", "inherit"], | ||
}); | ||
|
||
logger.green("GPG key imported successfully"); | ||
} catch (error) { | ||
throw new GitKeyKitError("Failed to import GPG key", GitKeyKitCodes.KEY_IMPORT_ERROR, error); | ||
} | ||
} catch (error) { | ||
if (error instanceof GitKeyKitError) { | ||
throw error; | ||
} | ||
throw new GitKeyKitError("Unexpected error during key import", GitKeyKitCodes.KEY_IMPORT_ERROR, error); | ||
} | ||
} | ||
} |
Oops, something went wrong.