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
use anyhow::Result;
use clap::{Parser, Subcommand};
use kugiri::{extract, insert, remove, trim, update, upsert, wrap};
use std::fs;
mod io;
use io::{read_file_or_stdin, write_output};
const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), env!("GIT_VERSION_SUFFIX"));
#[derive(Parser)]
#[command(name = "kugiri")]
#[command(about = "Marker-based block editing CLI", long_about = None)]
#[command(version = VERSION)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Insert a new section before or after a marker
Insert {
/// File to edit
file: String,
/// Section ID for the new section
#[arg(long)]
id: String,
/// Content file (default: stdin, use '-' for stdin explicitly)
#[arg(long)]
body_file: Option<String>,
/// Insert before this marker ID
#[arg(long, conflicts_with = "after")]
before: Option<String>,
/// Insert after this marker ID
#[arg(long, conflicts_with = "before")]
after: Option<String>,
/// Write changes in-place
#[arg(short, long)]
write: bool,
},
/// Update an existing section identified by --id
Update {
/// File to edit
file: String,
/// Section ID to update
#[arg(long)]
id: String,
/// Content file (default: stdin, use '-' for stdin explicitly)
#[arg(long)]
body_file: Option<String>,
/// Write changes in-place
#[arg(short, long)]
write: bool,
},
/// Update existing section or insert if not found
Upsert {
/// File to edit
file: String,
/// Section ID for the section
#[arg(long)]
id: String,
/// Content file (default: stdin, use '-' for stdin explicitly)
#[arg(long)]
body_file: Option<String>,
/// Insert before this marker ID (for new sections)
#[arg(long, conflicts_with = "after")]
before: Option<String>,
/// Insert after this marker ID (for new sections)
#[arg(long, conflicts_with = "before")]
after: Option<String>,
/// Write changes in-place
#[arg(short, long)]
write: bool,
},
/// Remove a section and its markers
Remove {
/// File to edit
file: String,
/// Section ID to remove
#[arg(long)]
id: String,
/// Write changes in-place
#[arg(short, long)]
write: bool,
},
/// Print inner content of a section (without markers)
Extract {
/// File to read
file: String,
/// Section ID to extract
#[arg(long)]
id: String,
},
/// Output the file with all marker lines removed
Trim {
/// File to read
file: String,
},
/// Wrap content with KUGIRI markers
Wrap {
/// Section ID for the markers
#[arg(long)]
id: String,
/// Content file (default: stdin, use '-' for stdin explicitly)
#[arg(long)]
body_file: Option<String>,
},
}
fn main() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Insert {
file,
id,
body_file,
before,
after,
write,
} => {
let text = fs::read_to_string(&file)?;
let body = read_file_or_stdin(body_file.as_deref())?;
let result = insert(&text, &id, &body, before.as_deref(), after.as_deref())?;
write_output(&file, &result, write)?;
}
Commands::Update {
file,
id,
body_file,
write,
} => {
let text = fs::read_to_string(&file)?;
let body = read_file_or_stdin(body_file.as_deref())?;
let result = update(&text, &id, &body)?;
write_output(&file, &result, write)?;
}
Commands::Upsert {
file,
id,
body_file,
before,
after,
write,
} => {
let text = fs::read_to_string(&file)?;
let body = read_file_or_stdin(body_file.as_deref())?;
let result = upsert(&text, &id, &body, before.as_deref(), after.as_deref())?;
write_output(&file, &result, write)?;
}
Commands::Remove { file, id, write } => {
let text = fs::read_to_string(&file)?;
let result = remove(&text, &id)?;
write_output(&file, &result, write)?;
}
Commands::Extract { file, id } => {
let text = fs::read_to_string(&file)?;
let result = extract(&text, &id)?;
println!("{result}");
}
Commands::Trim { file } => {
let text = fs::read_to_string(&file)?;
let result = trim(&text);
println!("{result}");
}
Commands::Wrap { id, body_file } => {
let content = read_file_or_stdin(body_file.as_deref())?;
let result = wrap(&content, &id);
println!("{result}");
}
}
Ok(())
}