|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use clap::builder::styling; |
| 4 | +use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum}; |
| 5 | +use colored::Colorize; |
| 6 | + |
| 7 | +use crate::predicate::PredicateArgs; |
| 8 | + |
| 9 | +#[derive(Debug, Parser)] |
| 10 | +#[command( |
| 11 | + name = "ljx", |
| 12 | + version, |
| 13 | + about = "Offline toolbox for .logjet files", |
| 14 | + long_about = None |
| 15 | +)] |
| 16 | +pub struct Cli { |
| 17 | + #[command(subcommand)] |
| 18 | + pub command: Command, |
| 19 | +} |
| 20 | + |
| 21 | +pub fn build_cli() -> clap::Command { |
| 22 | + let appname = "ljx"; |
| 23 | + let styles = styling::Styles::styled() |
| 24 | + .header(styling::AnsiColor::Yellow.on_default()) |
| 25 | + .usage(styling::AnsiColor::Yellow.on_default()) |
| 26 | + .literal(styling::AnsiColor::BrightGreen.on_default()) |
| 27 | + .placeholder(styling::AnsiColor::BrightMagenta.on_default()); |
| 28 | + |
| 29 | + Cli::command() |
| 30 | + .styles(styles) |
| 31 | + .about(format!( |
| 32 | + "{} - {}", |
| 33 | + appname.bright_magenta().bold(), |
| 34 | + "offline toolbox for .logjet streams" |
| 35 | + )) |
| 36 | + .override_usage(format!( |
| 37 | + "{appname} <COMMAND> [OPTIONS] [ARGS]" |
| 38 | + )) |
| 39 | + .after_help( |
| 40 | + "Examples:\n ljx count telemetry.logjet -F error -i\n ljx filter telemetry.logjet -o only-logs.logjet -e 'java\\..*\\.bs'", |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Debug, Subcommand)] |
| 45 | +pub enum Command { |
| 46 | + Split(SplitArgs), |
| 47 | + Join(JoinArgs), |
| 48 | + Filter(FilterArgs), |
| 49 | + Count(CountArgs), |
| 50 | + Stats(StatsArgs), |
| 51 | + Cat(CatArgs), |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Debug, Clone, Args)] |
| 55 | +pub struct CountArgs { |
| 56 | + #[arg(value_name = "INPUT", help = "Input .logjet file or - for stdin")] |
| 57 | + pub input: PathBuf, |
| 58 | + |
| 59 | + #[command(flatten)] |
| 60 | + pub predicate: PredicateArgs, |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Debug, Clone, Args)] |
| 64 | +pub struct FilterArgs { |
| 65 | + #[arg(value_name = "INPUT", help = "Input .logjet file or - for stdin")] |
| 66 | + pub input: PathBuf, |
| 67 | + |
| 68 | + #[arg( |
| 69 | + short, |
| 70 | + long, |
| 71 | + value_name = "OUTPUT", |
| 72 | + help = "Output .logjet file or - for stdout" |
| 73 | + )] |
| 74 | + pub output: PathBuf, |
| 75 | + |
| 76 | + #[arg(long, value_enum, default_value_t = OutputCodec::Lz4)] |
| 77 | + pub codec: OutputCodec, |
| 78 | + |
| 79 | + #[arg( |
| 80 | + long, |
| 81 | + default_value_t = logjet::DEFAULT_BLOCK_TARGET_SIZE, |
| 82 | + help = "Target uncompressed bytes per output block" |
| 83 | + )] |
| 84 | + pub block_target_size: usize, |
| 85 | + |
| 86 | + #[command(flatten)] |
| 87 | + pub predicate: PredicateArgs, |
| 88 | +} |
| 89 | + |
| 90 | +#[derive(Debug, Clone, Args)] |
| 91 | +pub struct SplitArgs { |
| 92 | + #[arg(value_name = "INPUT")] |
| 93 | + pub input: PathBuf, |
| 94 | + |
| 95 | + #[arg(value_name = "OUTPUT_PREFIX")] |
| 96 | + pub output_prefix: PathBuf, |
| 97 | + |
| 98 | + #[arg(long)] |
| 99 | + pub max_bytes: Option<u64>, |
| 100 | + |
| 101 | + #[arg(long)] |
| 102 | + pub max_records: Option<u64>, |
| 103 | + |
| 104 | + #[arg(long, help = "RFC 3339 or unix-ns range support to be added")] |
| 105 | + pub timestamp_range: Option<String>, |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Debug, Clone, Args)] |
| 109 | +pub struct JoinArgs { |
| 110 | + #[arg(value_name = "INPUT", required = true)] |
| 111 | + pub inputs: Vec<PathBuf>, |
| 112 | + |
| 113 | + #[arg(short, long, value_name = "OUTPUT")] |
| 114 | + pub output: PathBuf, |
| 115 | + |
| 116 | + #[arg(long)] |
| 117 | + pub validate_sequence_continuity: bool, |
| 118 | +} |
| 119 | + |
| 120 | +#[derive(Debug, Clone, Args)] |
| 121 | +pub struct StatsArgs { |
| 122 | + #[arg(value_name = "INPUT")] |
| 123 | + pub input: PathBuf, |
| 124 | + |
| 125 | + #[arg(long, help = "Compute payload size summaries by record type")] |
| 126 | + pub field_stats: bool, |
| 127 | +} |
| 128 | + |
| 129 | +#[derive(Debug, Clone, Args)] |
| 130 | +pub struct CatArgs { |
| 131 | + #[arg(value_name = "INPUT")] |
| 132 | + pub input: PathBuf, |
| 133 | + |
| 134 | + #[arg(long, default_value_t = false)] |
| 135 | + pub hex_payload: bool, |
| 136 | +} |
| 137 | + |
| 138 | +#[derive(Debug, Clone, Copy, ValueEnum)] |
| 139 | +pub enum OutputCodec { |
| 140 | + None, |
| 141 | + Lz4, |
| 142 | +} |
| 143 | + |
| 144 | +impl From<OutputCodec> for logjet::Codec { |
| 145 | + fn from(value: OutputCodec) -> Self { |
| 146 | + match value { |
| 147 | + OutputCodec::None => Self::None, |
| 148 | + OutputCodec::Lz4 => Self::Lz4, |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments