pub mod entry;
pub mod merge;
pub mod resolve;
pub mod serve;
pub mod verify;
use crate::core::model::Row;
use clap::{Parser, Subcommand, ValueEnum, ValueHint};
use std::path::PathBuf;
use crate::store::file::DEFAULT_HOSTS_PATH;
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
pub enum Shell {
Bash,
Zsh,
Fish,
}
#[derive(Parser, Debug)]
#[command(name = "hostab", version, about, long_about = None)]
pub struct Cli {
#[arg(long, env = "HOSTS_FILE", default_value = DEFAULT_HOSTS_PATH, value_hint = ValueHint::FilePath, global = true)]
pub hosts_file: PathBuf,
#[arg(short, long, global = true)]
pub quiet: bool,
#[arg(short = 'o', long, default_value = "table", global = true)]
pub out: String,
#[arg(long, global = true)]
pub no_color: bool,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
#[command(subcommand, alias = "e")]
Entry(EntryCommands),
Verify {
#[arg(long)]
strict: bool,
},
Cat,
Resolve {
hosts: Vec<String>,
#[arg(short, long)]
local: bool,
},
Merge {
#[arg(short, long, required = true, value_hint = ValueHint::FilePath)]
src: Vec<String>,
#[arg(short, long, value_hint = ValueHint::FilePath)]
target: Option<PathBuf>,
},
Completion {
shell: Shell,
},
Version,
Serve {
#[arg(short, long, default_value = "3456")]
port: u16,
#[arg(long, default_value = "127.0.0.1")]
bind: String,
#[arg(long)]
no_docs: bool,
},
}
#[derive(Subcommand, Debug)]
pub enum EntryCommands {
List {
#[arg(long)]
ipv4: bool,
#[arg(long)]
ipv6: bool,
#[arg(short = 'f', long = "filter")]
pattern: Option<String>,
#[arg(short = 'i', long)]
ignore_case: bool,
},
Add {
ip: String,
hosts: Vec<String>,
#[arg(long)]
canonical: Option<String>,
#[arg(long)]
alias: Vec<String>,
#[arg(long)]
comment: Option<String>,
},
Rm {
hosts: Vec<String>,
#[arg(long)]
ip: Option<String>,
},
Disable {
hosts: Vec<String>,
#[arg(long)]
ip: Option<String>,
},
Enable {
hosts: Vec<String>,
#[arg(long)]
ip: Option<String>,
},
Toggle {
host: String,
#[arg(long)]
ip: Option<String>,
},
Edit {
host: String,
#[arg(long)]
ip: String,
},
}
pub fn print_output(cli: &Cli, rows: &[Row]) {
if cli.quiet {
return;
}
if cli.no_color {
std::env::set_var("NO_COLOR", "1");
}
let output_str = match cli.out.as_str() {
"json" => crate::output::json::format_json_rows(rows),
"raw" => crate::output::table::format_raw(rows),
"markdown" => crate::output::table::format_markdown(rows),
_ => crate::output::table::format_table(rows),
};
println!("{}", output_str);
}