git.delta.rocks / jrsonnet / refs/commits / bb34d421e2a0

difftreelog

refactor replace clap with structopt

Yaroslav Bolyukin2021-09-30parent: #b47cfbf.patch.diff
in: trunk

6 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -327,44 +327,12 @@
  "atty",
  "bitflags",
  "strsim 0.8.0",
- "textwrap 0.11.0",
- "unicode-width",
- "vec_map",
-]
-
-[[package]]
-name = "clap"
-version = "3.0.0-beta.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142"
-dependencies = [
- "atty",
- "bitflags",
- "clap_derive",
- "indexmap",
- "lazy_static",
- "os_str_bytes",
- "strsim 0.10.0",
- "termcolor",
- "textwrap 0.12.1",
+ "textwrap",
  "unicode-width",
  "vec_map",
 ]
 
 [[package]]
-name = "clap_derive"
-version = "3.0.0-beta.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1"
-dependencies = [
- "heck",
- "proc-macro-error",
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
 name = "cookie-factory"
 version = "0.3.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -478,7 +446,6 @@
  "anyhow",
  "base64",
  "chrono",
- "clap 3.0.0-beta.2",
  "env_logger",
  "hostname",
  "log",
@@ -487,6 +454,7 @@
  "peg",
  "serde",
  "serde_json",
+ "structopt",
  "tempfile",
  "time 0.3.2",
  "z85",
@@ -597,12 +565,6 @@
 ]
 
 [[package]]
-name = "hashbrown"
-version = "0.9.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
-
-[[package]]
 name = "heck"
 version = "0.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -726,16 +688,6 @@
 ]
 
 [[package]]
-name = "indexmap"
-version = "1.6.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2"
-dependencies = [
- "autocfg 1.0.1",
- "hashbrown",
-]
-
-[[package]]
 name = "instant"
 version = "0.1.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -955,12 +907,6 @@
 checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
 
 [[package]]
-name = "os_str_bytes"
-version = "2.3.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2ac6fe3538f701e339953a3ebbe4f39941aababa8a3f6964635b24ab526daeac"
-
-[[package]]
 name = "ouroboros"
 version = "0.9.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1154,9 +1100,9 @@
 
 [[package]]
 name = "rand"
-version = "0.8.3"
+version = "0.8.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
+checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
 dependencies = [
  "libc",
  "rand_chacha 0.3.0",
@@ -1458,7 +1404,7 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bf9d950ef167e25e0bdb073cf1d68e9ad2795ac826f2f3f59647817cf23c0bfa"
 dependencies = [
- "clap 2.33.3",
+ "clap",
  "lazy_static",
  "structopt-derive",
 ]
@@ -1519,7 +1465,7 @@
 dependencies = [
  "cfg-if",
  "libc",
- "rand 0.8.3",
+ "rand 0.8.4",
  "redox_syscall",
  "remove_dir_all",
  "winapi",
@@ -1539,15 +1485,6 @@
 version = "0.11.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
-dependencies = [
- "unicode-width",
-]
-
-[[package]]
-name = "textwrap"
-version = "0.12.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789"
 dependencies = [
  "unicode-width",
 ]
modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,7 +7,6 @@
 
 [dependencies]
 anyhow = "1.0"
-clap = { version = "3.0.0-beta.2", features = ["derive", "suggestions", "color"] }
 log = "0.4.14"
 env_logger = "0.9.0"
 serde = { version = "1.0", features = ["derive"] }
@@ -23,6 +22,7 @@
 base64 = "0.13.0"
 chrono = { version = "0.4.19", features = ["serde"] }
 z85 = "3.0.3"
+structopt = "0.3.23"
 
 [workspace]
 members = ["crates/nixlike", "cmds/install-secrets"]
modifiedsrc/cmds/build_systems.rsdiffbeforeafterboth
before · src/cmds/build_systems.rs
1use std::process::Command;23use crate::{command::CommandExt, host::Config, nix::SYSTEMS_ATTRIBUTE};4use anyhow::Result;5use clap::Clap;6use log::info;78#[derive(Clap)]9#[clap(group = clap::ArgGroup::new("target"))]10pub struct BuildSystems {11	/// --builders arg for nix12	#[clap(long)]13	builders: Option<String>,14	/// Jobs to run locally15	#[clap(long)]16	jobs: Option<usize>,17	/// Do not continue on error18	#[clap(long)]19	fail_fast: bool,20	#[clap(long)]21	privileged_build: bool,22	#[clap(subcommand)]23	subcommand: Option<Subcommand>,24}2526#[derive(Clap)]27enum Subcommand {28	/// Switch to built system until reboot29	Test,30	/// Switch to built system after reboot31	Boot,32	/// test + boot33	Switch,34}35impl Subcommand {36	fn should_switch_profile(&self) -> bool {37		matches!(self, Self::Test | Self::Switch)38	}39	fn name(&self) -> &'static str {40		match self {41			Self::Test => "test",42			Self::Boot => "boot",43			Self::Switch => "switch",44		}45	}46}4748impl BuildSystems {49	pub fn run(self, config: &Config) -> Result<()> {50		let hosts = config.list_hosts()?;5152		for host in hosts.iter() {53			if config.should_skip(host) {54				continue;55			}56			info!("Building host {}", host);57			let built = {58				let dir = tempfile::tempdir()?;59				dir.path().to_owned()60			};6162			let mut nix_build = if self.privileged_build {63				let mut out = Command::new("sudo");64				out.arg("nix");65				out66			} else {67				Command::new("nix")68			};69			nix_build70				.args(&["build", "--impure", "--no-link", "--out-link"])71				.arg(&built)72				.arg(format!(73					"{}.{}.config.system.build.toplevel",74					SYSTEMS_ATTRIBUTE, host,75				));7677			if let Some(builders) = &self.builders {78				nix_build.arg("--builders").arg(builders);79			}80			if let Some(jobs) = &self.jobs {81				nix_build.arg("--max-jobs");82				nix_build.arg(format!("{}", jobs));83			}84			if !self.fail_fast {85				nix_build.arg("--keep-going");86			}8788			nix_build.inherit_stdio().run()?;89			let built = std::fs::canonicalize(built)?;90			info!("Built closure: {:?}", built);91			if !config.is_local(host) {92				info!("Uploading system closure");93				Command::new("nix")94					.args(&["copy", "--to"])95					.arg(format!("ssh://root@{}", host))96					.arg(&built)97					.inherit_stdio()98					.run()?;99			}100			if let Some(subcommand) = &self.subcommand {101				if subcommand.should_switch_profile() {102					info!("Switching generation");103					config104						.command_on(host, "nix-env", true)105						.args(&["-p", "/nix/var/nix/profiles/system", "--set"])106						.arg(&built)107						.inherit_stdio()108						.run()?;109				}110				info!("Executing activation script");111				let mut switch_script = built.clone();112				switch_script.push("bin");113				switch_script.push("switch-to-configuration");114				config115					.command_on(host, switch_script, true)116					.arg(subcommand.name())117					.inherit_stdio()118					.run()?;119			}120		}121		Ok(())122	}123}
modifiedsrc/cmds/secrets/mod.rsdiffbeforeafterboth
--- a/src/cmds/secrets/mod.rs
+++ b/src/cmds/secrets/mod.rs
@@ -1,9 +1,9 @@
 use crate::{fleetdata::FleetSecret, host::Config};
 use anyhow::{bail, Result};
-use clap::Clap;
 use std::io::{self, Cursor, Read};
+use structopt::StructOpt;
 
-#[derive(Clap)]
+#[derive(StructOpt)]
 pub enum Secrets {
 	/// Force load keys for all defined hosts
 	ForceKeys,
@@ -14,9 +14,9 @@
 		/// Secret owners
 		machines: Vec<String>,
 		/// Override secret if already present
-		#[clap(long)]
+		#[structopt(long)]
 		force: bool,
-		#[clap(long)]
+		#[structopt(long)]
 		public: Option<String>,
 	},
 }
modifiedsrc/host.rsdiffbeforeafterboth
--- a/src/host.rs
+++ b/src/host.rs
@@ -9,7 +9,8 @@
 };
 
 use anyhow::Result;
-use clap::Clap;
+use structopt::clap::ArgGroup;
+use structopt::StructOpt;
 
 use crate::{command::CommandExt, fleetdata::FleetData};
 
@@ -102,19 +103,19 @@
 	}
 }
 
-#[derive(Clap, Clone)]
-#[clap(group = clap::ArgGroup::new("target_hosts"))]
+#[derive(StructOpt, Clone)]
+#[structopt(group = ArgGroup::with_name("target_hosts"))]
 pub struct FleetOpts {
 	/// All hosts except those would be skipped
-	#[clap(long, number_of_values = 1, group = "target_hosts")]
+	#[structopt(long, number_of_values = 1, group = "target_hosts")]
 	only: Vec<String>,
 
 	/// Hosts to skip
-	#[clap(long, number_of_values = 1, group = "target_hosts")]
+	#[structopt(long, number_of_values = 1, group = "target_hosts")]
 	skip: Vec<String>,
 
 	/// Host, which should be threaten as current machine
-	#[clap(long)]
+	#[structopt(long)]
 	pub localhost: Option<String>,
 }
 
modifiedsrc/main.rsdiffbeforeafterboth
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,13 +8,13 @@
 mod fleetdata;
 
 use anyhow::Result;
-use clap::Clap;
+use structopt::StructOpt;
 
 use cmds::{build_systems::BuildSystems, secrets::Secrets};
 use host::{Config, FleetOpts};
 
-#[derive(Clap)]
-#[clap(version = "1.0", author = "CertainLach <iam@lach.pw>")]
+#[derive(StructOpt)]
+#[structopt(version = "1.0", author = "CertainLach <iam@lach.pw>")]
 enum Opts {
 	/// Prepare systems for deployments
 	BuildSystems(BuildSystems),
@@ -22,11 +22,11 @@
 	Secrets(Secrets),
 }
 
-#[derive(Clap)]
+#[derive(StructOpt)]
 struct RootOpts {
-	#[clap(flatten)]
+	#[structopt(flatten)]
 	fleet_opts: FleetOpts,
-	#[clap(subcommand)]
+	#[structopt(subcommand)]
 	command: Opts,
 }
 
@@ -42,7 +42,7 @@
 	env_logger::Builder::new()
 		.filter_level(log::LevelFilter::Info)
 		.init();
-	let opts = RootOpts::parse();
+	let opts = RootOpts::from_args();
 	let config = opts.fleet_opts.build()?;
 
 	match run_command(&config, opts.command) {