refactor: pass parsed Commands from main
Some checks failed
Test / build (map[name:nightly]) (push) Successful in 1m30s
Test / build (map[name:stable]) (push) Successful in 2m2s
Release Please / Release-plz (push) Failing after 18s

This commit is contained in:
Paul Campbell 2024-12-15 20:07:34 +00:00
parent 6fe7be191f
commit 9e414d8947
14 changed files with 36 additions and 28 deletions

View file

@ -30,14 +30,14 @@ use kxio::kxprintln as p;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())] #[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())]
struct Commands { pub struct Commands {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
log: bool, pub log: bool,
#[clap(subcommand)] #[clap(subcommand)]
command: Command, pub command: Command,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
enum Command { pub enum Command {
/// Initialize the configuration /// Initialize the configuration
#[command(about = "Initialize configuration")] #[command(about = "Initialize configuration")]
Init, Init,
@ -95,8 +95,7 @@ impl FullCtx {
} }
#[cfg_attr(test, mutants::skip)] #[cfg_attr(test, mutants::skip)]
pub async fn run(ctx: &Ctx) -> color_eyre::Result<()> { pub async fn run(ctx: &Ctx, commands: &Commands) -> color_eyre::Result<()> {
let commands = Commands::parse();
if commands.log { if commands.log {
tracing::subscriber::set_global_default( tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder() tracing_subscriber::FmtSubscriber::builder()

View file

@ -1,11 +1,12 @@
// //
use clap::Parser;
use color_eyre::Result; use color_eyre::Result;
use trello_to_deck::{run, Ctx}; use trello_to_deck::{run, Commands, Ctx};
#[tokio::main] #[tokio::main]
#[cfg_attr(test, mutants::skip)] #[cfg_attr(test, mutants::skip)]
async fn main() -> Result<()> { async fn main() -> Result<()> {
color_eyre::install()?; color_eyre::install()?;
run(&Ctx::from(std::env::current_dir()?)).await run(&Ctx::from(std::env::current_dir()?), &Commands::parse()).await
} }

View file

@ -9,7 +9,7 @@ use crate::{
}; };
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum NextcloudCardCommand { pub enum NextcloudCardCommand {
List { List {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,

View file

@ -5,7 +5,7 @@ use crate::execute::Execute;
use crate::{p, FullCtx}; use crate::{p, FullCtx};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum NextcloudDeckCommand { pub enum NextcloudDeckCommand {
Get { Get {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,

View file

@ -29,7 +29,7 @@ pub(crate) struct DeckClient<'ctx> {
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum NextcloudCommand { pub enum NextcloudCommand {
#[clap(subcommand)] #[clap(subcommand)]
Deck(NextcloudDeckCommand), Deck(NextcloudDeckCommand),
#[clap(subcommand)] #[clap(subcommand)]

View file

@ -5,7 +5,7 @@ use crate::execute::Execute;
use crate::{p, FullCtx}; use crate::{p, FullCtx};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum NextcloudStackCommand { pub enum NextcloudStackCommand {
List { List {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,

View file

@ -1,15 +1,21 @@
//
use super::*; use super::*;
use crate::{Command, Commands};
use test_log::test; use test_log::test;
#[test] #[test(tokio::test)]
fn when_file_does_not_exist_should_create() { async fn when_file_does_not_exist_should_create() {
//given //given
let fs = given::a_filesystem(); let fs = given::a_filesystem();
let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer()); let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer());
let commands = Commands {
log: false,
command: Command::Init,
};
//when //when
let_assert!(Ok(_) = run(&ctx)); let_assert!(Ok(_) = crate::run(&ctx, &commands).await);
//then //then
let path = ctx.fs.base().join(f!("{NAME}.toml")); let path = ctx.fs.base().join(f!("{NAME}.toml"));
@ -18,17 +24,21 @@ fn when_file_does_not_exist_should_create() {
assert_eq!(contents, include_str!("../default-config.toml")); assert_eq!(contents, include_str!("../default-config.toml"));
} }
#[test] #[test(tokio::test)]
fn when_file_exists_should_err() { async fn when_file_exists_should_err() {
//given //given
let fs = given::a_filesystem(); let fs = given::a_filesystem();
let path = fs.base().join(f!("{NAME}.toml")); let path = fs.base().join(f!("{NAME}.toml"));
let file = fs.file(&path); let file = fs.file(&path);
file.write("").expect("create file"); file.write("").expect("create file");
let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer()); let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer());
let commands = Commands {
log: false,
command: Command::Init,
};
//when //when
let_assert!(Err(err) = run(&ctx)); let_assert!(Err(err) = crate::run(&ctx, &commands).await);
//then //then
assert!(err assert!(err

View file

@ -6,9 +6,7 @@ use std::collections::HashMap;
use assert2::let_assert; use assert2::let_assert;
use pretty_assertions::assert_eq as assert_peq; use pretty_assertions::assert_eq as assert_peq;
use crate::{ use crate::{config::AppConfig, f, nextcloud::NextcloudConfig, s, trello::TrelloConfig, Ctx, NAME};
config::AppConfig, f, init::run, nextcloud::NextcloudConfig, s, trello::TrelloConfig, Ctx, NAME,
};
mod api_result; mod api_result;
mod config; mod config;

View file

@ -9,7 +9,7 @@ use crate::{execute::Execute, p, FullCtx};
use super::model::{TrelloAttachmentId, TrelloCardId}; use super::model::{TrelloAttachmentId, TrelloCardId};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum TrelloAttachmentCommand { pub enum TrelloAttachmentCommand {
Get { Get {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,

View file

@ -7,7 +7,7 @@ use crate::{p, FullCtx};
use super::model::TrelloBoardId; use super::model::TrelloBoardId;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum TrelloBoardCommand { pub enum TrelloBoardCommand {
Get { Get {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,

View file

@ -7,7 +7,7 @@ use crate::{execute::Execute, p, FullCtx};
use super::model::TrelloCardId; use super::model::TrelloCardId;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum TrelloCardCommand { pub enum TrelloCardCommand {
Get { Get {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,

View file

@ -5,7 +5,7 @@ use crate::execute::Execute;
use crate::{p, FullCtx}; use crate::{p, FullCtx};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum TrelloMemberCommand { pub enum TrelloMemberCommand {
Get { Get {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,

View file

@ -29,7 +29,7 @@ pub(crate) mod stack;
mod tests; mod tests;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum TrelloCommand { pub enum TrelloCommand {
#[clap(subcommand)] #[clap(subcommand)]
Member(TrelloMemberCommand), Member(TrelloMemberCommand),

View file

@ -7,7 +7,7 @@ use crate::{execute::Execute, p, FullCtx};
use super::model::TrelloListId; use super::model::TrelloListId;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub(crate) enum TrelloStackCommand { pub enum TrelloStackCommand {
Get { Get {
#[clap(long, action = clap::ArgAction::SetTrue)] #[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool, dump: bool,