-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wash-cli): add tag command to set and remove host labels
Signed-off-by: Connor Smith <connor.smith.256@gmail.com>
- Loading branch information
1 parent
7fd1b09
commit b768883
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use anyhow::Result; | ||
|
||
use wash_lib::cli::{ | ||
tag::{handle_tag_host, TagHostCommand}, | ||
CommandOutput, OutputKind, | ||
}; | ||
|
||
use crate::appearance::spinner::Spinner; | ||
|
||
pub async fn handle_command(cmd: TagHostCommand, output_kind: OutputKind) -> Result<CommandOutput> { | ||
let sp: Spinner = Spinner::new(&output_kind)?; | ||
let out = handle_tag_host(cmd).await?; | ||
sp.finish_and_clear(); | ||
|
||
Ok(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use anyhow::{bail, Context, Result}; | ||
use clap::Parser; | ||
|
||
use crate::{ | ||
common::{boxed_err_to_anyhow, find_host_id}, | ||
config::WashConnectionOptions, | ||
}; | ||
|
||
use super::{CliConnectionOpts, CommandOutput}; | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
pub struct TagHostCommand { | ||
#[clap(flatten)] | ||
pub opts: CliConnectionOpts, | ||
|
||
/// ID of host to update the actor on. If a non-ID is provided, the host will be selected based | ||
/// on matching the prefix of the ID or the friendly name and will return an error if more than | ||
/// one host matches. | ||
#[clap(name = "host-id")] | ||
pub host_id: String, | ||
|
||
/// Delete the label, instead of adding it | ||
#[clap(long = "delete", default_value = "false", conflicts_with("value"))] | ||
pub delete: bool, | ||
|
||
/// Host label key, e.g. "cloud" | ||
#[clap(name = "key", alias = "label")] | ||
pub key: String, | ||
|
||
/// Host label value, e.g. "aws". Not required if `--delete` is set. | ||
#[clap(name = "value", required_unless_present("delete"))] | ||
pub value: Option<String>, | ||
} | ||
|
||
pub async fn handle_tag_host(cmd: TagHostCommand) -> Result<CommandOutput> { | ||
let wco: WashConnectionOptions = cmd.opts.try_into()?; | ||
let client = wco.into_ctl_client(None).await?; | ||
|
||
let (host_id, friendly_name) = find_host_id(&cmd.host_id, &client).await?; | ||
|
||
let friendly_name = if friendly_name.is_empty() { | ||
host_id.to_string() | ||
} else { | ||
friendly_name | ||
}; | ||
|
||
if cmd.delete { | ||
let ack = client | ||
.delete_label(&host_id, &cmd.key) | ||
.await | ||
.map_err(boxed_err_to_anyhow)?; | ||
if !ack.accepted { | ||
bail!("Operation failed: {}", ack.error); | ||
} | ||
|
||
Ok(CommandOutput::from_key_and_text( | ||
"result", | ||
format!("Host `{}` untagged with `{}`", friendly_name, cmd.key), | ||
)) | ||
} else { | ||
let value = cmd.value.context("No value provided")?; | ||
|
||
let ack = client | ||
.put_label(&host_id, &cmd.key, &value) | ||
.await | ||
.map_err(boxed_err_to_anyhow)?; | ||
if !ack.accepted { | ||
bail!("Operation failed: {}", ack.error); | ||
} | ||
|
||
Ok(CommandOutput::from_key_and_text( | ||
"result", | ||
format!( | ||
"Host `{}` tagged with `{}={}`", | ||
friendly_name, cmd.key, value | ||
), | ||
)) | ||
} | ||
} |