dotfiles/src/cli/mod.rs
Mutzi 0fc89e6e40
All checks were successful
Gitea Organization/dotfiles/pipeline/head This commit looks good
Added a cli and arg parsing
2023-02-14 17:23:22 +01:00

69 lines
2.2 KiB
Rust

use bpaf::{command, construct, Parser};
use crate::common::config::ModToml;
use crate::common::{operations, repository};
use crate::common::operations::NamedModule;
#[derive(Debug, Clone)]
pub enum CliCommand {
Install(Vec<String>),
Collect(Vec<String>),
Upload(String),
List(),
Pull(),
Diff()
}
pub fn get_parser() -> impl Parser<CliCommand> {
let modules = bpaf::positional::<String>("Modules").many();
let install =
command("install", construct!(CliCommand::Install(modules)).to_options())
.help("Install modules");
let modules = bpaf::positional::<String>("Modules").many();
let collect =
command("collect", construct!(CliCommand::Collect(modules)).to_options())
.help("Collect modules");
let commit_msg = bpaf::positional::<String>("Commit message");
let upload =
command("upload", construct!(CliCommand::Upload(commit_msg)).to_options())
.help("Upload files");
let list = command("list", construct!(CliCommand::List()).to_options())
.help("List modules");
let pull = command("pull", construct!(CliCommand::Pull()).to_options())
.help("Downloads update");
let diff = command("diff", construct!(CliCommand::Diff()).to_options())
.help("Show diff of current changes");
construct!([
install,
collect,
upload,
list,
pull,
diff
])
}
pub fn run(config: &ModToml, command: CliCommand) {
let map_mod = |name: String| -> NamedModule {
match config.modules.get(&name) {
None => { println!("Module {name} doesn't exist"); std::process::exit(1); }
Some(v) => NamedModule(name, v.clone())
}
};
match command {
CliCommand::Install(mods) => operations::install(&mods.into_iter().map(map_mod).collect::<Vec<NamedModule>>()),
CliCommand::Collect(mods) => operations::collect(&mods.into_iter().map(map_mod).collect::<Vec<NamedModule>>()),
CliCommand::Upload(msg) => operations::upload(|| msg),
CliCommand::List() => config.modules.iter().for_each(|m| println!("Module {}", m.0)),
CliCommand::Pull() => repository::pull_repo(),
CliCommand::Diff() => repository::diff()
}
}