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
use anyhow::Result;
use clap::{CommandFactory, Parser};
use clap_complete::Shell;
use console::style;
use std::path::PathBuf;
mod config;
mod create_template;
mod init;
mod prompts;
mod publish;
mod remote;
mod search;
mod spec;
mod templates;
#[cfg(feature = "tui")]
mod tui;
mod update;
mod versioning;
mod work;
#[derive(Parser)]
#[command(name = "fledge", version, about = "Get your projects ready to fly.")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(clap::Subcommand)]
enum Commands {
/// Create a new project from a template
Init {
/// Project name
name: String,
/// Template to use (skip interactive selection)
#[arg(short, long)]
template: Option<String>,
/// Parent directory for the project
#[arg(short, long, default_value = ".")]
output: PathBuf,
/// Skip git init and initial commit
#[arg(long)]
no_git: bool,
/// Skip dependency installation (post-create hooks)
#[arg(long)]
no_install: bool,
/// Force re-clone of cached remote templates
#[arg(long)]
refresh: bool,
/// Show what would be created without writing anything
#[arg(long)]
dry_run: bool,
/// Skip all confirmation prompts (accept defaults)
#[arg(short, long)]
yes: bool,
},
/// List available templates
List,
/// Generate shell completions
Completions {
/// Shell to generate completions for
#[arg(value_enum)]
shell: Shell,
},
/// Manage global configuration
Config {
#[command(subcommand)]
action: ConfigAction,
},
/// Scaffold a new fledge template
CreateTemplate {
/// Template name
name: String,
/// Parent directory for the template
#[arg(short, long, default_value = ".")]
output: PathBuf,
},
/// Search for templates on GitHub
Search {
/// Keyword to filter results
query: Option<String>,
/// Maximum number of results
#[arg(short, long, default_value = "20")]
limit: usize,
/// Output results as JSON
#[arg(long)]
json: bool,
},
/// Update project from its source template
Update {
/// Show what would change without writing anything
#[arg(long)]
dry_run: bool,
/// Force re-clone of cached remote templates
#[arg(long)]
refresh: bool,
/// Skip all confirmation prompts
#[arg(short, long)]
yes: bool,
},
/// Publish a template to GitHub
Publish {
/// Path to the template directory
#[arg(default_value = ".")]
path: PathBuf,
/// Publish under a GitHub organization
#[arg(long)]
org: Option<String>,
/// Create as a private repository
#[arg(long)]
private: bool,
/// Override the repository description
#[arg(long)]
description: Option<String>,
},
/// Manage specs (check, init, new)
Spec {
#[command(subcommand)]
action: SpecSubcommand,
},
/// Feature branch and PR workflow
Work {
#[command(subcommand)]
action: WorkSubcommand,
},
/// Interactive TUI for browsing and scaffolding templates (requires --features tui)
#[cfg(feature = "tui")]
Tui {
/// Parent directory for the project
#[arg(short, long, default_value = ".")]
output: PathBuf,
/// Skip git init and initial commit
#[arg(long)]
no_git: bool,
},
}
#[derive(clap::Subcommand)]
enum SpecSubcommand {
/// Validate specs against source code
Check {
/// Treat warnings as errors
#[arg(long)]
strict: bool,
},
/// Initialize spec-sync configuration
Init,
/// Scaffold a new spec module
New {
/// Module name
name: String,
},
}
#[derive(clap::Subcommand)]
enum WorkSubcommand {
/// Start a new feature branch
Start {
/// Feature name (will be sanitized and prefixed with feat/)
name: String,
/// Base branch to branch from (default: main)
#[arg(long)]
base: Option<String>,
},
/// Create a pull request from the current branch
Pr {
/// PR title (auto-generated from branch name if omitted)
#[arg(short, long)]
title: Option<String>,
/// PR body/description
#[arg(short, long)]
body: Option<String>,
/// Create as a draft PR
#[arg(long)]
draft: bool,
/// Target base branch for the PR
#[arg(long)]
base: Option<String>,
},
/// Show current branch and PR status
Status,
}
#[derive(clap::Subcommand)]
enum ConfigAction {
/// Get a config value
Get {
/// Config key (e.g. defaults.github_org)
key: String,
},
/// Set a config value
Set {
/// Config key (e.g. defaults.github_org)
key: String,
/// Value to set
value: String,
},
/// Remove a config value
Unset {
/// Config key (e.g. defaults.github_org)
key: String,
},
/// Add a value to a list config key (templates.paths, templates.repos)
Add {
/// Config key (templates.paths or templates.repos)
key: String,
/// Value to add
value: String,
},
/// Remove a value from a list config key (templates.paths, templates.repos)
Remove {
/// Config key (templates.paths or templates.repos)
key: String,
/// Value to remove
value: String,
},
/// Show all config values
List,
/// Show config file path
Path,
}
fn main() {
if let Err(e) = run() {
eprintln!("{} {:#}", style("error:").red().bold(), e);
std::process::exit(1);
}
}
fn run() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Init {
name,
template,
output,
no_git,
no_install,
refresh,
dry_run,
yes,
} => {
init::run(init::InitOptions {
name,
template,
output,
no_git,
no_install,
refresh,
dry_run,
yes,
})?;
}
Commands::List => {
list_templates()?;
}
Commands::Config { action } => {
handle_config(action)?;
}
Commands::CreateTemplate { name, output } => {
create_template::run(create_template::CreateTemplateOptions { name, output })?;
}
Commands::Update {
dry_run,
refresh,
yes,
} => {
update::run(update::UpdateOptions {
dry_run,
refresh,
yes,
})?;
}
Commands::Search { query, limit, json } => {
search::run(search::SearchOptions { query, limit, json })?;
}
Commands::Publish {
path,
org,
private,
description,
} => {
publish::run(publish::PublishOptions {
path,
org,
private,
description,
})?;
}
Commands::Spec { action } => {
let action = match action {
SpecSubcommand::Check { strict } => spec::SpecAction::Check { strict },
SpecSubcommand::Init => spec::SpecAction::Init,
SpecSubcommand::New { name } => spec::SpecAction::New { name },
};
spec::run(action)?;
}
Commands::Work { action } => {
let action = match action {
WorkSubcommand::Start { name, base } => work::WorkAction::Start { name, base },
WorkSubcommand::Pr {
title,
body,
draft,
base,
} => work::WorkAction::Pr {
title,
body,
draft,
base,
},
WorkSubcommand::Status => work::WorkAction::Status,
};
work::run(action)?;
}
Commands::Completions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "fledge", &mut std::io::stdout());
}
#[cfg(feature = "tui")]
Commands::Tui { output, no_git } => {
tui::run(output, no_git)?;
}
}
Ok(())
}
fn handle_config(action: ConfigAction) -> Result<()> {
match action {
ConfigAction::Get { key } => {
let config = config::Config::load()?;
if !config::Config::is_valid_key(&key) {
anyhow::bail!(
"Unknown config key '{}'. Valid keys: defaults.author, defaults.github_org, defaults.license, github.token, templates.paths, templates.repos",
key
);
}
match config.get(&key) {
Some(value) if !value.is_empty() => println!("{}", value),
_ => println!("{} {} is not set", style("*").cyan().bold(), key),
}
}
ConfigAction::Set { key, value } => {
let mut config = config::Config::load()?;
config.set(&key, &value)?;
config.save()?;
println!(
"{} Set {} = {}",
style("✓").green().bold(),
style(&key).cyan(),
style(&value).green()
);
}
ConfigAction::Unset { key } => {
let mut config = config::Config::load()?;
config.unset(&key)?;
config.save()?;
println!("{} Unset {}", style("✓").green().bold(), style(&key).cyan());
}
ConfigAction::Add { key, value } => {
let mut config = config::Config::load()?;
config.add_to_list(&key, &value)?;
config.save()?;
println!(
"{} Added {} to {}",
style("✓").green().bold(),
style(&value).green(),
style(&key).cyan()
);
}
ConfigAction::Remove { key, value } => {
let mut config = config::Config::load()?;
let removed = config.remove_from_list(&key, &value)?;
if removed {
config.save()?;
println!(
"{} Removed {} from {}",
style("✓").green().bold(),
style(&value).green(),
style(&key).cyan()
);
} else {
println!(
"{} {} not found in {}",
style("*").cyan().bold(),
style(&value).dim(),
style(&key).cyan()
);
}
}
ConfigAction::List => {
let config = config::Config::load()?;
let path = config::Config::config_path();
println!(
"{} Config: {}\n",
style("*").cyan().bold(),
style(path.display()).dim()
);
print_config_entry("defaults.author", &config.defaults.author);
print_config_entry("defaults.github_org", &config.defaults.github_org);
print_config_entry("defaults.license", &config.defaults.license);
print_config_entry(
"github.token",
&config.github.token.as_ref().map(|_| "***".to_string()),
);
if config.templates.paths.is_empty() {
println!(
" {:<24} {}",
style("templates.paths").cyan(),
style("(none)").dim()
);
} else {
for (i, p) in config.templates.paths.iter().enumerate() {
if i == 0 {
println!(" {:<24} {}", style("templates.paths").cyan(), p);
} else {
println!(" {:<24} {}", "", p);
}
}
}
if config.templates.repos.is_empty() {
println!(
" {:<24} {}",
style("templates.repos").cyan(),
style("(none)").dim()
);
} else {
for (i, r) in config.templates.repos.iter().enumerate() {
if i == 0 {
println!(" {:<24} {}", style("templates.repos").cyan(), r);
} else {
println!(" {:<24} {}", "", r);
}
}
}
}
ConfigAction::Path => {
println!("{}", config::Config::config_path().display());
}
}
Ok(())
}
fn print_config_entry(key: &str, value: &Option<impl std::fmt::Display>) {
match value {
Some(v) => println!(" {:<24} {}", style(key).cyan(), v),
None => println!(" {:<24} {}", style(key).cyan(), style("(not set)").dim()),
}
}
fn list_templates() -> Result<()> {
let config = config::Config::load()?;
let extra_paths = config.extra_template_paths();
let token = config.github_token();
let available = templates::discover_templates_with_repos(
&extra_paths,
config.template_repos(),
token.as_deref(),
)?;
if available.is_empty() {
println!("No templates found.");
return Ok(());
}
println!("{}", style("Available templates:").bold());
for t in &available {
println!(
" {:<14} {}",
style(&t.name).green(),
style(&t.description).dim()
);
}
Ok(())
}