Skip to content

Commit

Permalink
feat(user): add a log command in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
qjerome committed Oct 15, 2024
1 parent b6813a9 commit 5c95311
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion kunai/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::cmp::max;
use std::collections::{HashMap, HashSet, VecDeque};

use std::fs::{self, DirBuilder, File};
use std::io::{self, BufRead, Write};
use std::io::{self, BufRead, BufReader, Write};
use std::net::IpAddr;

use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt};
Expand Down Expand Up @@ -2380,6 +2380,14 @@ struct InstallOpt {
enable_unit: bool,
}

#[derive(Debug, Args)]
struct LogsOpt {
/// Where to write the configuration file. Any intermediate directory
/// will be created if needed.
#[arg(short, long, default_value_t = String::from("/etc/kunai/config.yaml"))]
config: String,
}

#[derive(Debug, Subcommand)]
enum Command {
/// Install Kunai on the system
Expand All @@ -2390,6 +2398,9 @@ enum Command {
Replay(ReplayOpt),
/// Dump a default configuration
Config(ConfigOpt),
/// Easy way to show kunai logs. This will work only with a configuration file and with an output
/// file being configured.
Logs(LogsOpt),
}

impl Command {
Expand Down Expand Up @@ -2804,6 +2815,38 @@ WantedBy=sysinit.target"#,

Self::systemd_install(&co, &install_bin, &config_path)
}

fn logs(o: LogsOpt) -> anyhow::Result<()> {
let config: Config = serde_yaml::from_reader(
File::open(o.config).map_err(|e| anyhow!("failed to read config file: {e}"))?,
)
.map_err(|e| anyhow!("failed to parse config file: {e}"))?;

let output = PathBuf::from(config.output);
if !output.is_file() {
return Err(anyhow!(
"kunai output={} is not a regular file",
output.to_string_lossy()
));
}

// for the time being kunai does not allow specifying custom
// log storage options so we can fix them
let fd = firo::OpenOptions::new()
.compression(firo::Compression::Gzip)
.open(&output)?;

let reader = BufReader::new(fd);

for line in reader.lines() {
println!(
"{}",
line.map_err(|e| anyhow!("failed to read log file:{e}"))?
);
}

Ok(())
}
}

fn main() -> Result<(), anyhow::Error> {
Expand Down Expand Up @@ -2864,6 +2907,7 @@ fn main() -> Result<(), anyhow::Error> {
Some(Command::Install(o)) => Command::install(o),
Some(Command::Config(o)) => Command::config(o),
Some(Command::Replay(o)) => Command::replay(o),
Some(Command::Logs(o)) => Command::logs(o),
Some(Command::Run(o)) => Command::run(Some(o), verifier_level),
None => Command::run(None, verifier_level),
}
Expand Down

0 comments on commit 5c95311

Please sign in to comment.