Skip to content

Commit

Permalink
SubCommand::Ctx with client check
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Nov 15, 2024
1 parent efe2bf2 commit 19435d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/kamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,25 @@ pub(super) fn run() -> Result<()> {
),
};

let mut output = std::io::stdout();
let command = kamp
.subcommand
.unwrap_or_else(|| SubCommand::Ctx(Default::default()));

let Some(command) = kamp.subcommand else {
return if session.is_none() {
Err(Error::InvalidContext("session is required"))
} else {
[session, client]
.into_iter()
.zip(["session", "client"])
.try_for_each(|(opt, name)| match opt {
Some(val) => writeln!(output, "{name}: {val}"),
None => Ok(()),
})
.map_err(From::from)
};
};
let mut output = std::io::stdout();

match command {
SubCommand::Init(opt) => cmd::init(opt.export, opt.alias)
.and_then(|res| write!(output, "{res}").map_err(From::from)),
SubCommand::Ctx(opt) => {
let Some(session) = session else {
return Err(Error::InvalidContext("session is required"));
};
match (opt.client, client) {
(true, None) => Err(Error::InvalidContext("client is required")),
(true, Some(client)) => writeln!(output, "client: {client}").map_err(From::from),
(false, _) => writeln!(output, "session: {session}").map_err(From::from),
}
}
SubCommand::List(opt) if opt.all => {
let sessions = kak::list_sessions()?;
let sessions = String::from_utf8(sessions).map_err(anyhow::Error::new)?;
Expand Down
13 changes: 13 additions & 0 deletions src/kamp/argv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(super) enum SubCommand {
List(list::Options),
Get(get::Options),
Cat(cat::Options),
Ctx(ctx::Options),
}

pub(super) mod init {
Expand Down Expand Up @@ -260,3 +261,15 @@ mod cat {
pub buffers: Vec<String>,
}
}

mod ctx {
use super::*;
/// print session context
#[derive(FromArgs, PartialEq, Debug, Default)]
#[argh(subcommand, name = "ctx")]
pub struct Options {
/// print client if any otherwise throw an error
#[argh(switch, short = 'c')]
pub client: bool,
}
}

0 comments on commit 19435d1

Please sign in to comment.