Skip to content

Commit 144b321

Browse files
committed
refactor: replace structopt with clap
Signed-off-by: Yaroslav Bolyukin <[email protected]>
1 parent 8d4a7a1 commit 144b321

File tree

13 files changed

+482
-382
lines changed

13 files changed

+482
-382
lines changed

Cargo.lock

+354-329
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmds/fleet/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ age = { version = "0.7.0", features = ["ssh", "armor"] }
2020
base64 = "0.13.0"
2121
chrono = { version = "0.4.19", features = ["serde"] }
2222
z85 = "3.0.3"
23-
structopt = "0.3.23"
23+
clap = { version = "3.1.0", features = ["derive", "env", "wrap_help", "unicode"] }
2424
tokio = { version = "1.14.0", features = ["full"] }
2525
tracing = "0.1.29"
2626
tracing-subscriber = { version = "0.3.3", features = ["fmt", "env-filter"] }

cmds/fleet/src/cmds/build_systems.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@ use std::{env::current_dir, time::Duration};
22

33
use crate::{command::CommandExt, host::Config};
44
use anyhow::Result;
5-
use structopt::StructOpt;
5+
use clap::Parser;
66
use tokio::{process::Command, task::LocalSet, time::sleep};
77
use tracing::{error, field, info, info_span, warn, Instrument};
88

9-
#[derive(StructOpt, Clone)]
9+
#[derive(Parser, Clone)]
1010
pub struct BuildSystems {
11-
/// --builders arg for nix
12-
#[structopt(long)]
13-
builders: Option<String>,
1411
/// Jobs to run locally
15-
#[structopt(long)]
12+
#[clap(long)]
1613
jobs: Option<usize>,
1714
/// Do not continue on error
18-
#[structopt(long)]
15+
#[clap(long)]
1916
fail_fast: bool,
20-
#[structopt(long)]
17+
/// Run builds as sudo
18+
#[clap(long)]
2119
privileged_build: bool,
22-
#[structopt(subcommand)]
20+
#[clap(subcommand)]
2321
subcommand: Subcommand,
22+
23+
/// --builders arg for nix
24+
#[clap(long)]
25+
builders: Option<String>,
26+
/// --show-trace arg for nix
2427
#[structopt(long)]
2528
show_trace: bool,
2629
}
@@ -65,7 +68,7 @@ impl From<Subcommand> for Action {
6568
}
6669
}
6770

68-
#[derive(StructOpt, Clone)]
71+
#[derive(Parser, Clone)]
6972
enum Subcommand {
7073
/// Upload, but do not switch
7174
Upload,

cmds/fleet/src/cmds/info.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ use std::collections::BTreeSet;
22

33
use crate::host::Config;
44
use anyhow::{ensure, Result};
5-
use structopt::StructOpt;
5+
use clap::Parser;
66

7-
#[derive(StructOpt)]
7+
#[derive(Parser)]
88
pub struct Info {
9-
#[structopt(long)]
9+
#[clap(long)]
1010
json: bool,
11-
#[structopt(subcommand)]
11+
#[clap(subcommand)]
1212
cmd: InfoCmd,
1313
}
1414

15-
#[derive(StructOpt)]
15+
#[derive(Parser)]
1616
pub enum InfoCmd {
1717
/// List hosts
1818
ListHosts {
19-
#[structopt(long)]
19+
#[clap(long)]
2020
tagged: Vec<String>,
2121
},
2222
/// List ips
2323
HostIps {
2424
host: String,
25-
#[structopt(long)]
25+
#[clap(long)]
2626
external: bool,
27-
#[structopt(long)]
27+
#[clap(long)]
2828
internal: bool,
2929
},
3030
}

cmds/fleet/src/cmds/secrets/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use crate::{
33
host::Config,
44
};
55
use anyhow::{bail, Result};
6+
use clap::Parser;
67
use futures::{StreamExt, TryStreamExt};
78
use std::{
89
io::{self, Cursor, Read},
910
path::PathBuf,
1011
};
11-
use structopt::StructOpt;
1212

13-
#[derive(StructOpt)]
13+
#[derive(Parser)]
1414
pub enum Secrets {
1515
/// Force load keys for all defined hosts
1616
ForceKeys,
@@ -21,11 +21,11 @@ pub enum Secrets {
2121
/// Secret owners
2222
machines: Vec<String>,
2323
/// Override secret if already present
24-
#[structopt(long)]
24+
#[clap(long)]
2525
force: bool,
26-
#[structopt(long)]
26+
#[clap(long)]
2727
public: Option<String>,
28-
#[structopt(long)]
28+
#[clap(long)]
2929
public_file: Option<PathBuf>,
3030
},
3131
/// Add secret, data should be provided in stdin
@@ -35,11 +35,11 @@ pub enum Secrets {
3535
/// Secret owners
3636
machine: String,
3737
/// Override secret if already present
38-
#[structopt(long)]
38+
#[clap(long)]
3939
force: bool,
40-
#[structopt(long)]
40+
#[clap(long)]
4141
public: Option<String>,
42-
#[structopt(long)]
42+
#[clap(long)]
4343
public_file: Option<PathBuf>,
4444
},
4545
}

cmds/fleet/src/command.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl<'de> Deserialize<'de> for LogField {
6363

6464
#[derive(Deserialize, Debug)]
6565
#[serde(rename_all = "camelCase", tag = "action")]
66+
#[allow(dead_code)]
6667
enum NixLog {
6768
Msg {
6869
level: u32,

cmds/fleet/src/host.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use std::{
88
};
99

1010
use anyhow::Result;
11+
use clap::{ArgGroup, Parser};
1112
use serde::de::DeserializeOwned;
12-
use structopt::clap::ArgGroup;
13-
use structopt::StructOpt;
1413
use tokio::process::Command;
1514

1615
use crate::{command::CommandExt, fleetdata::FleetData};
@@ -121,22 +120,25 @@ impl Config {
121120
}
122121
}
123122

124-
#[derive(StructOpt, Clone)]
125-
#[structopt(group = ArgGroup::with_name("target_hosts"))]
123+
#[derive(Parser, Clone)]
124+
#[clap(group = ArgGroup::new("target_hosts"))]
126125
pub struct FleetOpts {
127126
/// All hosts except those would be skipped
128-
#[structopt(long, number_of_values = 1, group = "target_hosts")]
127+
#[clap(long, number_of_values = 1, group = "target_hosts")]
129128
only: Vec<String>,
130129

131130
/// Hosts to skip
132-
#[structopt(long, number_of_values = 1, group = "target_hosts")]
131+
#[clap(long, number_of_values = 1, group = "target_hosts")]
133132
skip: Vec<String>,
134133

135134
/// Host, which should be threaten as current machine
136-
#[structopt(long)]
135+
#[clap(long)]
137136
pub localhost: Option<String>,
138137

139-
#[structopt(long, default_value = "x86_64-linux")]
138+
// TODO: unhardcode x86_64-linux
139+
/// Override detected system for host, to perform builds via
140+
/// binfmt-declared qemu instead of trying to crosscompile
141+
#[clap(long, default_value = "x86_64-linux")]
140142
pub local_system: String,
141143
}
142144

cmds/fleet/src/main.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,30 @@ mod fleetdata;
88
use std::io;
99

1010
use anyhow::{anyhow, Result};
11-
use structopt::clap::AppSettings::*;
12-
use structopt::StructOpt;
11+
use clap::Parser;
1312

1413
use cmds::{build_systems::BuildSystems, info::Info, secrets::Secrets};
1514
use host::{Config, FleetOpts};
1615
use tracing::{info, metadata::LevelFilter};
1716
use tracing_subscriber::EnvFilter;
1817

19-
#[derive(StructOpt)]
18+
#[derive(Parser)]
2019
enum Opts {
2120
/// Prepare systems for deployments
2221
BuildSystems(BuildSystems),
2322
/// Secret management
23+
#[clap(subcommand)]
2424
Secrets(Secrets),
2525
/// Config parsing
2626
Info(Info),
2727
}
2828

29-
#[derive(StructOpt)]
30-
#[structopt(
31-
version = "1.0",
32-
author,
33-
global_setting(ColorAuto),
34-
global_setting(ColoredHelp)
35-
)]
29+
#[derive(Parser)]
30+
#[clap(version = "1.0", author)]
3631
struct RootOpts {
37-
#[structopt(flatten)]
32+
#[clap(flatten)]
3833
fleet_opts: FleetOpts,
39-
#[structopt(subcommand)]
34+
#[clap(subcommand)]
4035
command: Opts,
4136
}
4237

@@ -64,7 +59,7 @@ async fn main() -> Result<()> {
6459
.map_err(|e| anyhow!("Failed to initialize logger: {}", e))?;
6560

6661
info!("Starting");
67-
let opts = RootOpts::from_args();
62+
let opts = RootOpts::parse();
6863
let config = opts.fleet_opts.build()?;
6964

7065
match run_command(&config, opts.command).await {

cmds/install-secrets/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ log = "0.4.14"
1111
nix = "0.23.1"
1212
serde = "1.0.130"
1313
serde_json = "1.0.68"
14-
structopt = "0.3.23"
14+
clap = { version = "3.1.0", features = ["derive", "env", "wrap_help", "unicode"] }
1515
tempfile = "3.2.0"
1616
z85 = "3.0.3"

cmds/install-secrets/src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use age::Decryptor;
22
use anyhow::{anyhow, bail, Context, Result};
3+
use clap::Parser;
34
use log::{error, warn};
45
use nix::fcntl::{renameat2, RenameFlags};
56
use nix::sys::stat::Mode;
@@ -15,10 +16,9 @@ use std::{
1516
os::unix::fs::DirBuilderExt,
1617
path::{Path, PathBuf},
1718
};
18-
use structopt::StructOpt;
1919

20-
#[derive(StructOpt)]
21-
#[structopt(author)]
20+
#[derive(Parser)]
21+
#[clap(author)]
2222
struct Opts {
2323
data: PathBuf,
2424
}
@@ -132,7 +132,7 @@ fn main() -> anyhow::Result<()> {
132132
.filter_level(log::LevelFilter::Info)
133133
.init();
134134

135-
let opts = Opts::from_args();
135+
let opts = Opts::parse();
136136
let data = fs::read(&opts.data).context("failed to read secrets data")?;
137137
let data_str = from_utf8(&data).context("failed to read data to string")?;
138138
let data: Data = serde_json::from_str(data_str).context("failed to parse data")?;

flake.lock

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ pkgs: super:
22
with pkgs;
33
{
44
fleet-install-secrets = callPackage ./fleet-install-secrets.nix { };
5+
fleet = callPackage ./fleet.nix { };
56
}

pkgs/fleet.nix

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ rustPlatform }:
2+
3+
rustPlatform.buildRustPackage rec {
4+
pname = "fleet";
5+
version = "0.0.1";
6+
name = "${pname}-${version}";
7+
8+
src = ../.;
9+
cargoBuildFlags = "-p ${pname}";
10+
cargoLock = {
11+
lockFile = ../Cargo.lock;
12+
};
13+
}

0 commit comments

Comments
 (0)