tenseleyflow/ndotfiles / e2b6586

Browse files

gpg reset script

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
e2b658617a13adc44e5a45db5ad26bb52f3a60c5
Parents
666b913
Tree
ccb2936

1 changed file

StatusFile+-
A Script/gpg-reset 161 0
Script/gpg-resetadded
@@ -0,0 +1,161 @@
1
+#!/bin/bash
2
+
3
+# gpg-reset - Reset GPG agent when it gets stuck
4
+# Usage: gpg-reset [options]
5
+
6
+set -e
7
+
8
+# Colors for output
9
+RED='\033[0;31m'
10
+GREEN='\033[0;32m'
11
+YELLOW='\033[1;33m'
12
+BLUE='\033[0;34m'
13
+NC='\033[0m' # No Color
14
+
15
+# Default options
16
+VERBOSE=false
17
+TEST_AFTER=true
18
+
19
+# Help function
20
+show_help() {
21
+    cat << EOF
22
+GPG Agent Reset Script
23
+
24
+Usage: gpg-reset [OPTIONS]
25
+
26
+Reset the GPG agent when it gets stuck or fails to sign.
27
+
28
+OPTIONS:
29
+    -h, --help      Show this help message
30
+    -v, --verbose   Show verbose output
31
+    -q, --quiet     Don't test after reset
32
+    -t, --test      Test GPG after reset (default)
33
+
34
+EXAMPLES:
35
+    gpg-reset               # Basic reset with test
36
+    gpg-reset -v            # Verbose reset
37
+    gpg-reset -q            # Quiet reset (no test)
38
+
39
+EOF
40
+}
41
+
42
+# Parse command line arguments
43
+while [[ $# -gt 0 ]]; do
44
+    case $1 in
45
+        -h|--help)
46
+            show_help
47
+            exit 0
48
+            ;;
49
+        -v|--verbose)
50
+            VERBOSE=true
51
+            shift
52
+            ;;
53
+        -q|--quiet)
54
+            TEST_AFTER=false
55
+            shift
56
+            ;;
57
+        -t|--test)
58
+            TEST_AFTER=true
59
+            shift
60
+            ;;
61
+        *)
62
+            echo -e "${RED}Error: Unknown option $1${NC}"
63
+            echo "Use -h or --help for usage information"
64
+            exit 1
65
+            ;;
66
+    esac
67
+done
68
+
69
+# Verbose output function
70
+verbose() {
71
+    if [[ "$VERBOSE" == true ]]; then
72
+        echo -e "${BLUE}[VERBOSE]${NC} $1"
73
+    fi
74
+}
75
+
76
+# Main reset function
77
+reset_gpg_agent() {
78
+    echo -e "${YELLOW}🔄 Resetting GPG agent...${NC}"
79
+    
80
+    # Step 1: Kill existing GPG processes
81
+    verbose "Killing existing GPG processes..."
82
+    pkill -f gpg-agent 2>/dev/null || true
83
+    killall gpg-agent 2>/dev/null || true
84
+    
85
+    # Step 2: Clear environment variables
86
+    verbose "Clearing GPG environment variables..."
87
+    unset GPG_AGENT_INFO 2>/dev/null || true
88
+    
89
+    # Step 3: Set TTY
90
+    verbose "Setting GPG_TTY..."
91
+    export GPG_TTY=$(tty)
92
+    
93
+    # Step 4: Kill agent via gpgconf
94
+    verbose "Killing agent via gpgconf..."
95
+    gpgconf --kill gpg-agent 2>/dev/null || true
96
+    
97
+    # Step 5: Launch new agent
98
+    verbose "Launching new GPG agent..."
99
+    gpgconf --launch gpg-agent
100
+    
101
+    echo -e "${GREEN}✅ GPG agent reset complete${NC}"
102
+}
103
+
104
+# Test function
105
+test_gpg() {
106
+    echo -e "${YELLOW}🧪 Testing GPG functionality...${NC}"
107
+    
108
+    # Test if GPG agent is responding
109
+    if gpg-connect-agent /bye >/dev/null 2>&1; then
110
+        verbose "GPG agent is responding"
111
+    else
112
+        echo -e "${RED}❌ GPG agent not responding${NC}"
113
+        return 1
114
+    fi
115
+    
116
+    # Test basic GPG operation
117
+    if echo "test" | gpg --clearsign >/dev/null 2>&1; then
118
+        echo -e "${GREEN}✅ GPG is working correctly${NC}"
119
+        return 0
120
+    else
121
+        echo -e "${YELLOW}⚠️  GPG agent reset but signing may still have issues${NC}"
122
+        echo -e "${YELLOW}   Try running: export GPG_TTY=\$(tty)${NC}"
123
+        return 1
124
+    fi
125
+}
126
+
127
+# Check if GPG is installed
128
+check_gpg() {
129
+    if ! command -v gpg >/dev/null 2>&1; then
130
+        echo -e "${RED}❌ Error: GPG is not installed${NC}"
131
+        exit 1
132
+    fi
133
+    
134
+    if ! command -v gpgconf >/dev/null 2>&1; then
135
+        echo -e "${RED}❌ Error: gpgconf is not available${NC}"
136
+        exit 1
137
+    fi
138
+}
139
+
140
+# Main execution
141
+main() {
142
+    verbose "Starting GPG agent reset script"
143
+    
144
+    # Check prerequisites
145
+    check_gpg
146
+    
147
+    # Reset the agent
148
+    reset_gpg_agent
149
+    
150
+    # Test if requested
151
+    if [[ "$TEST_AFTER" == true ]]; then
152
+        echo ""
153
+        test_gpg
154
+    fi
155
+    
156
+    echo ""
157
+    echo -e "${GREEN}🎉 Done! Try your git commit again.${NC}"
158
+}
159
+
160
+# Run main function
161
+main "$@"