Skip to content

Commit 6f5fe84

Browse files
committed
migrate to latest clap
Signed-off-by: Matt Wrock <matt@mattwrock.com>
1 parent c64b872 commit 6f5fe84

4 files changed

Lines changed: 56 additions & 119 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/builder-api/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ default-features = false
5050
features = [ "openssl" ]
5151

5252
[dependencies.clap]
53-
version = "*"
54-
features = [ "suggestions", "color", "unstable" ]
53+
version = "4"
54+
features = [ "derive", "color" ]
5555

5656
[dependencies.artifactory-client]
5757
path = "../artifactory-client"

components/builder-api/src/main.rs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,78 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#[macro_use]
16-
extern crate clap;
1715
#[macro_use]
1816
extern crate log;
1917

2018
use std::{fmt,
2119
path::PathBuf,
22-
process,
23-
str::FromStr};
20+
process};
2421

2522
use builder_core::config::ConfigFile;
23+
use clap::{Parser,
24+
Subcommand};
2625
use habitat_builder_api as bldr_api;
2726

2827
use crate::bldr_api::{config::Config,
2928
server};
3029

3130
const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/VERSION"));
3231

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<String>,
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+
3357
#[actix_rt::main]
3458
async fn main() {
3559
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 {
3963
Ok(_) => std::process::exit(0),
4064
Err(e) => exit_with(e, 1),
4165
}
4266
}
4367

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+
};
6075

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+
}
6879

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+
}
7383

74-
if let Some(path) = args.value_of("path") {
75-
config.api.data_path = PathBuf::from(path);
84+
cfg
85+
}
7686
}
77-
78-
config
7987
}
8088

8189
fn exit_with<T>(err: T, code: i32)

tools/token-generator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description = "Standalone CLI tool to generate user authentication tokens"
88

99
[dependencies]
1010
builder_core = { path = "../../components/builder-core" }
11-
clap = { version = "4.0", features = ["derive"] }
11+
clap = { version = "4", features = ["derive"] }
1212
anyhow = "1.0"
1313
log = "0.4"
1414
env_logger = "0.10"

0 commit comments

Comments
 (0)