Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve bool flags (#708) #709

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ trip www.example.com -m silent -v --log-format compact --log-span-events full
## Command Reference

```shell
A network diagnostic tool

Usage: trip [OPTIONS] [TARGETS]...

Arguments:
Expand All @@ -362,7 +364,7 @@ Options:
- stream: Display a continuous stream of tracing data
- pretty: Generate an pretty text table report for N cycles
- markdown: Generate a markdown text table report for N cycles
- csv: Generate a SCV report for N cycles
- csv: Generate a CSV report for N cycles
- json: Generate a JSON report for N cycles
- silent: Do not generate any tracing output for N cycles

Expand Down Expand Up @@ -458,12 +460,10 @@ Options:
--dns-timeout <DNS_TIMEOUT>
The maximum time to wait to perform DNS queries [default: 5s]

-z, --dns-lookup-as-info <DNS_LOOKUP_AS_INFO>
-z, --dns-lookup-as-info
Lookup autonomous system (AS) information during DNS queries [default:
false]

[possible values: true, false]

-a, --tui-address-mode <TUI_ADDRESS_MODE>
How to render addresses [default: host]

Expand Down Expand Up @@ -498,11 +498,9 @@ Options:
-s, --tui-max-samples <TUI_MAX_SAMPLES>
The maximum number of samples to record per hop [default: 256]

--tui-preserve-screen <TUI_PRESERVE_SCREEN>
--tui-preserve-screen
Preserve the screen on exit [default: false]

[possible values: true, false]

--tui-refresh-rate <TUI_REFRESH_RATE>
The Tui refresh rate [default: 100ms]

Expand Down
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl TryFrom<(Args, u16)> for TrippyConfig {
cfg_file_tui.tui_max_samples,
constants::DEFAULT_TUI_MAX_SAMPLES,
);
let tui_preserve_screen = cfg_layer(
let tui_preserve_screen = cfg_layer_bool_flag(
args.tui_preserve_screen,
cfg_file_tui.tui_preserve_screen,
constants::DEFAULT_TUI_PRESERVE_SCREEN,
Expand Down Expand Up @@ -381,7 +381,7 @@ impl TryFrom<(Args, u16)> for TrippyConfig {
cfg_file_dns.dns_resolve_method,
constants::DEFAULT_DNS_RESOLVE_METHOD,
);
let dns_lookup_as_info = cfg_layer(
let dns_lookup_as_info = cfg_layer_bool_flag(
args.dns_lookup_as_info,
cfg_file_dns.dns_lookup_as_info,
constants::DEFAULT_DNS_LOOKUP_AS_INFO,
Expand Down Expand Up @@ -555,6 +555,14 @@ fn cfg_layer_opt<T>(fst: Option<T>, snd: Option<T>) -> Option<T> {
}
}

fn cfg_layer_bool_flag(fst: bool, snd: Option<bool>, default: bool) -> bool {
match (fst, snd) {
(true, _) => true,
(false, Some(val)) => val,
(false, None) => default,
}
}

fn validate_logging(mode: Mode, verbose: bool) -> anyhow::Result<()> {
if matches!(mode, Mode::Tui) && verbose {
Err(anyhow!("cannot enable verbose logging in tui mode"))
Expand Down
102 changes: 46 additions & 56 deletions src/config/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,190 +17,180 @@ pub struct Args {
pub targets: Vec<String>,

/// Config file
#[arg(value_enum, short = 'c', long, display_order = 0, value_hint = clap::ValueHint::FilePath)]
#[arg(value_enum, short = 'c', long, value_hint = clap::ValueHint::FilePath)]
pub config_file: Option<String>,

/// Output mode [default: tui]
#[arg(value_enum, short = 'm', long, display_order = 1)]
#[arg(value_enum, short = 'm', long)]
pub mode: Option<Mode>,

/// Tracing protocol [default: icmp]
#[arg(value_enum, short = 'p', long, display_order = 2)]
#[arg(value_enum, short = 'p', long)]
pub protocol: Option<Protocol>,

/// Trace using the UDP protocol
#[arg(
long,
display_order = 3,
conflicts_with = "protocol",
conflicts_with = "tcp"
)]
#[arg(long, conflicts_with = "protocol", conflicts_with = "tcp")]
pub udp: bool,

/// Trace using the TCP protocol
#[arg(
long,
display_order = 4,
conflicts_with = "protocol",
conflicts_with = "udp"
)]
#[arg(long, conflicts_with = "protocol", conflicts_with = "udp")]
pub tcp: bool,

/// use IPv4 only
#[arg(short = '4', long, display_order = 5, conflicts_with = "ipv6")]
#[arg(short = '4', long, conflicts_with = "ipv6")]
pub ipv4: bool,

/// Use IPv6 only
#[arg(short = '6', long, display_order = 6, conflicts_with = "ipv4")]
#[arg(short = '6', long, conflicts_with = "ipv4")]
pub ipv6: bool,

/// The target port (TCP & UDP only) [default: 80]
#[arg(long, short = 'P', display_order = 7)]
#[arg(long, short = 'P')]
pub target_port: Option<u16>,

/// The source port (TCP & UDP only) [default: auto]
#[arg(long, short = 'S', display_order = 8)]
#[arg(long, short = 'S')]
pub source_port: Option<u16>,

/// The source IP address [default: auto]
#[arg(short = 'A', long, display_order = 9, conflicts_with = "interface")]
#[arg(short = 'A', long, conflicts_with = "interface")]
pub source_address: Option<String>,

/// The network interface [default: auto]
#[arg(short = 'I', long, display_order = 10)]
#[arg(short = 'I', long)]
pub interface: Option<String>,

/// The minimum duration of every round [default: 1s]
#[arg(short = 'i', long, display_order = 11)]
#[arg(short = 'i', long)]
pub min_round_duration: Option<String>,

/// The maximum duration of every round [default: 1s]
#[arg(short = 'T', long, display_order = 12)]
#[arg(short = 'T', long)]
pub max_round_duration: Option<String>,

/// The period of time to wait for additional ICMP responses after the target has responded
/// [default: 100ms]
#[arg(short = 'g', long, display_order = 13)]
#[arg(short = 'g', long)]
pub grace_duration: Option<String>,

/// The initial sequence number [default: 33000]
#[arg(long, display_order = 14)]
#[arg(long)]
pub initial_sequence: Option<u16>,

/// The Equal-cost Multi-Path routing strategy (IPv4/UDP only) [default: classic]
#[arg(value_enum, short = 'R', long, display_order = 15)]
#[arg(value_enum, short = 'R', long)]
pub multipath_strategy: Option<MultipathStrategyConfig>,

/// The maximum number of in-flight ICMP echo requests [default: 24]
#[arg(short = 'U', long, display_order = 16)]
#[arg(short = 'U', long)]
pub max_inflight: Option<u8>,

/// The TTL to start from [default: 1]
#[arg(short = 'f', long, display_order = 17)]
#[arg(short = 'f', long)]
pub first_ttl: Option<u8>,

/// The maximum number of TTL hops [default: 64]
#[arg(short = 't', long, display_order = 18)]
#[arg(short = 't', long)]
pub max_ttl: Option<u8>,

/// The size of IP packet to send (IP header + ICMP header + payload) [default: 84]
#[arg(long, display_order = 19)]
#[arg(long)]
pub packet_size: Option<u16>,

/// The repeating pattern in the payload of the ICMP packet [default: 0]
#[arg(long, display_order = 20)]
#[arg(long)]
pub payload_pattern: Option<u8>,

/// The TOS (i.e. DSCP+ECN) IP header value (TCP and UDP only) [default: 0]
#[arg(short = 'Q', long, display_order = 21)]
#[arg(short = 'Q', long)]
pub tos: Option<u8>,

/// The socket read timeout [default: 10ms]
#[arg(long, display_order = 22)]
#[arg(long)]
pub read_timeout: Option<String>,

/// How to perform DNS queries [default: system]
#[arg(value_enum, short = 'r', long, display_order = 23)]
#[arg(value_enum, short = 'r', long)]
pub dns_resolve_method: Option<DnsResolveMethod>,

/// The maximum time to wait to perform DNS queries [default: 5s]
#[arg(long, display_order = 24)]
#[arg(long)]
pub dns_timeout: Option<String>,

/// Lookup autonomous system (AS) information during DNS queries [default: false]
#[arg(long, short = 'z', display_order = 25)]
pub dns_lookup_as_info: Option<bool>,
#[arg(long, short = 'z')]
pub dns_lookup_as_info: bool,

/// How to render addresses [default: host]
#[arg(value_enum, short = 'a', long, display_order = 26)]
#[arg(value_enum, short = 'a', long)]
pub tui_address_mode: Option<AddressMode>,

/// How to render AS information [default: asn]
#[arg(value_enum, long, display_order = 27)]
#[arg(value_enum, long)]
pub tui_as_mode: Option<AsMode>,

/// How to render GeoIp information [default: short]
#[arg(value_enum, long, display_order = 28)]
#[arg(value_enum, long)]
pub tui_geoip_mode: Option<GeoIpMode>,

/// The maximum number of addresses to show per hop [default: auto]
#[arg(short = 'M', long, display_order = 29)]
#[arg(short = 'M', long)]
pub tui_max_addrs: Option<u8>,

/// The maximum number of samples to record per hop [default: 256]
#[arg(long, short = 's', display_order = 30)]
#[arg(long, short = 's')]
pub tui_max_samples: Option<usize>,

/// Preserve the screen on exit [default: false]
#[arg(long, display_order = 31)]
pub tui_preserve_screen: Option<bool>,
#[arg(long)]
pub tui_preserve_screen: bool,

/// The Tui refresh rate [default: 100ms]
#[arg(long, display_order = 32)]
#[arg(long)]
pub tui_refresh_rate: Option<String>,

/// The TUI theme colors [item=color,item=color,..]
#[arg(long, value_delimiter(','), value_parser = parse_tui_theme_color_value, display_order = 33)]
#[arg(long, value_delimiter(','), value_parser = parse_tui_theme_color_value)]
pub tui_theme_colors: Vec<(TuiThemeItem, TuiColor)>,

/// Print all TUI theme items and exit
#[arg(long, display_order = 34)]
#[arg(long)]
pub print_tui_theme_items: bool,

/// The TUI key bindings [command=key,command=key,..]
#[arg(long, value_delimiter(','), value_parser = parse_tui_binding_value, display_order = 35)]
#[arg(long, value_delimiter(','), value_parser = parse_tui_binding_value)]
pub tui_key_bindings: Vec<(TuiCommandItem, TuiKeyBinding)>,

/// Print all TUI commands that can be bound and exit
#[arg(long, display_order = 36)]
#[arg(long)]
pub print_tui_binding_commands: bool,

/// The number of report cycles to run [default: 10]
#[arg(short = 'C', long, display_order = 37)]
#[arg(short = 'C', long)]
pub report_cycles: Option<usize>,

/// The MaxMind City GeoLite2 mmdb file
#[arg(short = 'G', long, display_order = 38, value_hint = clap::ValueHint::FilePath)]
#[arg(short = 'G', long, value_hint = clap::ValueHint::FilePath)]
pub geoip_mmdb_file: Option<String>,

/// Generate shell completion
#[arg(long, display_order = 39)]
#[arg(long)]
pub generate: Option<Shell>,

/// The debug log format [default: pretty]
#[arg(long, display_order = 40)]
#[arg(long)]
pub log_format: Option<LogFormat>,

/// The debug log filter [default: trippy=debug]
#[arg(long, display_order = 41)]
#[arg(long)]
pub log_filter: Option<String>,

/// The debug log format [default: off]
#[arg(long, display_order = 42)]
#[arg(long)]
pub log_span_events: Option<LogSpanEvents>,

/// Enable verbose debug logging
#[arg(short = 'v', long, default_value_t = false, display_order = 43)]
#[arg(short = 'v', long, default_value_t = false)]
pub verbose: bool,
}

Expand Down
Loading