@@ -8,20 +8,43 @@ const execFileAsync = promisify(execFile);
8
8
9
9
async function getGpgKeyFingerprint ( ) : Promise < string > {
10
10
try {
11
- const { stdout } = await execFileAsync ( 'gpg --list-secret-keys' ) ;
11
+ /*
12
+ output of `gpg --list-secret-keys --with-colons`
13
+ ┌────────────────────────────────────────────────────────────────────────────┐
14
+ │ sec:u:4096:1:A1B2C3D4E5F6G7H8:1700848217:::u:::scESC:::#:::23::0: │
15
+ │ fpr:::::::::1234567890ABCDEF1234567890ABCDEF12345678: │<-- we want this one
16
+ │ grp:::::::::ABCDEF1234567890ABCDEF1234567890ABCDEF12: │
17
+ │ uid:u::::1700850983::0123456789ABCDEF0123456789ABCDEF01234567::Username │
18
+ │ <email>::::::::::0: │
19
+ │ ssb:u:4096:1:FEDCBA9876543210:1732404248:1858548248:::::e:::+:::23: │
20
+ │ fpr:::::::::FEDCBA9876543210FEDCBA9876543210FEDCBA98: │
21
+ │ grp:::::::::9876543210FEDCBA9876543210FEDCBA98765432: │
22
+ │ ssb:u:4096:1:1A2B3C4D5E6F7G8H:1732404191:1858548191:::::s:::+:::23: │
23
+ │ fpr:::::::::ABCD1234EFGH5678IJKL9012MNOP3456QRST7890: │
24
+ │ grp:::::::::WXYZ7890ABCD1234EFGH5678IJKL9012MNOP3456: │
25
+ └────────────────────────────────────────────────────────────────────────────┘
26
+ */
27
+ const { stdout } = await execFileAsync ( 'gpg' , [ '--list-secret-keys' , '--with-colons' ] ) ;
12
28
13
- // Find the longest string that could be a fingerprint
14
29
const lines = stdout . split ( '\n' ) ;
15
- let maxLength = 0 ;
30
+ let isPrimaryKey = false ;
16
31
let keyFingerprint = '' ;
17
-
32
+
18
33
for ( const line of lines ) {
19
- const tokens = line . trim ( ) . split ( / \s + / ) ;
20
- for ( const token of tokens ) {
21
- if ( token . length > maxLength ) {
22
- keyFingerprint = token ;
23
- maxLength = token . length ;
24
- }
34
+ const parts = line . split ( ':' ) ;
35
+ // Mark when we find a primary key (sec)
36
+ if ( parts [ 0 ] === 'sec' ) {
37
+ isPrimaryKey = true ;
38
+ continue ;
39
+ }
40
+ // Get the fingerprint only if it's for the primary key
41
+ if ( isPrimaryKey && parts [ 0 ] === 'fpr' ) {
42
+ keyFingerprint = parts [ 9 ] ;
43
+ break ;
44
+ }
45
+
46
+ if ( parts [ 0 ] === 'ssb' ) {
47
+ isPrimaryKey = false ;
25
48
}
26
49
}
27
50
@@ -47,7 +70,6 @@ async function setGitConfigValue(key: string, value: string): Promise<void> {
47
70
48
71
export async function setGitConfig ( gpgPath : string ) : Promise < GitKeyKitCodes > {
49
72
try {
50
- // Get user input
51
73
const username = await input ( {
52
74
message : 'Enter your name:' ,
53
75
validate : ( value ) => value . length > 0 || 'Name cannot be empty'
@@ -63,10 +85,8 @@ export async function setGitConfig(gpgPath: string): Promise<GitKeyKitCodes> {
63
85
64
86
console . log ( chalk . blue ( 'Setting git config...' ) ) ;
65
87
66
- // Get GPG key fingerprint
67
88
const keyFingerprint = await getGpgKeyFingerprint ( ) ;
68
89
69
- // Configure git settings
70
90
const configs = [
71
91
[ 'user.name' , username ] ,
72
92
[ 'user.email' , email ] ,
0 commit comments