-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
266 lines (235 loc) · 7.71 KB
/
mod.rs
File metadata and controls
266 lines (235 loc) · 7.71 KB
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
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{
generate,
shells::{Bash, Fish, Zsh},
};
use std::path::PathBuf;
use crate::core::{
commands,
types::{CacheEncoding, OutputFormat},
};
use crate::utils::app_config::AppConfig;
use crate::utils::error::Result;
use crate::utils::types::LogLevel;
#[derive(Parser, Debug)]
#[command(
name = "codeinput",
author,
about,
long_about = "code input CLI",
version
)]
//TODO: #[clap(setting = AppSettings::SubcommandRequired)]
//TODO: #[clap(global_setting(AppSettings::DeriveDisplayOrder))]
pub struct Cli {
/// Set a custom config file
/// TODO: parse(from_os_str)
#[arg(short, long, value_name = "FILE")]
pub config: Option<PathBuf>,
/// Set a custom config file
#[arg(name = "debug", short, long = "debug", value_name = "DEBUG")]
pub debug: Option<bool>,
/// Set Log Level
#[arg(
name = "log_level",
short,
long = "log-level",
value_name = "LOG_LEVEL"
)]
pub log_level: Option<LogLevel>,
/// Subcommands
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
#[clap(
name = "codeowners",
about = "Manage and analyze CODEOWNERS files",
long_about = "Tools for parsing, validating and querying CODEOWNERS files"
)]
Codeowners {
#[clap(subcommand)]
subcommand: CodeownersSubcommand,
},
#[clap(
name = "completion",
about = "Generate completion scripts",
long_about = None,
)]
Completion {
#[clap(subcommand)]
subcommand: CompletionSubcommand,
},
#[clap(
name = "config",
about = "Show Configuration",
long_about = None,
)]
Config,
}
#[derive(Subcommand, PartialEq, Debug)]
enum CompletionSubcommand {
#[clap(about = "generate the autocompletion script for bash")]
Bash,
#[clap(about = "generate the autocompletion script for zsh")]
Zsh,
#[clap(about = "generate the autocompletion script for fish")]
Fish,
}
#[derive(Subcommand, PartialEq, Debug)]
enum CodeownersSubcommand {
#[clap(
name = "parse",
about = "Preprocess CODEOWNERS files and build ownership map"
)]
Parse {
/// Directory path to analyze (default: current directory)
#[arg(default_value = ".")]
path: PathBuf,
/// Custom cache file location
#[arg(long, value_name = "FILE", default_value = ".codeowners.cache")]
cache_file: Option<PathBuf>,
/// Output format: json|bincode
#[arg(long, value_name = "FORMAT", default_value = "bincode", value_parser = parse_cache_encoding)]
format: CacheEncoding,
},
#[clap(
name = "list-files",
about = "Find and list files with their owners based on filter criteria"
)]
ListFiles {
/// Directory path to analyze (default: current directory)
#[arg(default_value = ".")]
path: Option<PathBuf>,
/// Only show files with specified tags
#[arg(long, value_name = "LIST")]
tags: Option<String>,
/// Only show files owned by these owners
#[arg(long, value_name = "LIST")]
owners: Option<String>,
/// Show only unowned files
#[arg(long)]
unowned: bool,
/// Show all files including unowned/untagged
#[arg(long)]
show_all: bool,
/// Output format: text|json|bincode
#[arg(long, value_name = "FORMAT", default_value = "text", value_parser = parse_output_format)]
format: OutputFormat,
/// Custom cache file location
#[arg(long, value_name = "FILE", default_value = ".codeowners.cache")]
cache_file: Option<PathBuf>,
},
#[clap(
name = "list-owners",
about = "Display aggregated owner statistics and associations"
)]
ListOwners {
/// Directory path to analyze (default: current directory)
#[arg(default_value = ".")]
path: Option<PathBuf>,
/// Output format: text|json|bincode
#[arg(long, value_name = "FORMAT", default_value = "text", value_parser = parse_output_format)]
format: OutputFormat,
/// Custom cache file location
#[arg(long, value_name = "FILE", default_value = ".codeowners.cache")]
cache_file: Option<PathBuf>,
},
#[clap(
name = "list-tags",
about = "Audit and analyze tag usage across CODEOWNERS files"
)]
ListTags {
/// Directory path to analyze (default: current directory)
#[arg(default_value = ".")]
path: Option<PathBuf>,
/// Output format: text|json|bincode
#[arg(long, value_name = "FORMAT", default_value = "text", value_parser = parse_output_format)]
format: OutputFormat,
/// Custom cache file location
#[arg(long, value_name = "FILE", default_value = ".codeowners.cache")]
cache_file: Option<PathBuf>,
},
}
pub fn cli_match() -> Result<()> {
// Parse the command line arguments
let cli = Cli::parse();
// Merge clap config file if the value is set
AppConfig::merge_config(cli.config.as_deref())?;
let app = Cli::command();
let matches = app.get_matches();
AppConfig::merge_args(matches)?;
// Execute the subcommand
match &cli.command {
Commands::Codeowners { subcommand } => codeowners(subcommand)?,
Commands::Completion { subcommand } => {
let mut app = Cli::command();
match subcommand {
CompletionSubcommand::Bash => {
generate(Bash, &mut app, "codeinput", &mut std::io::stdout());
}
CompletionSubcommand::Zsh => {
generate(Zsh, &mut app, "codeinput", &mut std::io::stdout());
}
CompletionSubcommand::Fish => {
generate(Fish, &mut app, "codeinput", &mut std::io::stdout());
}
}
}
Commands::Config => commands::config()?,
}
Ok(())
}
/// Handle codeowners subcommands
pub(crate) fn codeowners(subcommand: &CodeownersSubcommand) -> Result<()> {
match subcommand {
CodeownersSubcommand::Parse {
path,
cache_file,
format,
} => commands::codeowners_parse(path, cache_file.as_deref(), *format),
CodeownersSubcommand::ListFiles {
path,
tags,
owners,
unowned,
show_all,
format,
cache_file,
} => commands::codeowners_list_files(
path.as_deref(),
tags.as_deref(),
owners.as_deref(),
*unowned,
*show_all,
format,
cache_file.as_deref(),
),
CodeownersSubcommand::ListOwners {
path,
format,
cache_file,
} => commands::codeowners_list_owners(path.as_deref(), format, cache_file.as_deref()),
CodeownersSubcommand::ListTags {
path,
format,
cache_file,
} => commands::codeowners_list_tags(path.as_deref(), format, cache_file.as_deref()),
}
}
fn parse_output_format(s: &str) -> std::result::Result<OutputFormat, String> {
match s.to_lowercase().as_str() {
"text" => Ok(OutputFormat::Text),
"json" => Ok(OutputFormat::Json),
"bincode" => Ok(OutputFormat::Bincode),
_ => Err(format!("Invalid output format: {}", s)),
}
}
fn parse_cache_encoding(s: &str) -> std::result::Result<CacheEncoding, String> {
match s.to_lowercase().as_str() {
"bincode" => Ok(CacheEncoding::Bincode),
"json" => Ok(CacheEncoding::Json),
_ => Err(format!("Invalid cache encoding: {}", s)),
}
}