openstack_cli_network/
lib.rs1use clap::error::{Error, ErrorKind};
17use clap::{Arg, ArgAction, ArgMatches, Args, Command, FromArgMatches};
18
19use openstack_cli_core::{cli::CliArgs, error::OpenStackCliError};
20use openstack_sdk::AsyncOpenStack;
21
22pub mod v2;
23
24const API_VERSION_ARG_ID: &str = "os_network_api_version";
25const API_VERSION_LONG: &str = "os-network-api-version";
26const API_VERSION_ENV: &str = "OS_NETWORK_API_VERSION";
27const DEFAULT_NETWORK_API_VERSION: &str = "2";
28
29pub enum NetworkCommand {
31 V2(v2::NetworkCommand),
32}
33
34impl Args for NetworkCommand {
35 fn augment_args(cmd: Command) -> Command {
36 let cmd = cmd.arg(
41 Arg::new(API_VERSION_ARG_ID)
42 .long(API_VERSION_LONG)
43 .env(API_VERSION_ENV)
44 .global(true)
45 .action(ArgAction::Set)
46 .default_value(DEFAULT_NETWORK_API_VERSION)
47 .help("Network API version to use (default: 2)"),
48 );
49 v2::NetworkCommand::augment_args(cmd)
50 }
51
52 fn augment_args_for_update(cmd: Command) -> Command {
53 Self::augment_args(cmd)
54 }
55}
56
57impl FromArgMatches for NetworkCommand {
58 fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
59 let version = matches
60 .get_one::<String>(API_VERSION_ARG_ID)
61 .map(String::as_str)
62 .unwrap_or(DEFAULT_NETWORK_API_VERSION);
63 match version {
64 "2" => Ok(Self::V2(v2::NetworkCommand::from_arg_matches(matches)?)),
65 other => Err(Error::raw(
66 ErrorKind::InvalidValue,
67 format!("unsupported Network API version: {other}. Supported: 2\n"),
68 )),
69 }
70 }
71
72 fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
73 *self = Self::from_arg_matches(matches)?;
74 Ok(())
75 }
76}
77
78impl NetworkCommand {
79 pub async fn take_action<C: CliArgs>(
81 &self,
82 parsed_args: &C,
83 client: &mut AsyncOpenStack,
84 ) -> Result<(), OpenStackCliError> {
85 match self {
86 Self::V2(cmd) => cmd.take_action(parsed_args, client).await,
87 }
88 }
89}