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
|
use std::{net::SocketAddr, str::FromStr};
use clap::{Parser, Subcommand, arg, command};
use just_enough_vcs::{
system::action_system::action::ActionContext,
utils::tcp_connection::error::TcpTargetError,
vcs::{actions::local_actions::proc_set_upstream_vault_action, registry::client_registry},
};
use just_enough_vcs_cli::utils::{lang_selector::current_locales, md_colored::md};
use rust_i18n::{set_locale, t};
// Import i18n files
rust_i18n::i18n!("locales", fallback = "en");
#[derive(Parser, Debug)]
#[command(
disable_help_flag = true,
disable_version_flag = true,
disable_help_subcommand = true,
help_template = "{all-args}"
)]
struct JustEnoughVcsWorkspace {
#[command(subcommand)]
command: JustEnoughVcsWorkspaceCommand,
}
#[derive(Subcommand, Debug)]
enum JustEnoughVcsWorkspaceCommand {
// Member management
/// Manage your local accounts
#[command(subcommand)]
Account(AccountManage),
/// Create an empty workspace
Create(CreateWorkspaceArgs),
/// Create an empty workspace in the current directory
Init(InitWorkspaceArgs),
/// Get workspace information in the current directory
#[command(alias = "h")]
Here(HereArgs),
// Sheet management
/// Manage sheets in the workspace
#[command(subcommand)]
Sheet(SheetManage),
// File management
/// Track files to the upstream vault
/// First track - Create and upload the "First Version", then hold them
/// Subsequent tracks - Update files with new versions
#[command(alias = "t")]
Track(TrackFileArgs),
/// Hold files for editing
#[command(alias = "hd")]
Hold(HoldFileArgs),
/// Throw files, and release edit rights
#[command(alias = "tr")]
Throw(ThrowFileArgs),
/// Move or rename files safely
#[command(alias = "mv")]
Move(MoveFileArgs),
/// Export files to other worksheet
#[command(alias = "out")]
Export(ExportFileArgs),
/// Import files from reference sheet or import area
#[command(alias = "in")]
Import(ImportFileArgs),
// Connection management
/// Direct to an upstream vault and stain this workspace
Direct(DirectArgs),
/// DANGER ZONE : Unstain this workspace
Unstain(UnstainArgs),
// Other
/// Query built-in documentation
Docs(DocsArgs),
}
#[derive(Subcommand, Debug)]
enum AccountManage {
/// Show help information
#[command(alias = "--help", alias = "-h")]
Help,
/// Register a member to this computer
Add(AccountAddArgs),
/// Remove a account from this computer
Remove(AccountRemoveArgs),
}
#[derive(Subcommand, Debug)]
enum SheetManage {
/// Show help information
#[command(alias = "--help", alias = "-h")]
Help,
}
#[derive(Parser, Debug)]
struct AccountAddArgs {
/// Member name
member_name: String,
}
#[derive(Parser, Debug)]
struct AccountRemoveArgs {
/// Member name
member_name: String,
}
#[derive(Parser, Debug)]
struct CreateWorkspaceArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct InitWorkspaceArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct HereArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct TrackFileArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct HoldFileArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct ThrowFileArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct MoveFileArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct ExportFileArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct ImportFileArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct DirectArgs {
/// Show help information
#[arg(short, long)]
help: bool,
/// Upstream vault address
upstream: String,
}
#[derive(Parser, Debug)]
struct UnstainArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[derive(Parser, Debug)]
struct DocsArgs {
/// Show help information
#[arg(short, long)]
help: bool,
}
#[tokio::main]
async fn main() {
// Init i18n
set_locale(¤t_locales());
// Init colored
#[cfg(windows)]
colored::control::set_virtual_terminal(true).unwrap();
let Ok(parser) = JustEnoughVcsWorkspace::try_parse() else {
println!("{}", md(t!("jv.help")));
return;
};
match parser.command {
JustEnoughVcsWorkspaceCommand::Account(account_manage) => match account_manage {
AccountManage::Help => {
println!("{}", md(t!("jv.account")));
}
AccountManage::Add(account_add_args) => todo!(),
AccountManage::Remove(account_remove_args) => todo!(),
},
JustEnoughVcsWorkspaceCommand::Create(create_workspace_args) => {
if create_workspace_args.help {
println!("{}", md(t!("jv.create")));
return;
}
jv_create(create_workspace_args).await;
}
JustEnoughVcsWorkspaceCommand::Init(init_workspace_args) => {
if init_workspace_args.help {
println!("{}", md(t!("jv.init")));
return;
}
jv_init(init_workspace_args).await;
}
JustEnoughVcsWorkspaceCommand::Here(here_args) => {
if here_args.help {
println!("{}", md(t!("jv.here")));
return;
}
jv_here(here_args).await;
}
JustEnoughVcsWorkspaceCommand::Sheet(sheet_manage) => match sheet_manage {
SheetManage::Help => {
println!("{}", md(t!("jv.sheet")));
return;
}
},
JustEnoughVcsWorkspaceCommand::Track(track_file_args) => {
if track_file_args.help {
println!("{}", md(t!("jv.track")));
return;
}
jv_track(track_file_args).await;
}
JustEnoughVcsWorkspaceCommand::Hold(hold_file_args) => {
if hold_file_args.help {
println!("{}", md(t!("jv.hold")));
return;
}
jv_hold(hold_file_args).await;
}
JustEnoughVcsWorkspaceCommand::Throw(throw_file_args) => {
if throw_file_args.help {
println!("{}", md(t!("jv.throw")));
return;
}
jv_throw(throw_file_args).await;
}
JustEnoughVcsWorkspaceCommand::Move(move_file_args) => {
if move_file_args.help {
println!("{}", md(t!("jv.move")));
return;
}
jv_move(move_file_args).await;
}
JustEnoughVcsWorkspaceCommand::Export(export_file_args) => {
if export_file_args.help {
println!("{}", md(t!("jv.export")));
return;
}
jv_export(export_file_args).await;
}
JustEnoughVcsWorkspaceCommand::Import(import_file_args) => {
if import_file_args.help {
println!("{}", md(t!("jv.import")));
return;
}
jv_import(import_file_args).await;
}
JustEnoughVcsWorkspaceCommand::Direct(direct_args) => {
if direct_args.help {
println!("{}", md(t!("jv.direct")));
return;
}
jv_direct(direct_args).await;
}
JustEnoughVcsWorkspaceCommand::Unstain(unstain_args) => {
if unstain_args.help {
println!("{}", md(t!("jv.unstain")));
return;
}
jv_unstain(unstain_args).await;
}
JustEnoughVcsWorkspaceCommand::Docs(docs_args) => {
if docs_args.help {
println!("{}", md(t!("jv.docs")));
return;
}
jv_docs(docs_args).await;
}
}
}
async fn jv_create(_args: CreateWorkspaceArgs) {
todo!()
}
async fn jv_init(_args: InitWorkspaceArgs) {
todo!()
}
async fn jv_here(_args: HereArgs) {
todo!()
}
async fn jv_track(_args: TrackFileArgs) {
todo!()
}
async fn jv_hold(_args: HoldFileArgs) {
todo!()
}
async fn jv_throw(_args: ThrowFileArgs) {
todo!()
}
async fn jv_move(_args: MoveFileArgs) {
todo!()
}
async fn jv_export(_args: ExportFileArgs) {
todo!()
}
async fn jv_import(_args: ImportFileArgs) {
todo!()
}
async fn jv_direct(args: DirectArgs) {
let pool = client_registry::client_action_pool();
let upstream = match SocketAddr::from_str(&args.upstream) {
Ok(result) => result,
Err(_) => {
eprintln!(
"{}",
md(t!(
"jv.fail.parse.str_to_sockaddr",
str = &args.upstream.trim()
))
);
return;
}
};
let ctx = ActionContext::local();
match proc_set_upstream_vault_action(&pool, ctx, upstream).await {
Err(e) => handle_err(e),
_ => {}
};
}
async fn jv_unstain(_args: UnstainArgs) {
todo!()
}
async fn jv_docs(_args: DocsArgs) {
todo!()
}
pub fn handle_err(err: TcpTargetError) {
let e: Option<(String, String, bool)> = match err {
TcpTargetError::Io(err) => Some(fsio(err)),
TcpTargetError::File(err) => Some(fsio(err)),
TcpTargetError::Serialization(err) => Some(serialize(err)),
TcpTargetError::Authentication(err) => Some(auth(err)),
TcpTargetError::Network(err) => Some(connection(err)),
TcpTargetError::Timeout(err) => Some(connection(err)),
TcpTargetError::Protocol(err) => Some(connection(err)),
_ => Some((
err.to_string(),
md(t!("jv.fail.action_operation_fail.type_other")),
false,
)),
};
if let Some((err_text, err_tip, has_tip)) = e {
eprintln!(
"{}\n{}",
md(t!("jv.fail.action_operation_fail.main", err = err_text)),
err_tip,
);
if has_tip {
eprintln!(
"{}",
md(t!("jv.fail.action_operation_fail.info_contact_admin"))
)
}
}
}
type ErrorText = String;
type ErrorTip = String;
type HasTip = bool;
fn fsio(err: String) -> (ErrorText, ErrorTip, HasTip) {
(
err,
md(t!("jv.fail.action_operation_fail.type_fsio")).to_string(),
true,
)
}
fn serialize(err: String) -> (ErrorText, ErrorTip, HasTip) {
(
err,
md(t!("jv.fail.action_operation_fail.type_serialize")).to_string(),
true,
)
}
fn auth(err: String) -> (ErrorText, ErrorTip, HasTip) {
(
err,
md(t!("jv.fail.action_operation_fail.type_auth")).to_string(),
true,
)
}
fn connection(err: String) -> (ErrorText, ErrorTip, HasTip) {
(
err,
md(t!("jv.fail.action_operation_fail.type_connection")).to_string(),
true,
)
}
|