spikes 0.4.0

Drop-in feedback collection for static HTML mockups
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
mod auth;
mod commands;
mod config;
mod error;
mod output;
mod spike;
mod storage;

use clap::{Parser, Subcommand};
use commands::delete::DeleteOptions;
use commands::deploy::DeployOptions;
use commands::export::ExportFormat;
use commands::inject::InjectOptions;
use commands::list::ListOptions;
use commands::login::LoginOptions;
use commands::pull::PullOptions;
use commands::push::PushOptions;
use commands::resolve::ResolveOptions;
use commands::serve::ServeOptions;
use commands::share::ShareOptions;
use commands::shares::SharesOptions;
use commands::unshare::UnshareOptions;
use commands::usage::UsageOptions;

#[derive(Parser)]
#[command(name = "spikes")]
#[command(about = "Feedback collection for static mockups", long_about = None)]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Port for dev server (magic mode)
    #[arg(long, short, default_value = "3847", global = true)]
    port: u16,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize a .spikes/ directory
    Init {
        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Self-host instead of using hosted spikes.sh (non-interactive opt-out)
        #[arg(long)]
        self_host: bool,
    },

    /// List all spikes
    List {
        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Filter by page name
        #[arg(long)]
        page: Option<String>,

        /// Filter by reviewer name
        #[arg(long)]
        reviewer: Option<String>,

        /// Filter by rating (love, like, meh, no)
        #[arg(long)]
        rating: Option<String>,

        /// Show only unresolved spikes
        #[arg(long)]
        unresolved: bool,
    },

    /// Show a single spike by ID
    Show {
        /// Spike ID (or prefix)
        id: String,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Export all spikes
    Export {
        /// Output format: json, csv, or jsonl
        #[arg(long, short, default_value = "json")]
        format: String,
    },

    /// Show elements with most feedback
    Hotspots {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// List all reviewers who left feedback
    Reviewers {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Add widget script tag to HTML files
    Inject {
        /// Directory containing HTML files
        directory: String,

        /// Remove widget script tags instead of adding
        #[arg(long)]
        remove: bool,

        /// URL for widget script (default: /spikes.js for local serve)
        #[arg(long)]
        widget_url: Option<String>,

        /// Endpoint URL for the widget to POST feedback to (overrides config)
        #[arg(long)]
        endpoint: Option<String>,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Start local development server
    Serve {
        /// Port to listen on (default: 3847)
        #[arg(long, short, default_value = "3847")]
        port: u16,

        /// Directory to serve (default: current directory)
        #[arg(long, short, default_value = ".")]
        dir: String,

        /// Enable review mode with spike markers on pages
        #[arg(long, short)]
        marked: bool,

        /// Allowed CORS origin (e.g., https://spikes.sh). Without this flag, CORS is disabled (same-origin only).
        #[arg(long)]
        cors_allow_origin: Option<String>,
    },

    /// Deploy backend to Cloudflare
    Deploy {
        #[command(subcommand)]
        backend: DeployBackend,
    },

    /// Fetch spikes from remote and merge with local
    Pull {
        /// Remote endpoint URL (or from .spikes/config.toml)
        #[arg(long)]
        endpoint: Option<String>,

        /// Auth token (or from .spikes/config.toml)
        #[arg(long)]
        token: Option<String>,

        /// Pull from a public share URL (e.g., https://spikes.sh/s/project-slug)
        #[arg(long)]
        from: Option<String>,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Upload local spikes to remote
    Push {
        /// Remote endpoint URL (or from .spikes/config.toml)
        #[arg(long)]
        endpoint: Option<String>,

        /// Auth token (or from .spikes/config.toml)
        #[arg(long)]
        token: Option<String>,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Sync with remote (pull then push)
    Sync {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Manage remote endpoint configuration
    Remote {
        #[command(subcommand)]
        action: RemoteAction,
    },

    /// Show current configuration
    Config {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Show version
    Version,

    /// Update spikes CLI and widget to the latest version
    Update,

    /// Log in to spikes.sh hosted service
    Login {
        /// Auth token (or enter interactively)
        #[arg(long)]
        token: Option<String>,

        /// Use email magic link instead of browser flow
        #[arg(long)]
        email: bool,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Log out from spikes.sh
    Logout {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Show current user identity
    Whoami {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Upload a directory to spikes.sh for instant sharing
    Share {
        /// Directory to upload
        directory: String,

        /// Custom name for the share URL
        #[arg(long)]
        name: Option<String>,

        /// Password-protect the share (Pro only)
        #[arg(long)]
        password: Option<String>,

        /// Host URL for the API
        #[arg(long, default_value = "https://spikes.sh")]
        host: String,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// List your shared projects on spikes.sh
    Shares {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Delete a shared project from spikes.sh
    Unshare {
        /// Share slug to delete
        slug: String,

        /// Skip confirmation prompt
        #[arg(long, short)]
        force: bool,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Delete a spike from local storage
    Delete {
        /// Spike ID or prefix (minimum 4 characters)
        id: String,

        /// Skip confirmation prompt
        #[arg(long, short)]
        force: bool,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Mark a spike as resolved
    Resolve {
        /// Spike ID or prefix (minimum 4 characters)
        id: String,

        /// Mark as unresolved instead
        #[arg(long)]
        unresolve: bool,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Open Stripe billing portal in browser
    Billing {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Upgrade to Pro subscription via Stripe checkout
    Upgrade {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Display current usage statistics
    Usage {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// MCP (Model Context Protocol) server for agent integration
    Mcp {
        #[command(subcommand)]
        action: McpAction,
    },

    /// Manage API keys for agent authentication
    Auth {
        #[command(subcommand)]
        action: AuthAction,
    },
}

#[derive(Subcommand)]
enum DeployBackend {
    /// Scaffold Cloudflare Worker + D1 for self-hosted feedback sync
    ///
    /// This scaffolds a Cloudflare Worker with D1 database for self-hosting your feedback
    /// backend. Use this when you need data isolation or a custom domain. For quick
    /// temporary previews, use `spikes share` instead.
    ///
    /// spikes.sh already hosts this backend for you — this is for self-hosting only.
    Cloudflare {
        /// Output directory (default: ./spikes-worker)
        #[arg(long)]
        dir: Option<String>,

        /// Output as JSON
        #[arg(long)]
        json: bool,

        /// Bypass the hosted spikes.sh warning prompt (non-interactive)
        #[arg(long, short)]
        force: bool,
    },
}

#[derive(Subcommand)]
enum McpAction {
    /// Start the MCP server (stdio transport by default)
    Serve {
        /// Use hosted API instead of local JSONL (requires SPIKES_TOKEN or auth.toml)
        #[arg(long)]
        remote: bool,

        /// Transport mode: stdio or http (default: stdio)
        #[arg(long, value_enum, default_value = "stdio")]
        transport: McpTransport,

        /// Port for HTTP transport (default: 3848, only used with --transport http)
        #[arg(long, default_value = "3848")]
        port: u16,

        /// Bind address for HTTP transport (default: 127.0.0.1, only used with --transport http)
        #[arg(long, default_value = "127.0.0.1")]
        bind: String,
    },

    /// Generate MCP config for Claude Desktop, Cursor, or other clients
    Install {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },
}

/// MCP transport mode
#[derive(Clone, Debug, clap::ValueEnum)]
enum McpTransport {
    /// Use standard input/output for JSON-RPC (default)
    Stdio,
    /// Use HTTP transport with POST endpoint
    Http,
}

#[derive(Subcommand)]
enum AuthAction {
    /// Create a new API key for agent authentication
    CreateKey {
        /// Optional name/label for the key
        #[arg(long)]
        name: Option<String>,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// List all API keys
    ListKeys {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Revoke an API key
    RevokeKey {
        /// The key ID to revoke (e.g. key_abc123)
        key_id: String,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },
}

#[derive(Subcommand)]
enum RemoteAction {
    /// Add or update remote endpoint
    Add {
        /// Endpoint URL
        endpoint: String,

        /// Auth token
        #[arg(long)]
        token: Option<String>,

        /// Use spikes.sh hosted backend
        #[arg(long)]
        hosted: bool,
    },

    /// Remove remote configuration
    Remove,

    /// Show current remote configuration
    Show {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },
}

fn main() {
    let cli = Cli::parse();

    let result = match cli.command {
        // Magic mode: no subcommand = auto-serve current directory
        None => commands::magic::run(cli.port),
        Some(Commands::Init { json, self_host }) => commands::init::run(json, self_host),
        Some(Commands::List {
            json,
            page,
            reviewer,
            rating,
            unresolved,
        }) => commands::list::run(ListOptions {
            json,
            page,
            reviewer,
            rating,
            unresolved,
        }),
        Some(Commands::Show { id, json }) => commands::show::run(&id, json),
        Some(Commands::Export { format }) => {
            let fmt = match format.parse::<ExportFormat>() {
                Ok(f) => f,
                Err(e) => {
                    eprintln!("Error: {}", e);
                    std::process::exit(1);
                }
            };
            commands::export::run(fmt)
        }
        Some(Commands::Hotspots { json }) => commands::hotspots::run(json),
        Some(Commands::Reviewers { json }) => commands::reviewers::run(json),
        Some(Commands::Inject {
            directory,
            remove,
            widget_url,
            endpoint,
            json,
        }) => commands::inject::run(InjectOptions {
            directory,
            remove,
            widget_url,
            endpoint,
            json,
        }),
        Some(Commands::Serve { port, dir, marked, cors_allow_origin }) => commands::serve::run(ServeOptions {
            port,
            directory: dir,
            marked,
            cors_allow_origin,
        }),
        Some(Commands::Deploy { backend }) => match backend {
            DeployBackend::Cloudflare { dir, json, force } => {
                commands::deploy::run(DeployOptions { dir, json, force })
            }
        },
        Some(Commands::Pull {
            endpoint,
            token,
            from,
            json,
        }) => commands::pull::run(PullOptions {
            endpoint,
            token,
            from,
            json,
        }),
        Some(Commands::Push {
            endpoint,
            token,
            json,
        }) => commands::push::run(PushOptions {
            endpoint,
            token,
            json,
        }),
        Some(Commands::Sync { json }) => commands::sync::run(json),
        Some(Commands::Remote { action }) => match action {
            RemoteAction::Add { endpoint, token, hosted } => {
                commands::remote::add(&endpoint, token, hosted)
            }
            RemoteAction::Remove => commands::remote::remove(),
            RemoteAction::Show { json } => commands::remote::show(json),
        },
        Some(Commands::Config { json }) => commands::config_cmd::run(json),
        Some(Commands::Version) => {
            println!("spikes {}", env!("CARGO_PKG_VERSION"));
            Ok(())
        }
        Some(Commands::Update) => commands::update::run(),
        Some(Commands::Login { token, email, json }) => commands::login::run(LoginOptions { token, email, json }),
        Some(Commands::Logout { json }) => commands::logout::run(json),
        Some(Commands::Whoami { json }) => commands::whoami::run(json),
        Some(Commands::Share { directory, name, password, host, json }) => {
            commands::share::run(ShareOptions { directory, name, password, host, json })
        }
        Some(Commands::Shares { json }) => commands::shares::run(SharesOptions { json }),
        Some(Commands::Unshare { slug, force, json }) => {
            commands::unshare::run(UnshareOptions { slug, force, json })
        }
        Some(Commands::Delete { id, force, json }) => {
            commands::delete::run(DeleteOptions { id, force, json })
        }
        Some(Commands::Resolve { id, unresolve, json }) => {
            commands::resolve::run(ResolveOptions { id, unresolve, json })
        }
        Some(Commands::Billing { json }) => commands::billing::run(json),
        Some(Commands::Upgrade { json }) => commands::upgrade::run(json),
        Some(Commands::Usage { json }) => commands::usage::run(UsageOptions { json }),
        Some(Commands::Mcp { action }) => match action {
            McpAction::Serve { remote, transport, port, bind } => {
                let transport_mode = match transport {
                    McpTransport::Stdio => commands::mcp::TransportMode::Stdio,
                    McpTransport::Http => commands::mcp::TransportMode::Http { port, bind },
                };
                commands::mcp::run(remote, transport_mode)
            }
            McpAction::Install { json } => {
                commands::mcp::install(json)
            }
        },
        Some(Commands::Auth { action }) => match action {
            AuthAction::CreateKey { name, json } => {
                commands::auth_keys::create_key(name, json)
            }
            AuthAction::ListKeys { json } => {
                commands::auth_keys::list_keys(json)
            }
            AuthAction::RevokeKey { key_id, json } => {
                commands::auth_keys::revoke_key(&key_id, json)
            }
        },
    };

    if let Err(e) = result {
        eprintln!("Error: {}", e);
        std::process::exit(1);
    }
}