use crate::printer::Printer;
use anyhow::{Context, Result};
use log::info;
use reinfer_client::{BucketIdentifier, Client, SourceIdentifier, TransformTag, UpdateSource};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct UpdateSourceArgs {
#[structopt(name = "source")]
source: SourceIdentifier,
#[structopt(long = "title")]
title: Option<String>,
#[structopt(long = "description")]
description: Option<String>,
#[structopt(long = "should-translate")]
should_translate: Option<bool>,
#[structopt(long = "bucket")]
bucket: Option<BucketIdentifier>,
#[structopt(long = "transform-tag")]
transform_tag: Option<TransformTag>,
}
pub fn update(client: &Client, args: &UpdateSourceArgs, printer: &Printer) -> Result<()> {
let UpdateSourceArgs {
source,
title,
description,
should_translate,
bucket,
transform_tag,
} = args;
let bucket_id = match bucket.to_owned() {
Some(BucketIdentifier::Id(bucket_id)) => Some(bucket_id),
Some(full_name @ BucketIdentifier::FullName(_)) => Some(
client
.get_bucket(full_name)
.context("Fetching bucket for id.")?
.id,
),
None => None,
};
let source_full_name = match source.to_owned() {
SourceIdentifier::FullName(name) => name,
source @ SourceIdentifier::Id(_) => client
.get_source(source)
.context("Fetching source id.")?
.full_name(),
};
let source = client
.update_source(
&source_full_name,
UpdateSource {
title: title.as_deref(),
description: description.as_deref(),
should_translate: *should_translate,
bucket_id,
sensitive_properties: None,
transform_tag: transform_tag.as_ref(),
},
)
.context("Operation to update a source has failed")?;
info!(
"Source `{}` [id: {}] updated successfully",
source.full_name().0,
source.id.0
);
printer.print_resources(&[source])?;
Ok(())
}