Skip to content

Commit

Permalink
Merge pull request #247 from kpcyrd/clap-4
Browse files Browse the repository at this point in the history
Port to clap 4
  • Loading branch information
kpcyrd authored Aug 31, 2023
2 parents 1eeb150 + 88cb8b5 commit fe588f3
Show file tree
Hide file tree
Showing 44 changed files with 1,088 additions and 1,013 deletions.
1,377 changes: 769 additions & 608 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ rustyline = "10.0"
log = "0.4"
env_logger = "0.9"
hlua-badtouch = "0.4"
structopt = "0.3"
clap = { version = "4.3.11", features = ["derive", "env"] }
clap_complete = "4.3.2"
failure = "0.1"
rand = "0.8"
colored = "2"
Expand Down
9 changes: 4 additions & 5 deletions examples/boxxy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[macro_use] extern crate boxxy;
extern crate sn0int;
#[macro_use]
extern crate boxxy;
extern crate env_logger;
extern crate sn0int;

fn stage1(sh: &mut boxxy::Shell, _args: Vec<String>) -> Result<(), boxxy::Error> {
shprintln!(sh, "[*] starting stage1");
Expand All @@ -14,8 +15,6 @@ fn main() {

println!("stage1 activate sandbox");

let toolbox = boxxy::Toolbox::new().with(vec![
("stage1", stage1),
]);
let toolbox = boxxy::Toolbox::new().with(vec![("stage1", stage1)]);
boxxy::Shell::new(toolbox).run()
}
15 changes: 7 additions & 8 deletions examples/maxmind.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use clap::Parser;
use sn0int::errors::*;
use sn0int::geoip::{AsnDB, GeoIP, Maxmind};
use sn0int::paths;
use std::net::IpAddr;
use std::path::Path;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub enum Args {
#[structopt(name="asn")]
#[command(name = "asn")]
Asn(AsnArgs),
#[structopt(name="geoip")]
#[command(name = "geoip")]
GeoIP(GeoIPArgs),
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct AsnArgs {
ip: IpAddr,
}
Expand All @@ -30,7 +30,7 @@ impl AsnArgs {
}
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct GeoIPArgs {
ip: IpAddr,
}
Expand All @@ -47,9 +47,8 @@ impl GeoIPArgs {
}
}


fn run() -> Result<()> {
let args = Args::from_args();
let args = Args::parse();
debug!("{:?}", args);
let cache_dir = paths::cache_dir()?;
match args {
Expand Down
22 changes: 10 additions & 12 deletions examples/spinners.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use clap::Parser;
use sn0int::term::{Spinner, StackedSpinners, SPINNERS};
use std::thread;
use std::time::Duration;
use sn0int::term::{SPINNERS, Spinner, StackedSpinners};
use structopt::StructOpt;


#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub enum Args {
#[structopt(name="single")]
#[command(name = "single")]
Single(Single),
#[structopt(name="stacked")]
#[command(name = "stacked")]
Stacked(Stacked),
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Single {
idx: usize,
#[structopt(long="ticks", default_value="100")]
#[structopt(long = "ticks", default_value = "100")]
ticks: usize,
}

Expand All @@ -32,9 +31,8 @@ impl Single {
}
}

#[derive(Debug, StructOpt)]
pub struct Stacked {
}
#[derive(Debug, Parser)]
pub struct Stacked {}

impl Stacked {
fn run(&self) {
Expand All @@ -59,7 +57,7 @@ impl Stacked {
}

fn main() {
let args = Args::from_args();
let args = Args::parse();
match args {
Args::Single(args) => args.run(),
Args::Stacked(args) => args.run(),
Expand Down
1 change: 1 addition & 0 deletions sn0int-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ serde = { version = "1.0", features=["derive"] }
rocket_failure_errors = "0.2"
anyhow = "1.0"
nom = "7.0"
clap = { version = "4.3.11", features = ["derive"] }
2 changes: 1 addition & 1 deletion sn0int-common/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn token(s: &str) -> nom::IResult<&str, &str> {
nom::bytes::complete::take_while1(valid_char)(s)
}

#[derive(Debug, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ModuleID {
pub author: String,
pub name: String,
Expand Down
3 changes: 2 additions & 1 deletion sn0int-common/src/metadata/stealth.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use clap::ValueEnum;
use crate::errors::*;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Debug, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
#[derive(Debug, Eq, PartialEq, PartialOrd, Clone, ValueEnum, Serialize, Deserialize)]
pub enum Stealth {
Loud,
Normal,
Expand Down
118 changes: 66 additions & 52 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use clap::{CommandFactory, Parser};
use clap_complete::Shell;
use crate::cmd;
use crate::errors::*;
use crate::options;
use crate::workspaces::Workspace;
use structopt::StructOpt;
use structopt::clap::{AppSettings, Shell};
use sn0int_common::ModuleID;
use std::io;

#[derive(Debug, StructOpt)]
#[structopt(global_settings = &[AppSettings::ColoredHelp])]
#[derive(Debug, Parser)]
#[command(version)]
pub struct Args {
/// Select a different workspace instead of the default
#[structopt(short="w", long="workspace", env="SN0INT_WORKSPACE")]
#[arg(short = 'w', long="workspace", env="SN0INT_WORKSPACE")]
pub workspace: Option<Workspace>,

#[structopt(subcommand)]
#[command(subcommand)]
pub subcommand: Option<SubCommand>,
}

Expand All @@ -22,49 +24,49 @@ impl Args {
}
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub enum SubCommand {
/// Run a module directly
#[structopt(name="run")]
#[command(name="run")]
Run(Run),
/// For internal use
#[structopt(name="sandbox")]
#[command(name="sandbox")]
Sandbox(Sandbox),
/// Login to the registry for publishing
#[structopt(name="login")]
#[command(name="login")]
Login(Login),
/// Create a new module
#[structopt(name="new")]
#[command(name="new")]
New(New),
/// Publish a script to the registry
#[structopt(name="publish")]
#[command(name="publish")]
Publish(Publish),
/// Install a module from the registry
#[structopt(name="install")]
#[command(name="install")]
Install(Install),
/// Search in the registry
#[structopt(name="search")]
#[command(name="search")]
Search(Search),
/// The sn0int package manager
#[structopt(name="pkg")]
#[command(name="pkg")]
Pkg(cmd::pkg_cmd::Args),
/// Insert into the database
#[structopt(name="add")]
#[command(name="add")]
Add(cmd::add_cmd::Args),
/// Select from the database
#[structopt(name="select")]
#[command(name="select")]
Select(cmd::select_cmd::Args),
/// Delete from the database
#[structopt(name="delete")]
#[command(name="delete")]
Delete(cmd::delete_cmd::Args),
/// Query logged activity
#[structopt(name="activity")]
#[command(name="activity")]
Activity(cmd::activity_cmd::Args),
/// Include entities in the scope
#[structopt(name="scope")]
#[command(name="scope")]
Scope(cmd::scope_cmd::Args),
/// Exclude entities from scope
#[structopt(name="noscope")]
#[command(name="noscope")]
Noscope(cmd::noscope_cmd::Args),
/// Manage autoscope rules
Autoscope(cmd::autoscope_cmd::Args),
Expand All @@ -73,111 +75,123 @@ pub enum SubCommand {
/// Rescope all entities based on autonoscope rules
Rescope(cmd::rescope_cmd::Args),
/// Manage workspaces
#[structopt(name="workspace")]
#[command(name="workspace")]
Workspace(cmd::workspace_cmd::Args),
/// Calendar
#[structopt(name="cal")]
#[command(name="cal")]
Cal(cmd::cal_cmd::Args),
/// Notify
#[structopt(name="notify")]
#[command(name="notify")]
Notify(cmd::notify_cmd::Args),
/// Verify blob storage for corrupt and dangling blobs
#[structopt(name="fsck")]
#[command(name="fsck")]
Fsck(cmd::fsck_cmd::Args),
/// Export a workspace for external processing
#[structopt(name="export")]
#[command(name="export")]
Export(cmd::export_cmd::Args),
/// Show statistics about your current workspace
#[structopt(name="stats")]
#[command(name="stats")]
Stats(cmd::stats_cmd::Args),
/// Run a lua repl
#[structopt(name="repl")]
#[command(name="repl")]
Repl,
/// Show paths of various file system locations
#[structopt(name="paths")]
#[command(name="paths")]
Paths,
/// Generate shell completions
#[structopt(name="completions")]
#[command(name="completions")]
Completions(Completions),
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Run {
#[structopt(flatten)]
#[command(flatten)]
pub run: cmd::run_cmd::Args,
/// Run a module from a path
#[structopt(short="f", long="file")]
#[arg(short = 'f', long="file")]
pub file: bool,
/// Expose stdin to modules
#[structopt(long="stdin")]
#[arg(long="stdin")]
pub stdin: bool,
/// Automatically grant access to a keyring namespace
#[structopt(long="grant")]
#[arg(long="grant")]
pub grants: Vec<String>,
/// Automatically grant access to all requested keys
#[structopt(long="grant-full-keyring")]
#[arg(long="grant-full-keyring")]
pub grant_full_keyring: bool,
/// Automatically deny access to all requested keys
#[structopt(long="deny-keyring")]
#[arg(long="deny-keyring")]
pub deny_keyring: bool,
/// Exit on first error and set exit code
#[structopt(short="x", long="exit-on-error")]
#[arg(short = 'x', long="exit-on-error")]
pub exit_on_error: bool,
/// Set an option
#[structopt(short="o", long="option")]
#[arg(short = 'o', long="option")]
pub options: Vec<options::Opt>,
/// Narrow down targeted entities
#[structopt(short="t", long="target")]
#[arg(short = 't', long="target")]
pub target: Option<String>,
/// Dump the sandbox init message to stdout instead of running a child process
#[structopt(long="dump-sandbox-init-msg")]
#[arg(long="dump-sandbox-init-msg")]
pub dump_sandbox_init_msg: bool,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Sandbox {
/// This value is only used for process listings
_label: String,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Login {
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct New {
/// Path to the new file
pub path: String,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Publish {
/// The scripts to publish
#[structopt(required = true)]
#[arg(required = true)]
pub paths: Vec<String>,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Install {
/// The script to install
pub module: ModuleID,
/// Specify the version, defaults to the latest version
pub version: Option<String>,
#[structopt(short="f", long="force")]
#[arg(short = 'f', long="force")]
pub force: bool,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Search {
/// Only show modules that aren't installed yet
#[structopt(long="new")]
#[arg(long="new")]
pub new: bool,
/// The search query
pub query: String,
}

#[derive(Debug, StructOpt)]
/// Generate shell completions
#[derive(Debug, Parser)]
pub struct Completions {
#[structopt(possible_values=&Shell::variants())]
pub shell: Shell,
}

impl Completions {
pub fn generate(&self) -> Result<()> {
clap_complete::generate(
self.shell,
&mut Args::command(),
"sn0int",
&mut io::stdout(),
);
Ok(())
}
}
Loading

0 comments on commit fe588f3

Please sign in to comment.