-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclaude-tw
More file actions
executable file
·317 lines (261 loc) · 9.6 KB
/
claude-tw
File metadata and controls
executable file
·317 lines (261 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/bash
# Claude-TaskWarrior Integration
# Main CLI interface for TaskWarrior-Claude Code integration
# Usage: claude-tw [command] [options]
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="$HOME/.claude-taskwarrior.conf"
SYNC_SCRIPT="$SCRIPT_DIR/sync.js"
SESSION_FILE="/tmp/claude-tw-session.json"
# Default configuration
DEFAULT_PROJECT="claude-session"
DEFAULT_AUTO_SYNC="true"
DEFAULT_BACKUP_DIR="$HOME/.claude-taskwarrior-backups"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Initialize configuration if it doesn't exist
init_config() {
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "# Claude-TaskWarrior Configuration" > "$CONFIG_FILE"
echo "PROJECT=$DEFAULT_PROJECT" >> "$CONFIG_FILE"
echo "AUTO_SYNC=$DEFAULT_AUTO_SYNC" >> "$CONFIG_FILE"
echo "BACKUP_DIR=$DEFAULT_BACKUP_DIR" >> "$CONFIG_FILE"
echo "CREATED=$(date)" >> "$CONFIG_FILE"
mkdir -p "$DEFAULT_BACKUP_DIR"
echo -e "${GREEN}✓${NC} Configuration initialized at $CONFIG_FILE"
fi
# Load configuration
source "$CONFIG_FILE"
}
# Load current session state
load_session() {
if [[ -f "$SESSION_FILE" ]]; then
SESSION_ACTIVE=$(jq -r '.active // false' "$SESSION_FILE" 2>/dev/null || echo "false")
SESSION_PROJECT=$(jq -r '.project // "claude-session"' "$SESSION_FILE" 2>/dev/null || echo "claude-session")
SESSION_START=$(jq -r '.start_time // ""' "$SESSION_FILE" 2>/dev/null || echo "")
else
SESSION_ACTIVE="false"
SESSION_PROJECT="$PROJECT"
SESSION_START=""
fi
}
# Save session state
save_session() {
local active="$1"
local project="$2"
local start_time="$3"
cat > "$SESSION_FILE" << EOF
{
"active": $active,
"project": "$project",
"start_time": "$start_time",
"last_sync": "$(date -Iseconds)"
}
EOF
}
# Start a new session
start_session() {
local project_name="${1:-$PROJECT}"
init_config
echo -e "${BLUE}🚀 Starting Claude-TaskWarrior session...${NC}"
# Create backup
backup_tasks "$project_name"
# Import existing TaskWarrior tasks
if task project:"$project_name" export &>/dev/null; then
local task_count=$(task project:"$project_name" count 2>/dev/null || echo "0")
if [[ "$task_count" -gt 0 ]]; then
echo -e "${YELLOW}📥 Found $task_count existing tasks in TaskWarrior${NC}"
node "$SYNC_SCRIPT" export "$project_name" > "/tmp/claude-import-$$.json"
echo -e "${GREEN}✓${NC} Exported existing tasks for Claude import"
echo -e "${BLUE}💡 Claude can import these with: TodoWrite$(cat /tmp/claude-import-$$.json)${NC}"
fi
fi
# Save session state
save_session "true" "$project_name" "$(date -Iseconds)"
echo -e "${GREEN}✓${NC} Session started for project: $project_name"
echo -e "${BLUE}💡 Use 'claude-tw sync' to sync Claude todos to TaskWarrior${NC}"
echo -e "${BLUE}💡 Use 'claude-tw status' to check session status${NC}"
}
# Sync Claude todos to TaskWarrior
sync_todos() {
load_session
if [[ "$SESSION_ACTIVE" != "true" ]]; then
echo -e "${RED}❌ No active session. Start one with: claude-tw start${NC}"
exit 1
fi
local todos_json="$1"
if [[ -z "$todos_json" ]]; then
echo -e "${YELLOW}⚠️ No todos provided. Usage: claude-tw sync '[{\"id\":\"...\"}]'${NC}"
exit 1
fi
echo -e "${BLUE}🔄 Syncing todos to TaskWarrior...${NC}"
# Backup before sync
backup_tasks "$SESSION_PROJECT"
# Clear existing project tasks and import new ones
if task project:"$SESSION_PROJECT" count &>/dev/null && [[ $(task project:"$SESSION_PROJECT" count) -gt 0 ]]; then
echo "yes" | task project:"$SESSION_PROJECT" delete &>/dev/null
fi
# Import new todos
node "$SYNC_SCRIPT" import "$SESSION_PROJECT" "$todos_json"
local task_count=$(task project:"$SESSION_PROJECT" count 2>/dev/null || echo "0")
echo -e "${GREEN}✓${NC} Synced $task_count tasks to TaskWarrior project: $SESSION_PROJECT"
# Update session
save_session "true" "$SESSION_PROJECT" "$SESSION_START"
}
# Export TaskWarrior tasks for Claude
export_todos() {
load_session
if [[ "$SESSION_ACTIVE" != "true" ]]; then
echo -e "${RED}❌ No active session. Start one with: claude-tw start${NC}"
exit 1
fi
local task_count=$(task project:"$SESSION_PROJECT" count 2>/dev/null || echo "0")
if [[ "$task_count" -eq 0 ]]; then
echo -e "${YELLOW}⚠️ No tasks found in project: $SESSION_PROJECT${NC}"
echo "[]"
exit 0
fi
echo -e "${BLUE}📤 Exporting $task_count tasks from TaskWarrior...${NC}" >&2
node "$SYNC_SCRIPT" export "$SESSION_PROJECT"
}
# Show session status
show_status() {
load_session
echo -e "${BLUE}📊 Claude-TaskWarrior Status${NC}"
echo "=================================="
if [[ "$SESSION_ACTIVE" == "true" ]]; then
echo -e "Status: ${GREEN}Active${NC}"
echo "Project: $SESSION_PROJECT"
echo "Started: $SESSION_START"
local task_count=$(task project:"$SESSION_PROJECT" count 2>/dev/null || echo "0")
local pending_count=$(task project:"$SESSION_PROJECT" status:pending count 2>/dev/null || echo "0")
local completed_count=$(task project:"$SESSION_PROJECT" status:completed count 2>/dev/null || echo "0")
echo "Tasks: $task_count total, $pending_count pending, $completed_count completed"
if [[ "$task_count" -gt 0 ]]; then
echo ""
echo -e "${BLUE}Recent tasks:${NC}"
task project:"$SESSION_PROJECT" limit:5 2>/dev/null || echo "No tasks found"
fi
else
echo -e "Status: ${YELLOW}Inactive${NC}"
echo "Use 'claude-tw start' to begin a new session"
fi
}
# Create backup of current tasks
backup_tasks() {
local project_name="$1"
local backup_file="$BACKUP_DIR/backup-$(date +%Y%m%d-%H%M%S).json"
if task project:"$project_name" export &>/dev/null; then
task project:"$project_name" export > "$backup_file" 2>/dev/null
echo -e "${GREEN}✓${NC} Backup created: $backup_file"
fi
}
# End current session
end_session() {
load_session
if [[ "$SESSION_ACTIVE" != "true" ]]; then
echo -e "${YELLOW}⚠️ No active session to end${NC}"
exit 0
fi
echo -e "${BLUE}🏁 Ending Claude-TaskWarrior session...${NC}"
# Final backup
backup_tasks "$SESSION_PROJECT"
# Show final summary
local task_count=$(task project:"$SESSION_PROJECT" count 2>/dev/null || echo "0")
local completed_count=$(task project:"$SESSION_PROJECT" status:completed count 2>/dev/null || echo "0")
echo -e "${GREEN}✓${NC} Session completed"
echo "Final stats: $completed_count of $task_count tasks completed"
# Clear session
rm -f "$SESSION_FILE"
}
# Show help
show_help() {
echo "Claude-TaskWarrior Integration CLI"
echo ""
echo "USAGE:"
echo " claude-tw <command> [options]"
echo ""
echo "COMMANDS:"
echo " start [project] Start new session (default project: claude-session)"
echo " sync '<json>' Sync Claude todos to TaskWarrior"
echo " export Export TaskWarrior tasks for Claude import"
echo " status Show current session status"
echo " end End current session"
echo " backup Create manual backup"
echo " config Show configuration"
echo " help Show this help"
echo ""
echo "EXAMPLES:"
echo " claude-tw start # Start session with default project"
echo " claude-tw start chrome-launch # Start session with custom project"
echo " claude-tw sync '[{\"id\":\"1\",\"content\":\"Task\"}]' # Sync todos"
echo " claude-tw export # Export for Claude"
echo ""
echo "FILES:"
echo " Config: $CONFIG_FILE"
echo " Session: $SESSION_FILE"
echo " Backups: $BACKUP_DIR"
}
# Main command dispatcher
main() {
case "$1" in
start)
start_session "$2"
;;
sync)
sync_todos "$2"
;;
export)
export_todos
;;
status)
show_status
;;
end)
end_session
;;
backup)
load_session
backup_tasks "${SESSION_PROJECT:-$DEFAULT_PROJECT}"
;;
config)
init_config
echo "Configuration file: $CONFIG_FILE"
cat "$CONFIG_FILE"
;;
help|--help|-h)
show_help
;;
*)
echo -e "${RED}❌ Unknown command: $1${NC}"
echo "Use 'claude-tw help' for usage information"
exit 1
;;
esac
}
# Check dependencies
check_dependencies() {
local missing_deps=()
if ! command -v task &> /dev/null; then
missing_deps+=("taskwarrior")
fi
if ! command -v node &> /dev/null; then
missing_deps+=("nodejs")
fi
if ! command -v jq &> /dev/null; then
missing_deps+=("jq")
fi
if [[ ${#missing_deps[@]} -gt 0 ]]; then
echo -e "${RED}❌ Missing dependencies: ${missing_deps[*]}${NC}"
echo "Install them with: sudo apt install ${missing_deps[*]}"
exit 1
fi
}
# Initialize
check_dependencies
init_config
# Run main function
main "$@"