-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(runtime): Remove dependency on clap #822
Merged
+111
−40
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,33 @@ | ||
use std::path::PathBuf; | ||
use std::{fmt::Debug, path::PathBuf, str::FromStr}; | ||
|
||
use clap::{Parser, ValueEnum}; | ||
use tonic::transport::{Endpoint, Uri}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(version)] | ||
pub struct Args { | ||
/// Port to start runtime on | ||
#[arg(long)] | ||
pub port: u16, | ||
use crate::args::args; | ||
|
||
/// Address to reach provisioner at | ||
#[arg(long, default_value = "http://localhost:3000")] | ||
pub provisioner_address: Endpoint, | ||
|
||
/// Type of storage manager to start | ||
#[arg(long, value_enum)] | ||
pub storage_manager_type: StorageManagerType, | ||
|
||
/// Path to use for storage manager | ||
#[arg(long)] | ||
pub storage_manager_path: PathBuf, | ||
|
||
/// Address to reach the authentication service at | ||
#[arg(long, default_value = "http://127.0.0.1:8008")] | ||
pub auth_uri: Uri, | ||
args! { | ||
pub struct Args { | ||
"--port" => pub port: u16, | ||
"--provisioner-address" => #[arg(default_value = "http://localhost:3000")] pub provisioner_address: Endpoint, | ||
"--storage-manager-type" => pub storage_manager_type: StorageManagerType, | ||
"--storage-manager-path" => pub storage_manager_path: PathBuf, | ||
"--auth-uri" => #[arg(default_value = "http://127.0.0.1:8008")] pub auth_uri: Uri, | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, ValueEnum)] | ||
#[derive(Clone, Debug)] | ||
pub enum StorageManagerType { | ||
/// Use a deloyer artifacts directory | ||
Artifacts, | ||
|
||
/// Use a local working directory | ||
WorkingDir, | ||
} | ||
|
||
impl FromStr for StorageManagerType { | ||
type Err = (); | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"artifacts" => Ok(StorageManagerType::Artifacts), | ||
"working-dir" => Ok(StorageManagerType::WorkingDir), | ||
_ => Err(()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#[derive(Debug)] | ||
pub enum Error { | ||
DuplicatedArgument { arg: &'static str }, | ||
MissingRequiredArgument { arg: &'static str }, | ||
UnexpectedArgument { arg: String }, | ||
|
||
InvalidValue { arg: &'static str, value: String }, | ||
MissingValue { arg: &'static str }, | ||
} | ||
|
||
impl std::fmt::Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::DuplicatedArgument { arg } => write!(f, "duplicated argument {arg}"), | ||
Self::MissingRequiredArgument { arg } => write!(f, "missing required argument {arg}"), | ||
Self::UnexpectedArgument { arg } => write!(f, "unexpected argument: {arg}"), | ||
|
||
Self::InvalidValue { arg, value } => { | ||
write!(f, "invalid value for argument {arg}: {value}") | ||
} | ||
Self::MissingValue { arg } => write!(f, "missing value for argument {arg}"), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
macro_rules! args { | ||
// Internal rules used to handle optional default values | ||
(@unwrap $arg:literal, $field:ident $(,)?) => { | ||
$field.ok_or_else(|| $crate::args::Error::MissingRequiredArgument { arg: $arg })? | ||
}; | ||
(@unwrap $arg:literal, $field:ident, $default:literal) => { | ||
$field.unwrap_or_else(|| $default.parse().unwrap()) | ||
}; | ||
|
||
( | ||
pub struct $struct:ident { | ||
$($arg:literal => $(#[arg(default_value = $default:literal)])? pub $field:ident: $ty:ty),+ $(,)? | ||
chesedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
) => { | ||
#[derive(::std::fmt::Debug)] | ||
pub struct $struct { | ||
$(pub $field: $ty,)+ | ||
} | ||
|
||
impl $struct { | ||
pub fn parse() -> ::std::result::Result<Self, $crate::args::Error> { | ||
$(let mut $field: ::std::option::Option<$ty> = None;)+ | ||
|
||
// The first argument is the path of the executable. | ||
let mut args_iter = ::std::env::args().skip(1); | ||
while let ::std::option::Option::Some(arg) = args_iter.next() { | ||
match arg.as_str() { | ||
$($arg => { | ||
if $field.is_some() { | ||
return ::std::result::Result::Err($crate::args::Error::DuplicatedArgument { arg: $arg }); | ||
} | ||
let raw_value = args_iter | ||
.next() | ||
.ok_or_else(|| $crate::args::Error::MissingValue { arg: $arg })?; | ||
let value = raw_value.parse().map_err(|_| $crate::args::Error::InvalidValue { | ||
arg: $arg, | ||
value: raw_value, | ||
})?; | ||
$field = ::std::option::Option::Some(value); | ||
})+ | ||
_ => return ::std::result::Result::Err($crate::args::Error::UnexpectedArgument { arg }), | ||
} | ||
} | ||
|
||
::std::result::Result::Ok($struct { | ||
$($field: $crate::args::args!(@unwrap $arg, $field, $($default)?),)+ | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) use args; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
use clap::Parser; | ||
use crate::args::args; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(version)] | ||
pub struct NextArgs { | ||
/// Port to start runtime on | ||
#[arg(long)] | ||
pub port: u16, | ||
args! { | ||
pub struct NextArgs { | ||
"--port" => pub port: u16, | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe these should be in a separate macro_rule (
unwrap_arg
?). They feel a bit unexpected here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I you call an exported macro from another module, then every macro it calls must also be exported. I tracked down this trick for dealing with it without polluting any namespaces. http://danielkeep.github.io/tlborm/book/pat-internal-rules.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, calling the new macro using its full namespace will also work here 😄