-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli_args.rs
330 lines (301 loc) · 10 KB
/
cli_args.rs
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
/*
* Tackler-NG 2023-2025
* SPDX-License-Identifier: Apache-2.0
*/
use clap::builder::PossibleValue;
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser, Subcommand};
use std::error::Error;
use std::path::PathBuf;
use tackler_api::txn_ts;
use tackler_core::config;
use tackler_core::kernel::settings::{FileInput, FsInput, GitInput, InputSettings};
use tackler_core::kernel::Settings;
use tackler_core::parser::GitInputSelector;
//
// Default subcommand setup:
// https://github.com/clap-rs/clap/issues/975
//
#[derive(Parser)]
#[command(author, version=env!("VERSION"), about, long_about = None)]
#[command(args_conflicts_with_subcommands = true)]
pub(crate) struct Cli {
#[command(subcommand)]
command: Option<Commands>,
#[clap(flatten)]
pub args: DefaultArgs,
}
impl Cli {
pub(crate) fn cmd(&self) -> Commands {
let command = self
.command
.clone()
.unwrap_or(Commands::Report(self.args.clone()));
match self.command {
Some(_) => command,
None => {
if self.args.conf_path.is_none() {
let mut cmd = Cli::command();
let msg = format!(
"config file is not provided, use: \n\n{} --config <path/to/config-file>",
cmd.get_name()
);
cmd.error(ErrorKind::MissingRequiredArgument, msg.as_str())
.exit();
}
command
}
}
}
}
#[derive(Debug, Clone, clap::Args)]
#[group(multiple = false)]
pub(crate) struct GitInputGroup {
/// Git reference name
#[arg(
long = "input.git.ref",
value_name = "refname",
group = "git_input_group"
)]
pub(crate) input_git_ref: Option<String>,
/// Git object name (commit id)
#[arg(
long = "input.git.commit",
value_name = "sha",
group = "git_input_group"
)]
pub(crate) input_git_commit: Option<String>,
}
#[derive(Clone, Subcommand)]
#[allow(clippy::large_enum_variant)]
pub(crate) enum Commands {
/// create new bookkeeping setup
New { name: String },
/// Initialize existing bookkeeping setup
Init {},
/// This is the default action: run specified reports and exports
Report(DefaultArgs),
}
#[derive(Debug, Clone, clap::Args)]
pub(crate) struct DefaultArgs {
#[arg(long = "config", value_name = "path_to_config-file")]
pub(crate) conf_path: Option<PathBuf>,
/// Strict txn data mode
///
/// Turn on strict validation of transactions (accounts, commodities and tags).
#[arg(long = "strict.mode", value_name = "true|false")]
pub(crate) strict_mode: Option<bool>,
/// Txn set audit mode
///
/// Produce checksums for transaction data and account selectors
#[arg(long = "audit.mode", value_name = "true|false")]
pub(crate) audit_mode: Option<bool>,
/// Path to output directory
#[arg(
long = "output.dir",
value_name = "path_to_output-directory",
requires("output_name")
)]
pub(crate) output_directory: Option<PathBuf>,
/// Basename of report files
#[arg(
long = "output.prefix",
value_name = "filename-prefix",
requires("output_directory")
)]
pub(crate) output_name: Option<String>,
/// Path to single transaction journal file
#[arg(long="input.file",
value_name = "path_to_journal-file",
conflicts_with_all([
"input_storage",
"input_fs_dir",
"input_fs_ext",
"input_git_repo",
"input_git_ref",
"input_git_dir"])
)]
pub(crate) input_filename: Option<PathBuf>,
///
/// Select used transaction storage
///
#[arg(long="input.storage",
value_name = "type_of_storage",
value_parser([
PossibleValue::new(config::StorageType::STORAGE_FS),
PossibleValue::new(config::StorageType::STORAGE_GIT),
])
)]
pub(crate) input_storage: Option<String>,
/// Filsystem path to transaction directory
///
/// This could be a root or node of txn shard tree
#[arg(long="input.fs.dir",
value_name = "path_to_transaction-directory",
requires("input_fs_ext"),
conflicts_with_all([
"input_git_repo",
"input_git_ref",
"input_git_dir"])
)]
pub(crate) input_fs_dir: Option<PathBuf>,
/// Txn file extension
#[arg(
long = "input.fs.ext",
value_name = "txn_file-suffix",
requires("input_fs_dir")
)]
pub(crate) input_fs_ext: Option<String>,
/// Path to git repository
///
/// Path to '.git' directory or bare git-repository.
///
/// This could be a path to '.git' directory inside working copy
#[arg(
long = "input.git.repository",
value_name = "path",
requires("input_git_dir"),
requires("git_input_group")
)]
//requires("git_input_group"),
pub(crate) input_git_repo: Option<PathBuf>,
#[clap(flatten)]
git_input_selector: GitInputGroup,
/// Path (inside git repository) to transaction directory
///
/// This could be a root or node of txn shard tree
#[arg(
long = "input.git.dir",
value_name = "path_to_transaction-directory",
requires("input_git_repo")
)]
pub(crate) input_git_dir: Option<String>,
/// Account selectors for reports and exports
///
/// List of patterns (regex) for account names.
///
/// Use anchors ('^...$') for exact match.
///
/// Use empty string "" to list all accounts
#[arg(long = "accounts", value_name = "regex", num_args(1..))]
pub(crate) accounts: Option<Vec<String>>,
/// List of Reports to generate
///
/// The list is space separated
#[arg(long = "reports", value_name = "type", num_args(1..),
value_parser([
PossibleValue::new("register"),
PossibleValue::new("balance"),
PossibleValue::new("balance-group"),
])
)]
pub(crate) reports: Option<Vec<String>>,
/// Group-by -selector for 'balance-group' report
#[arg(long = "group-by", value_name = "group-by", num_args(1),
value_parser([
PossibleValue::new(txn_ts::GroupBy::YEAR),
PossibleValue::new(txn_ts::GroupBy::MONTH),
PossibleValue::new(txn_ts::GroupBy::DATE),
PossibleValue::new(txn_ts::GroupBy::ISO_WEEK),
PossibleValue::new(txn_ts::GroupBy::ISO_WEEK_DATE),
])
)]
pub(crate) group_by: Option<String>,
/// List of Exports to generate
///
/// The list is space separated
#[arg(long = "exports", value_name = "type", num_args(1..),
value_parser([
PossibleValue::new("identity"),
PossibleValue::new("equity"),
])
)]
pub(crate) exports: Option<Vec<String>>,
/// Txn Filter definition in JSON
///
/// This could be ascii armored with base64 encoding
///
/// The ascii armor must have prefix 'base64:'
///
/// e.g. "base64:eyJ0eG5GaWx0ZXIiOnsiTnVsbGFyeVRSVUUiOnt9fX0K"
#[arg(long = "api-filter-def", value_name = "txn_filter")]
pub(crate) api_filter_def: Option<String>,
}
impl DefaultArgs {
fn get_git_selector(&self) -> Option<GitInputSelector> {
match (
&self.git_input_selector.input_git_commit,
&self.git_input_selector.input_git_ref,
) {
(Some(commit), None) => Some(GitInputSelector::CommitId(commit.clone())),
(None, Some(git_ref)) => Some(GitInputSelector::Reference(git_ref.clone())),
(None, None) => None,
(Some(_), Some(_)) => {
panic!("IE: this should not be possible, Clap configuration is broken")
}
}
}
pub(crate) fn get_input_type(
&self,
settings: &Settings,
) -> Result<InputSettings, Box<dyn Error>> {
let git_selector = self.get_git_selector();
if let Some(filename) = &self.input_filename {
let i = FileInput {
path: filename.clone(),
};
Ok(InputSettings::File(i))
} else if self.input_fs_dir.is_some() {
let i = FsInput {
dir: self
.input_fs_dir
.clone()
.expect("IE: This should not be possible (Clap)"),
suffix: self
.input_fs_ext
.clone()
.expect("IE: This should not be possible (Clap)"),
};
Ok(InputSettings::Fs(i))
} else if self.input_git_repo.is_some() {
let i = GitInput {
repo: self.input_git_repo.clone().unwrap(/*:ok: is_some */),
git_ref: git_selector.expect("IE: This should not be possible (Clap)"),
dir: self
.input_git_dir
.clone()
.expect("IE: This should not be possible (Clap)"),
ext: String::from("txn"),
};
Ok(InputSettings::Git(i))
} else if self.input_git_repo.is_none() && git_selector.is_some() {
match settings.get_input_settings(
Some(&config::StorageType::STORAGE_GIT.to_string()),
Some(self.conf_path.as_ref().unwrap().as_path()),
)? {
InputSettings::Git(git) => Ok(InputSettings::Git(GitInput {
git_ref: git_selector.unwrap(/*:ok: is_some */),
..git
})),
_ => {
let msg = "CLI Arg handling: Internal logic error";
Err(msg.into())
}
}
} else {
settings.get_input_settings(
self.input_storage.as_ref(),
Some(self.conf_path.as_ref().unwrap().as_path()),
)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
}
}