Add `rad-self` command

Signed-off-by: xphoniex <xphoniex@users.noreply.github.com>
This commit is contained in:
xphoniex 2022-11-08 11:05:25 +01:00 committed by Alexis Sellier
parent c7ba5a7b3f
commit c829045b8d
No known key found for this signature in database
4 changed files with 142 additions and 13 deletions

View File

@ -1,4 +1,10 @@
pub mod auth; #[path = "commands/auth.rs"]
pub mod checkout; pub mod rad_auth;
pub mod help; #[path = "commands/checkout.rs"]
pub mod init; pub mod rad_checkout;
#[path = "commands/help.rs"]
pub mod rad_help;
#[path = "commands/init.rs"]
pub mod rad_init;
#[path = "commands/self.rs"]
pub mod rad_self;

View File

@ -3,9 +3,10 @@ use std::ffi::OsString;
use crate::terminal as term; use crate::terminal as term;
use crate::terminal::args::{Args, Error, Help}; use crate::terminal::args::{Args, Error, Help};
use super::auth as rad_auth; use super::rad_auth;
use super::checkout as rad_checkout; use super::rad_checkout;
use super::init as rad_init; use super::rad_init;
use super::rad_self;
pub const HELP: Help = Help { pub const HELP: Help = Help {
name: "help", name: "help",
@ -14,7 +15,13 @@ pub const HELP: Help = Help {
usage: "Usage: rad help [--help]", usage: "Usage: rad help [--help]",
}; };
const COMMANDS: &[Help] = &[rad_auth::HELP, rad_init::HELP, rad_checkout::HELP, HELP]; const COMMANDS: &[Help] = &[
rad_auth::HELP,
rad_init::HELP,
rad_checkout::HELP,
rad_self::HELP,
HELP,
];
#[derive(Default)] #[derive(Default)]
pub struct Options {} pub struct Options {}

View File

@ -0,0 +1,113 @@
use std::ffi::OsString;
use radicle::crypto::ssh;
use radicle::Profile;
use crate::terminal as term;
use crate::terminal::args::{Args, Error, Help};
pub const HELP: Help = Help {
name: "self",
description: "Show information about your radicle identity and device",
version: env!("CARGO_PKG_VERSION"),
usage: r#"
Usage
rad self [<option>...]
Options
--profile Show Profile ID
--help Show help
"#,
};
#[derive(Debug)]
enum Show {
Profile,
All,
}
#[derive(Debug)]
pub struct Options {
show: Show,
}
impl Args for Options {
fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
use lexopt::prelude::*;
let mut parser = lexopt::Parser::from_args(args);
let mut show: Option<Show> = None;
while let Some(arg) = parser.next()? {
match arg {
Long("profile") if show.is_none() => {
show = Some(Show::Profile);
}
Long("help") => {
return Err(Error::Help.into());
}
_ => return Err(anyhow::anyhow!(arg.unexpected())),
}
}
Ok((
Options {
show: show.unwrap_or(Show::All),
},
vec![],
))
}
}
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?;
match options.show {
Show::Profile => {
term::print(profile.id());
}
Show::All => all(&profile)?,
}
Ok(())
}
fn all(profile: &Profile) -> anyhow::Result<()> {
let mut table = term::Table::default();
let node_id = profile.id();
table.push([String::from("ID"), term::format::tertiary(node_id)]);
let ssh_short = ssh::fmt::fingerprint(node_id);
table.push([
String::from("Key (hash)"),
term::format::tertiary(ssh_short),
]);
let ssh_long = ssh::fmt::key(node_id);
table.push([String::from("Key (full)"), term::format::tertiary(ssh_long)]);
let storage_path = profile.paths().storage();
table.push([
String::from("Storage (git)"),
term::format::tertiary(storage_path.display()),
]);
let keys_path = profile.paths().keys();
table.push([
String::from("Storage (keys)"),
term::format::tertiary(keys_path.display()),
]);
let node_path = profile.paths().node();
table.push([
String::from("Node (socket)"),
term::format::tertiary(node_path.join("radicle.sock").display()),
]);
table.render();
Ok(())
}

View File

@ -6,11 +6,6 @@ use anyhow::anyhow;
use radicle_cli::commands::*; use radicle_cli::commands::*;
use radicle_cli::terminal as term; use radicle_cli::terminal as term;
use auth as rad_auth;
use checkout as rad_checkout;
use help as rad_help;
use init as rad_init;
pub const NAME: &str = "rad"; pub const NAME: &str = "rad";
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const DESCRIPTION: &str = "Radicle command line interface"; pub const DESCRIPTION: &str = "Radicle command line interface";
@ -139,6 +134,14 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
args.to_vec(), args.to_vec(),
); );
} }
"self" => {
term::run_command_args::<rad_self::Options, _>(
rad_self::HELP,
"Command",
rad_self::run,
args.to_vec(),
);
}
_ => { _ => {
let exe = format!("{}-{}", NAME, exe); let exe = format!("{}-{}", NAME, exe);
let status = process::Command::new(exe.clone()).args(args).status(); let status = process::Command::new(exe.clone()).args(args).status();