From 5522e9c40a6b2beb1cfea51e90351b90fada5931 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 2 Oct 2022 10:50:35 +0300 Subject: refactor: use clap instead of structopt clap version 3 basically replaces structopt entirely. Sponsored-by: author --- src/bin/obnam-server.rs | 9 ++++----- src/bin/obnam.rs | 14 +++++++------- src/cmd/backup.rs | 8 ++++---- src/cmd/chunk.rs | 12 +++--------- src/cmd/chunkify.rs | 4 ++-- src/cmd/gen_info.rs | 5 ++--- src/cmd/get_chunk.rs | 5 ++--- src/cmd/init.rs | 6 +++--- src/cmd/inspect.rs | 5 ++--- src/cmd/list.rs | 4 ++-- src/cmd/list_backup_versions.rs | 6 +++--- src/cmd/list_files.rs | 6 +++--- src/cmd/resolve.rs | 4 ++-- src/cmd/restore.rs | 6 ++---- src/cmd/show_config.rs | 4 ++-- src/cmd/show_gen.rs | 6 +++--- 16 files changed, 46 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs index 6cf4122..cfa2cb5 100644 --- a/src/bin/obnam-server.rs +++ b/src/bin/obnam-server.rs @@ -1,4 +1,5 @@ use anyhow::Context; +use clap::Parser; use log::{debug, error, info}; use obnam::chunk::DataChunk; use obnam::chunkid::ChunkId; @@ -11,16 +12,14 @@ use std::default::Default; use std::net::{SocketAddr, ToSocketAddrs}; use std::path::{Path, PathBuf}; use std::sync::Arc; -use structopt::StructOpt; use tokio::sync::Mutex; use warp::http::StatusCode; use warp::hyper::body::Bytes; use warp::Filter; -#[derive(Debug, StructOpt)] -#[structopt(name = "obnam2-server", about = "Backup server")] +#[derive(Debug, Parser)] +#[clap(name = "obnam2-server", about = "Backup server")] struct Opt { - #[structopt(parse(from_os_str))] config: PathBuf, } @@ -28,7 +27,7 @@ struct Opt { async fn main() -> anyhow::Result<()> { pretty_env_logger::init_custom_env("OBNAM_SERVER_LOG"); - let opt = Opt::from_args(); + let opt = Opt::parse(); let config = load_config(&opt.config)?; let addresses: Vec = config.address.to_socket_addrs()?.collect(); diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 089a7a1..b962c61 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -1,3 +1,4 @@ +use clap::Parser; use directories_next::ProjectDirs; use log::{debug, error, info, LevelFilter}; use log4rs::append::file::FileAppender; @@ -19,7 +20,6 @@ use obnam::cmd::show_gen::ShowGeneration; use obnam::config::ClientConfig; use obnam::performance::{Clock, Performance}; use std::path::{Path, PathBuf}; -use structopt::StructOpt; const QUALIFIER: &str = ""; const ORG: &str = ""; @@ -38,7 +38,7 @@ fn main() { } fn main_program(perf: &mut Performance) -> anyhow::Result<()> { - let opt = Opt::from_args(); + let opt = Opt::parse(); let config = ClientConfig::read(&config_filename(&opt))?; setup_logging(&config.log)?; @@ -96,17 +96,17 @@ fn default_config() -> PathBuf { } } -#[derive(Debug, StructOpt)] -#[structopt(name = "obnam-backup", about = "Simplistic backup client")] +#[derive(Debug, Parser)] +#[clap(name = "obnam-backup", about = "Simplistic backup client")] struct Opt { - #[structopt(long, short, parse(from_os_str))] + #[clap(long, short)] config: Option, - #[structopt(subcommand)] + #[clap(subcommand)] cmd: Command, } -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] enum Command { Init(Init), Backup(Backup), diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs index 80dbb1f..a18027b 100644 --- a/src/cmd/backup.rs +++ b/src/cmd/backup.rs @@ -10,21 +10,21 @@ use crate::generation::GenId; use crate::performance::{Clock, Performance}; use crate::schema::VersionComponent; +use clap::Parser; use log::info; use std::time::SystemTime; -use structopt::StructOpt; use tempfile::tempdir; use tokio::runtime::Runtime; /// Make a backup. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct Backup { /// Force a full backup, instead of an incremental one. - #[structopt(long)] + #[clap(long)] full: bool, /// Backup schema major version to use. - #[structopt(long)] + #[clap(long)] backup_version: Option, } diff --git a/src/cmd/chunk.rs b/src/cmd/chunk.rs index 445d23f..293de20 100644 --- a/src/cmd/chunk.rs +++ b/src/cmd/chunk.rs @@ -5,22 +5,19 @@ use crate::chunkmeta::ChunkMeta; use crate::cipher::CipherEngine; use crate::config::ClientConfig; use crate::error::ObnamError; +use clap::Parser; use std::path::PathBuf; -use structopt::StructOpt; /// Encrypt a chunk. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct EncryptChunk { /// The name of the file containing the cleartext chunk. - #[structopt(parse(from_os_str))] filename: PathBuf, /// Name of file where to write the encrypted chunk. - #[structopt(parse(from_os_str))] output: PathBuf, /// Chunk metadata as JSON. - #[structopt()] json: String, } @@ -43,18 +40,15 @@ impl EncryptChunk { } /// Decrypt a chunk. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct DecryptChunk { /// Name of file containing encrypted chunk. - #[structopt(parse(from_os_str))] filename: PathBuf, /// Name of file where to write the cleartext chunk. - #[structopt(parse(from_os_str))] output: PathBuf, /// Chunk metadata as JSON. - #[structopt()] json: String, } diff --git a/src/cmd/chunkify.rs b/src/cmd/chunkify.rs index e2ce05f..91cb0be 100644 --- a/src/cmd/chunkify.rs +++ b/src/cmd/chunkify.rs @@ -4,10 +4,10 @@ use crate::config::ClientConfig; use crate::engine::Engine; use crate::error::ObnamError; use crate::workqueue::WorkQueue; +use clap::Parser; use serde::Serialize; use sha2::{Digest, Sha256}; use std::path::PathBuf; -use structopt::StructOpt; use tokio::fs::File; use tokio::io::{AsyncReadExt, BufReader}; use tokio::runtime::Runtime; @@ -18,7 +18,7 @@ use tokio::sync::mpsc; const Q: usize = 8; /// Split files into chunks and show their metadata. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct Chunkify { /// Names of files to split into chunks. filenames: Vec, diff --git a/src/cmd/gen_info.rs b/src/cmd/gen_info.rs index 0aec103..901a0ae 100644 --- a/src/cmd/gen_info.rs +++ b/src/cmd/gen_info.rs @@ -4,16 +4,15 @@ use crate::chunk::ClientTrust; use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; +use clap::Parser; use log::info; -use structopt::StructOpt; use tempfile::NamedTempFile; use tokio::runtime::Runtime; /// Show metadata for a generation. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct GenInfo { /// Reference of the generation. - #[structopt()] gen_ref: String, } diff --git a/src/cmd/get_chunk.rs b/src/cmd/get_chunk.rs index 0b27084..1561492 100644 --- a/src/cmd/get_chunk.rs +++ b/src/cmd/get_chunk.rs @@ -4,15 +4,14 @@ use crate::chunkid::ChunkId; use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; +use clap::Parser; use std::io::{stdout, Write}; -use structopt::StructOpt; use tokio::runtime::Runtime; /// Fetch a chunk from the server. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct GetChunk { /// Identifier of chunk to fetch. - #[structopt()] chunk_id: String, } diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 8e555ca..1310f66 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -3,15 +3,15 @@ use crate::config::ClientConfig; use crate::error::ObnamError; use crate::passwords::{passwords_filename, Passwords}; -use structopt::StructOpt; +use clap::Parser; const PROMPT: &str = "Obnam passphrase: "; /// Initialize client by setting passwords. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct Init { /// Only for testing. - #[structopt(long)] + #[clap(long)] insecure_passphrase: Option, } diff --git a/src/cmd/inspect.rs b/src/cmd/inspect.rs index 02801ae..3b41075 100644 --- a/src/cmd/inspect.rs +++ b/src/cmd/inspect.rs @@ -6,16 +6,15 @@ use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; +use clap::Parser; use log::info; -use structopt::StructOpt; use tempfile::NamedTempFile; use tokio::runtime::Runtime; /// Make a backup. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct Inspect { /// Reference to generation to inspect. - #[structopt()] gen_id: String, } diff --git a/src/cmd/list.rs b/src/cmd/list.rs index bbb9c91..8bc6978 100644 --- a/src/cmd/list.rs +++ b/src/cmd/list.rs @@ -4,11 +4,11 @@ use crate::chunk::ClientTrust; use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; -use structopt::StructOpt; +use clap::Parser; use tokio::runtime::Runtime; /// List generations on the server. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct List {} impl List { diff --git a/src/cmd/list_backup_versions.rs b/src/cmd/list_backup_versions.rs index 859d91c..c78ccfc 100644 --- a/src/cmd/list_backup_versions.rs +++ b/src/cmd/list_backup_versions.rs @@ -4,13 +4,13 @@ use crate::config::ClientConfig; use crate::dbgen::{schema_version, DEFAULT_SCHEMA_MAJOR, SCHEMA_MAJORS}; use crate::error::ObnamError; -use structopt::StructOpt; +use clap::Parser; /// List supported backup schema versions. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct ListSchemaVersions { /// List only the default version. - #[structopt(long)] + #[clap(long)] default_only: bool, } diff --git a/src/cmd/list_files.rs b/src/cmd/list_files.rs index fb4764d..e8276cd 100644 --- a/src/cmd/list_files.rs +++ b/src/cmd/list_files.rs @@ -6,15 +6,15 @@ use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; -use structopt::StructOpt; +use clap::Parser; use tempfile::NamedTempFile; use tokio::runtime::Runtime; /// List files in a backup. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct ListFiles { /// Reference to backup to list files in. - #[structopt(default_value = "latest")] + #[clap(default_value = "latest")] gen_id: String, } diff --git a/src/cmd/resolve.rs b/src/cmd/resolve.rs index 12432cc..a7774d7 100644 --- a/src/cmd/resolve.rs +++ b/src/cmd/resolve.rs @@ -4,11 +4,11 @@ use crate::chunk::ClientTrust; use crate::client::BackupClient; use crate::config::ClientConfig; use crate::error::ObnamError; -use structopt::StructOpt; +use clap::Parser; use tokio::runtime::Runtime; /// Resolve a generation reference into a generation id. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct Resolve { /// The generation reference. generation: String, diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index 223d481..c4c06d2 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -9,6 +9,7 @@ use crate::dbgen::FileId; use crate::error::ObnamError; use crate::fsentry::{FilesystemEntry, FilesystemKind}; use crate::generation::{LocalGeneration, LocalGenerationError}; +use clap::Parser; use indicatif::{ProgressBar, ProgressStyle}; use libc::{chmod, mkfifo, timespec, utimensat, AT_FDCWD, AT_SYMLINK_NOFOLLOW}; use log::{debug, error, info}; @@ -20,19 +21,16 @@ use std::os::unix::fs::symlink; use std::os::unix::net::UnixListener; use std::path::StripPrefixError; use std::path::{Path, PathBuf}; -use structopt::StructOpt; use tempfile::NamedTempFile; use tokio::runtime::Runtime; /// Restore a backup. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct Restore { /// Reference to generation to restore. - #[structopt()] gen_id: String, /// Path to directory where restored files are written. - #[structopt(parse(from_os_str))] to: PathBuf, } diff --git a/src/cmd/show_config.rs b/src/cmd/show_config.rs index 7ac52ec..8e0ce30 100644 --- a/src/cmd/show_config.rs +++ b/src/cmd/show_config.rs @@ -2,10 +2,10 @@ use crate::config::ClientConfig; use crate::error::ObnamError; -use structopt::StructOpt; +use clap::Parser; /// Show actual client configuration. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct ShowConfig {} impl ShowConfig { diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs index f47a07b..95d3fd3 100644 --- a/src/cmd/show_gen.rs +++ b/src/cmd/show_gen.rs @@ -7,17 +7,17 @@ use crate::db::DbInt; use crate::error::ObnamError; use crate::fsentry::FilesystemKind; use crate::generation::GenId; +use clap::Parser; use indicatif::HumanBytes; use serde::Serialize; -use structopt::StructOpt; use tempfile::NamedTempFile; use tokio::runtime::Runtime; /// Show information about a generation. -#[derive(Debug, StructOpt)] +#[derive(Debug, Parser)] pub struct ShowGeneration { /// Reference to the generation. Defaults to latest. - #[structopt(default_value = "latest")] + #[clap(default_value = "latest")] gen_id: String, } -- cgit v1.2.1