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
use clap::{ColorChoice, Parser, Subcommand};
#[derive(Parser)]
#[command(
name = "kagi",
about = "Manage encrypted environment variables",
version,
color = ColorChoice::Auto
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
/// Allow insecure HTTP remotes for non-localhost addresses
#[arg(long, global = true)]
pub allow_insecure_http: bool,
}
#[derive(Subcommand)]
pub enum Commands {
/// Initialize a new kagi repository in the current directory
Init {
/// Default environments for services (comma-separated, e.g., development,test)
#[arg(short, long, value_delimiter = ',', num_args = 0..=1, default_missing_value = "")]
envs: Option<Vec<String>>,
/// Enable nested service inference from subdirectories
#[arg(long)]
nested: bool,
/// Overwrite existing .kagi/ directory
#[arg(long)]
force: bool,
},
/// Store an encrypted secret for an environment
Set {
/// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
#[arg(short, long)]
service: Option<String>,
/// Description of the secret (shown in exports and lists)
#[arg(short, long)]
desc: Option<String>,
/// Service or environment name
first: Option<String>,
/// Environment name or secret key
second: Option<String>,
/// Secret key or value
third: Option<String>,
/// Value to store (will be encrypted), when service and env are both provided positionally
fourth: Option<String>,
},
/// Get service/env/key information or decrypt one secret value
Get {
/// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
#[arg(short, long)]
service: Option<String>,
/// Show decrypted values when listing. Requires an interactive terminal.
#[arg(long = "show")]
show_values: bool,
/// Service or environment name
first: Option<String>,
/// Environment name or secret key
second: Option<String>,
/// Secret key, when service and env are both provided positionally
third: Option<String>,
},
/// Run a command with injected environment variables
Run {
/// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
#[arg(short, long)]
service: Option<String>,
/// <service> <command>... or <command>... when the scope is inferred from nested directory.
#[arg(required = false, trailing_var_arg = true)]
args: Vec<String>,
},
/// Export secrets as KEY=value lines (suitable for shell sourcing)
Export {
/// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
#[arg(short, long)]
service: Option<String>,
/// Directory to write one .env file per environment when exporting a service
#[arg(short, long)]
out: Option<String>,
/// Service or environment name
first: Option<String>,
/// Environment name, when service is provided positionally
second: Option<String>,
},
/// Import secrets from a .env file
Import {
/// Optional service scope (e.g., api, web). Defaults to the inferred nested directory.
#[arg(short, long)]
service: Option<String>,
/// Service or environment name
first: Option<String>,
/// Environment name, when service is provided positionally
second: Option<String>,
/// Path to the env file to import
#[arg(short, long, default_value = ".env")]
file: String,
/// Overwrite existing keys without prompting
#[arg(long)]
force: bool,
},
/// Synchronize keys from .env.example (and optional sources) across environments
Sync {
/// Optional service scope (e.g., api, web). Defaults to the inferred nested directory when present.
#[arg(short, long)]
service: Option<String>,
/// Path to the .env.example template file
#[arg(short, long, default_value = ".env.example")]
example: String,
/// Additional source files to merge (comma-separated, later overrides earlier)
#[arg(long, value_delimiter = ',')]
sources: Vec<String>,
/// Environments to sync (comma-separated)
#[arg(
long,
value_delimiter = ',',
default_value = "development,test,staging,production"
)]
envs: Vec<String>,
},
/// Manage default environments
Env {
#[command(subcommand)]
command: EnvCommands,
},
/// Manage project members
Member {
#[command(subcommand)]
command: MemberCommands,
},
#[cfg(feature = "server")]
/// Manage remote projects (join, list, approve, delete)
Project {
#[command(subcommand)]
command: ProjectCommands,
},
#[cfg(feature = "server")]
/// Start the Kagi remote sync server
Serve {
/// Database file path
#[arg(long, default_value = "")]
db: String,
/// Server key file path
#[arg(long, default_value = "")]
key_file: String,
/// Bind address
#[arg(long, default_value = "127.0.0.1:13816")]
bind: String,
/// Max body size (e.g. 10mb)
#[arg(long, default_value = "10mb")]
max_body: String,
/// Allow serving HTTP on non-localhost addresses (unsafe for production)
#[arg(long)]
allow_insecure_http: bool,
},
#[cfg(feature = "server")]
/// Manage remote server credentials and connections
Remote {
#[command(subcommand)]
command: RemoteCommands,
},
#[cfg(feature = "server")]
/// Upload local encrypted project state to remote server
Push,
#[cfg(feature = "server")]
/// Download encrypted project state from remote server
Pull {
/// Optional project token for pulling without local project
token: Option<String>,
},
#[cfg(feature = "server")]
/// Compare local and remote revisions
Status,
}
#[derive(Subcommand)]
pub enum EnvCommands {
/// List configured default environments
List,
/// Add an environment to every service
Add {
/// Environment name to add
env: String,
},
/// Rename an environment across every service
Rename {
/// Existing environment name
old: String,
/// New environment name
new: String,
},
/// Delete an environment from every service
Del {
/// Environment name to delete
env: String,
},
}
#[derive(Subcommand)]
pub enum MemberCommands {
/// List active members and pending join requests
List,
/// Request to join this project from a new device
Join {
/// Display name for the member requesting access
#[arg(short, long)]
name: Option<String>,
},
/// Approve a pending join request
Approve {
/// Member id from `kagi member list`
member_id: String,
},
/// Remove a member's access wrapper
Del {
/// Member id from `kagi member list`
member_id: String,
},
}
#[cfg(feature = "server")]
#[derive(Subcommand)]
pub enum RemoteCommands {
/// Save an admin token for a remote server (stored in OS keychain)
Login {
/// Remote server URL
#[arg(long)]
remote: String,
/// Admin token from server first startup
#[arg(long)]
token: String,
},
}
#[cfg(feature = "server")]
#[derive(Subcommand)]
pub enum ProjectCommands {
/// Request to register this local project on a remote server
Join {
/// Remote server URL
#[arg(long)]
remote: String,
},
/// List projects on the remote server (admin only)
List {
/// Remote server URL (optional if saved via `kagi remote login`)
#[arg(long)]
remote: Option<String>,
},
/// Approve a pending project registration request (admin only)
Approve {
/// Remote server URL (optional if saved via `kagi remote login`)
#[arg(long)]
remote: Option<String>,
/// Project ID to approve
project_id: String,
},
/// Delete a project from the remote server (admin or project admin)
Del {
/// Remote server URL (optional if saved via `kagi remote login`)
#[arg(long)]
remote: Option<String>,
/// Project ID to delete
project_id: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
#[cfg(feature = "server")]
fn test_server_commands_available_with_server_feature() {
let cmd = Cli::command();
let names: Vec<_> = cmd.get_subcommands().map(|c| c.get_name()).collect();
assert!(names.contains(&"serve"), "serve should be present");
assert!(names.contains(&"push"), "push should be present");
assert!(names.contains(&"pull"), "pull should be present");
assert!(names.contains(&"status"), "status should be present");
assert!(names.contains(&"project"), "project should be present");
assert!(names.contains(&"remote"), "remote should be present");
}
#[test]
#[cfg(not(feature = "server"))]
fn test_server_commands_not_available_without_server_feature() {
let cmd = Cli::command();
let names: Vec<_> = cmd.get_subcommands().map(|c| c.get_name()).collect();
assert!(
!names.contains(&"serve"),
"serve should NOT be present when server feature is disabled"
);
assert!(
!names.contains(&"push"),
"push should NOT be present when server feature is disabled"
);
assert!(
!names.contains(&"pull"),
"pull should NOT be present when server feature is disabled"
);
assert!(
!names.contains(&"status"),
"status should NOT be present when server feature is disabled"
);
assert!(
!names.contains(&"project"),
"project should NOT be present when server feature is disabled"
);
assert!(
!names.contains(&"remote"),
"remote should NOT be present when server feature is disabled"
);
}
}