|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -#[macro_use] |
16 | | -extern crate clap; |
17 | 15 | #[macro_use] |
18 | 16 | extern crate log; |
19 | 17 |
|
20 | 18 | use std::{fmt, |
21 | 19 | path::PathBuf, |
22 | | - process, |
23 | | - str::FromStr}; |
| 20 | + process}; |
24 | 21 |
|
25 | 22 | use builder_core::config::ConfigFile; |
| 23 | +use clap::{Parser, |
| 24 | + Subcommand}; |
26 | 25 | use habitat_builder_api as bldr_api; |
27 | 26 |
|
28 | 27 | use crate::bldr_api::{config::Config, |
29 | 28 | server}; |
30 | 29 |
|
31 | 30 | const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/VERSION")); |
32 | 31 |
|
| 32 | +#[derive(Parser, Debug)] |
| 33 | +#[command(version = VERSION, about = "Habitat builder-api", subcommand_required = true, arg_required_else_help = true)] |
| 34 | +struct BuilderApi { |
| 35 | + #[command(subcommand)] |
| 36 | + command: Commands, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Subcommand, Debug)] |
| 40 | +enum Commands { |
| 41 | + /// Run the builder-api server |
| 42 | + Start { |
| 43 | + /// Filepath to configuration file. |
| 44 | + #[arg(short, long)] |
| 45 | + config: Option<PathBuf>, |
| 46 | + |
| 47 | + /// Filepath to store packages, keys, and other artifacts. |
| 48 | + #[arg(short, long)] |
| 49 | + path: Option<PathBuf>, |
| 50 | + |
| 51 | + /// Listen port. [default: 9636] |
| 52 | + #[arg(long)] |
| 53 | + port: Option<u16>, |
| 54 | + }, |
| 55 | +} |
| 56 | + |
33 | 57 | #[actix_rt::main] |
34 | 58 | async fn main() { |
35 | 59 | env_logger::init(); |
36 | | - let matches = app().get_matches(); |
37 | | - debug!("CLI matches: {:?}", matches); |
38 | | - match server::run(config_from_args(&matches)).await { |
| 60 | + let cli = BuilderApi::parse(); |
| 61 | + debug!("CLI: {:?}", cli); |
| 62 | + match server::run(config_from_args(cli)).await { |
39 | 63 | Ok(_) => std::process::exit(0), |
40 | 64 | Err(e) => exit_with(e, 1), |
41 | 65 | } |
42 | 66 | } |
43 | 67 |
|
44 | | -fn app<'a, 'b>() -> clap::App<'a, 'b> { |
45 | | - clap_app!(BuilderApi => |
46 | | - (version: VERSION) |
47 | | - (about: "Habitat builder-api") |
48 | | - (@setting VersionlessSubcommands) |
49 | | - (@setting SubcommandRequiredElseHelp) |
50 | | - (@subcommand start => |
51 | | - (about: "Run the builder-api server") |
52 | | - (@arg config: -c --config +takes_value |
53 | | - "Filepath to configuration file.") |
54 | | - (@arg path: -p --path +takes_value |
55 | | - "Filepath to store packages, keys, and other artifacts.") |
56 | | - (@arg port: --port +takes_value "Listen port. [default: 9636]") |
57 | | - ) |
58 | | - ) |
59 | | -} |
| 68 | +fn config_from_args(cli: BuilderApi) -> Config { |
| 69 | + match cli.command { |
| 70 | + Commands::Start { config, path, port } => { |
| 71 | + let mut cfg = match config { |
| 72 | + Some(cfg_path) => Config::from_file(cfg_path).unwrap(), |
| 73 | + None => Config::default(), |
| 74 | + }; |
60 | 75 |
|
61 | | -fn config_from_args(matches: &clap::ArgMatches) -> Config { |
62 | | - let cmd = matches.subcommand_name().unwrap(); |
63 | | - let args = matches.subcommand_matches(cmd).unwrap(); |
64 | | - let mut config = match args.value_of("config") { |
65 | | - Some(cfg_path) => Config::from_file(cfg_path).unwrap(), |
66 | | - None => Config::default(), |
67 | | - }; |
| 76 | + if let Some(p) = port { |
| 77 | + cfg.http.port = p; |
| 78 | + } |
68 | 79 |
|
69 | | - if let Some(port) = args.value_of("port") { |
70 | | - u16::from_str(port).map(|p| config.http.port = p) |
71 | | - .expect("Specified port must be a valid u16"); |
72 | | - } |
| 80 | + if let Some(p) = path { |
| 81 | + cfg.api.data_path = p; |
| 82 | + } |
73 | 83 |
|
74 | | - if let Some(path) = args.value_of("path") { |
75 | | - config.api.data_path = PathBuf::from(path); |
| 84 | + cfg |
| 85 | + } |
76 | 86 | } |
77 | | - |
78 | | - config |
79 | 87 | } |
80 | 88 |
|
81 | 89 | fn exit_with<T>(err: T, code: i32) |
|
0 commit comments