use clap::error::{Error, ErrorKind};
use clap::{Arg, ArgAction, ArgMatches, Args, Command, FromArgMatches};
use openstack_cli_core::{cli::CliArgs, error::OpenStackCliError};
use openstack_sdk::AsyncOpenStack;
pub mod v2;
const API_VERSION_ARG_ID: &str = "os_image_api_version";
const API_VERSION_LONG: &str = "os-image-api-version";
const API_VERSION_ENV: &str = "OS_IMAGE_API_VERSION";
const DEFAULT_IMAGE_API_VERSION: &str = "2";
pub enum ImageCommand {
V2(v2::ImageCommand),
}
impl Args for ImageCommand {
fn augment_args(cmd: Command) -> Command {
let cmd = cmd.arg(
Arg::new(API_VERSION_ARG_ID)
.long(API_VERSION_LONG)
.env(API_VERSION_ENV)
.global(true)
.action(ArgAction::Set)
.default_value(DEFAULT_IMAGE_API_VERSION)
.help("Image API version to use (default: 2)"),
);
v2::ImageCommand::augment_args(cmd)
}
fn augment_args_for_update(cmd: Command) -> Command {
Self::augment_args(cmd)
}
}
impl FromArgMatches for ImageCommand {
fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
let version = matches
.get_one::<String>(API_VERSION_ARG_ID)
.map(String::as_str)
.unwrap_or(DEFAULT_IMAGE_API_VERSION);
match version {
"2" => Ok(Self::V2(v2::ImageCommand::from_arg_matches(matches)?)),
other => Err(Error::raw(
ErrorKind::InvalidValue,
format!("unsupported Image API version: {other}. Supported: 2\n"),
)),
}
}
fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
*self = Self::from_arg_matches(matches)?;
Ok(())
}
}
impl ImageCommand {
pub async fn take_action<C: CliArgs>(
&self,
parsed_args: &C,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
match self {
Self::V2(cmd) => cmd.take_action(parsed_args, client).await,
}
}
}