mod commands;
mod errors;
mod output;
use crate::errors::Error;
use clap::{Parser, Subcommand};
use output::{ConsoleOutput, DebugOutput, Output};
#[derive(Parser)]
#[command(arg_required_else_help = true)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Init,
Meta,
Track {
file_name: String,
#[arg(short = 'f')]
forced: bool,
},
Preview,
Klick,
Log,
Revert { name: String },
#[cfg(debug_assertions)]
Debug,
Register { username: String, email: String },
}
fn main() {
let cli = Cli::parse();
let mut output = ConsoleOutput::new();
let exit_status: Result<(), Error> = match &cli.command {
Some(Commands::Init) => commands::initialise(&mut output, None),
Some(Commands::Meta) => commands::meta(&mut output, None),
Some(Commands::Track { file_name, forced }) => {
commands::track(file_name, forced, &mut output, None)
}
Some(Commands::Preview) => commands::preview(&mut output, None),
Some(Commands::Klick) => commands::snapshot(None),
Some(Commands::Log) => commands::log(&mut output, None),
Some(Commands::Revert { name }) => commands::revert(&mut output, name.to_owned(), None),
#[cfg(debug_assertions)]
Some(Commands::Debug) => commands::debug_meta(&mut output, None),
Some(Commands::Register { username, email }) => commands::register(username, email),
None => {
unreachable!();
}
};
match exit_status {
Ok(()) => {
output.print();
0
}
Err(ref e) => {
let mut output = DebugOutput::new();
e.handle(&mut output);
match output.print() {
Some(error_output) => {
for line in error_output {
eprintln!("{}", line);
}
}
None => {
eprintln!("An error occurred")
}
};
1
}
};
}