feat(core): implement log command

This commit is contained in:
Federico Terzi 2021-03-29 21:10:30 +02:00
parent aa6853293a
commit 38fb74cdac
2 changed files with 128 additions and 72 deletions

55
espanso/src/cli/log.rs Normal file
View File

@ -0,0 +1,55 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* espanso is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::{fs::File, io::BufReader};
use std::io::BufRead;
use super::{CliModule, CliModuleArgs};
pub fn new() -> CliModule {
CliModule {
requires_paths: true,
subcommand: "log".to_string(),
entry: log_main,
..Default::default()
}
}
fn log_main(args: CliModuleArgs) {
let paths = args.paths.expect("missing paths argument");
let log_file = paths.runtime.join(crate::LOG_FILE_NAME);
if !log_file.exists() {
eprintln!("No log file found.");
std::process::exit(2);
}
let log_file = File::open(log_file);
if let Ok(log_file) = log_file {
let reader = BufReader::new(log_file);
for line in reader.lines() {
if let Ok(line) = line {
println!("{}", line);
}
}
} else {
eprintln!("Error reading log file");
std::process::exit(1);
}
}

View File

@ -36,6 +36,7 @@ const LOG_FILE_NAME: &str = "espanso.log";
lazy_static! {
static ref CLI_HANDLERS: Vec<CliModule> = vec![
cli::path::new(),
cli::log::new(),
];
}
@ -79,46 +80,46 @@ fn main() {
.short("v")
.multiple(true)
.help("Sets the level of verbosity"))
.subcommand(SubCommand::with_name("cmd")
.about("Send a command to the espanso daemon.")
.subcommand(SubCommand::with_name("exit")
.about("Terminate the daemon."))
.subcommand(SubCommand::with_name("enable")
.about("Enable the espanso replacement engine."))
.subcommand(SubCommand::with_name("disable")
.about("Disable the espanso replacement engine."))
.subcommand(SubCommand::with_name("toggle")
.about("Toggle the status of the espanso replacement engine."))
)
.subcommand(SubCommand::with_name("edit")
.about("Open the default text editor to edit config files and reload them automatically when exiting")
.arg(Arg::with_name("config")
.help("Defaults to \"default\". The configuration file name to edit (without the .yml extension)."))
.arg(Arg::with_name("norestart")
.short("n")
.long("norestart")
.required(false)
.takes_value(false)
.help("Avoid restarting espanso after editing the file"))
)
.subcommand(SubCommand::with_name("detect")
.about("Tool to detect current window properties, to simplify filters creation."))
.subcommand(SubCommand::with_name("daemon")
.about("Start the daemon without spawning a new process."))
.subcommand(SubCommand::with_name("register")
.about("MacOS and Linux only. Register espanso in the system daemon manager."))
.subcommand(SubCommand::with_name("unregister")
.about("MacOS and Linux only. Unregister espanso from the system daemon manager."))
// .subcommand(SubCommand::with_name("cmd")
// .about("Send a command to the espanso daemon.")
// .subcommand(SubCommand::with_name("exit")
// .about("Terminate the daemon."))
// .subcommand(SubCommand::with_name("enable")
// .about("Enable the espanso replacement engine."))
// .subcommand(SubCommand::with_name("disable")
// .about("Disable the espanso replacement engine."))
// .subcommand(SubCommand::with_name("toggle")
// .about("Toggle the status of the espanso replacement engine."))
// )
// .subcommand(SubCommand::with_name("edit")
// .about("Open the default text editor to edit config files and reload them automatically when exiting")
// .arg(Arg::with_name("config")
// .help("Defaults to \"default\". The configuration file name to edit (without the .yml extension)."))
// .arg(Arg::with_name("norestart")
// .short("n")
// .long("norestart")
// .required(false)
// .takes_value(false)
// .help("Avoid restarting espanso after editing the file"))
// )
// .subcommand(SubCommand::with_name("detect")
// .about("Tool to detect current window properties, to simplify filters creation."))
// .subcommand(SubCommand::with_name("daemon")
// .about("Start the daemon without spawning a new process."))
// .subcommand(SubCommand::with_name("register")
// .about("MacOS and Linux only. Register espanso in the system daemon manager."))
// .subcommand(SubCommand::with_name("unregister")
// .about("MacOS and Linux only. Unregister espanso from the system daemon manager."))
.subcommand(SubCommand::with_name("log")
.about("Print the latest daemon logs."))
.subcommand(SubCommand::with_name("start")
.about("Start the daemon spawning a new process in the background."))
.subcommand(SubCommand::with_name("stop")
.about("Stop the espanso daemon."))
.subcommand(SubCommand::with_name("restart")
.about("Restart the espanso daemon."))
.subcommand(SubCommand::with_name("status")
.about("Check if the espanso daemon is running or not."))
.about("Print the daemon logs."))
// .subcommand(SubCommand::with_name("start")
// .about("Start the daemon spawning a new process in the background."))
// .subcommand(SubCommand::with_name("stop")
// .about("Stop the espanso daemon."))
// .subcommand(SubCommand::with_name("restart")
// .about("Restart the espanso daemon."))
// .subcommand(SubCommand::with_name("status")
// .about("Check if the espanso daemon is running or not."))
.subcommand(SubCommand::with_name("path")
.about("Prints all the espanso directory paths to easily locate configuration and matches.")
.subcommand(SubCommand::with_name("config")
@ -135,39 +136,39 @@ fn main() {
.subcommand(SubCommand::with_name("base")
.about("Print the default match file path."))
)
.subcommand(SubCommand::with_name("match")
.about("List and execute matches from the CLI")
.subcommand(SubCommand::with_name("list")
.about("Print all matches to standard output")
.arg(Arg::with_name("json")
.short("j")
.long("json")
.help("Return the matches as json")
.required(false)
.takes_value(false)
)
.arg(Arg::with_name("onlytriggers")
.short("t")
.long("onlytriggers")
.help("Print only triggers without replacement")
.required(false)
.takes_value(false)
)
.arg(Arg::with_name("preservenewlines")
.short("n")
.long("preservenewlines")
.help("Preserve newlines when printing replacements")
.required(false)
.takes_value(false)
)
)
.subcommand(SubCommand::with_name("exec")
.about("Triggers the expansion of the given match")
.arg(Arg::with_name("trigger")
.help("The trigger of the match to be expanded")
)
)
)
// .subcommand(SubCommand::with_name("match")
// .about("List and execute matches from the CLI")
// .subcommand(SubCommand::with_name("list")
// .about("Print all matches to standard output")
// .arg(Arg::with_name("json")
// .short("j")
// .long("json")
// .help("Return the matches as json")
// .required(false)
// .takes_value(false)
// )
// .arg(Arg::with_name("onlytriggers")
// .short("t")
// .long("onlytriggers")
// .help("Print only triggers without replacement")
// .required(false)
// .takes_value(false)
// )
// .arg(Arg::with_name("preservenewlines")
// .short("n")
// .long("preservenewlines")
// .help("Preserve newlines when printing replacements")
// .required(false)
// .takes_value(false)
// )
// )
// .subcommand(SubCommand::with_name("exec")
// .about("Triggers the expansion of the given match")
// .arg(Arg::with_name("trigger")
// .help("The trigger of the match to be expanded")
// )
// )
// )
// Package manager
.subcommand(SubCommand::with_name("package")
.about("Espanso package manager commands")