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_network_api_version";
const API_VERSION_LONG: &str = "os-network-api-version";
const API_VERSION_ENV: &str = "OS_NETWORK_API_VERSION";
const DEFAULT_NETWORK_API_VERSION: &str = "2";
pub enum NetworkCommand {
V2(v2::NetworkCommand),
}
impl Args for NetworkCommand {
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_NETWORK_API_VERSION)
.help("Network API version to use (default: 2)"),
);
v2::NetworkCommand::augment_args(cmd)
}
fn augment_args_for_update(cmd: Command) -> Command {
Self::augment_args(cmd)
}
}
impl FromArgMatches for NetworkCommand {
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_NETWORK_API_VERSION);
match version {
"2" => Ok(Self::V2(v2::NetworkCommand::from_arg_matches(matches)?)),
other => Err(Error::raw(
ErrorKind::InvalidValue,
format!("unsupported Network 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 NetworkCommand {
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,
}
}
}