Skip to content

Commit

Permalink
make json a global option
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Mar 4, 2024
1 parent a642f13 commit 7abe9ec
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 28 deletions.
9 changes: 2 additions & 7 deletions src/commands/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ pub struct BackupCmd {
/// Don't scan the backup source for its size - this disables ETA estimation for backup.
#[clap(long)]
#[merge(strategy = merge::bool::overwrite_false)]
pub no_scan: bool,

/// Output generated snapshot in json format
#[clap(long)]
#[merge(strategy = merge::bool::overwrite_false)]
json: bool,
no_scan: bool,

/// Don't show any output
#[clap(long, conflicts_with = "json")]
Expand Down Expand Up @@ -228,7 +223,7 @@ impl BackupCmd {
.dry_run(config.global.dry_run);
let snap = repo.backup(&backup_opts, &source, opts.snap_opts.to_snapshot()?)?;

if opts.json {
if config.global.json {
let mut stdout = std::io::stdout();
serde_json::to_writer_pretty(&mut stdout, &snap)?;
} else if !opts.quiet {
Expand Down
12 changes: 6 additions & 6 deletions src/commands/forget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ pub(super) struct ForgetCmd {
#[clap(value_name = "ID")]
ids: Vec<String>,

/// Show infos in json format
#[clap(long)]
json: bool,

/// Don't show any output
#[clap(long, conflicts_with = "json")]
quiet: bool,
Expand Down Expand Up @@ -123,7 +119,7 @@ impl ForgetCmd {
ForgetGroups(vec![item])
};

if self.json {
if config.global.json {
let mut stdout = std::io::stdout();
serde_json::to_writer_pretty(&mut stdout, &groups)?;
} else if !self.quiet {
Expand All @@ -132,7 +128,11 @@ impl ForgetCmd {

let forget_snaps = groups.into_forget_ids();

match (forget_snaps.is_empty(), config.global.dry_run, self.json) {
match (
forget_snaps.is_empty(),
config.global.dry_run,
config.global.json,
) {
(true, _, false) => println!("nothing to remove"),
(false, true, false) => {
println!("would have removed the following snapshots:\n {forget_snaps:?}");
Expand Down
6 changes: 1 addition & 5 deletions src/commands/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ pub(super) struct MergeCmd {
#[clap(value_name = "ID")]
ids: Vec<String>,

/// Output generated snapshot in json format
#[clap(long)]
json: bool,

/// Remove input snapshots after merging
#[clap(long)]
delete: bool,
Expand Down Expand Up @@ -53,7 +49,7 @@ impl MergeCmd {

let snap = repo.merge_snapshots(&snapshots, &last_modified_node, snap)?;

if self.json {
if config.global.json {
let mut stdout = std::io::stdout();
serde_json::to_writer_pretty(&mut stdout, &snap)?;
}
Expand Down
6 changes: 1 addition & 5 deletions src/commands/repoinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ pub(crate) struct RepoInfoCmd {
/// Only scan index
#[clap(long)]
only_index: bool,

/// Show infos in json format
#[clap(long)]
json: bool,
}

impl Runnable for RepoInfoCmd {
Expand Down Expand Up @@ -66,7 +62,7 @@ impl RepoInfoCmd {
.transpose()?,
};

if self.json {
if config.global.json {
let mut stdout = std::io::stdout();
serde_json::to_writer_pretty(&mut stdout, &infos)?;
return Ok(());
Expand Down
6 changes: 1 addition & 5 deletions src/commands/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ pub(crate) struct SnapshotCmd {
#[arg(long)]
long: bool,

/// Show snapshots in json format
#[clap(long, conflicts_with = "long")]
json: bool,

/// Show all snapshots instead of summarizing identical follow-up snapshots
#[clap(long, conflicts_with_all = &["long", "json"])]
all: bool,
Expand All @@ -64,7 +60,7 @@ impl SnapshotCmd {
config.snapshot_filter.matches(sn)
})?;

if self.json {
if config.global.json {
let mut stdout = std::io::stdout();
serde_json::to_writer_pretty(&mut stdout, &groups)?;
return Ok(());
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ pub struct GlobalOptions {
#[clap(long, global = true, env = "RUSTIC_LOG_FILE", value_name = "LOGFILE")]
pub log_file: Option<PathBuf>,

/// Output in JSON format
#[clap(long, global = true, env = "RUSTIC_JSON")]
#[merge(strategy = merge::bool::overwrite_false)]
pub json: bool,

/// Settings to customize progress bars
#[clap(flatten)]
#[serde(flatten)]
Expand Down

0 comments on commit 7abe9ec

Please sign in to comment.