From 3835fbf30c163ede1b8bc8ffe428f49f95fd925c Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Fri, 17 May 2019 11:02:57 +0200 Subject: [PATCH] [PR Feedback] Order fields and clean cli flag func Signed-off-by: Salim Afiune --- components/hab/src/cli.rs | 127 +++++++++++++++--------------------- components/sup/src/event.rs | 8 +-- 2 files changed, 57 insertions(+), 78 deletions(-) diff --git a/components/hab/src/cli.rs b/components/hab/src/cli.rs index 89d5283487..f4716ebb3d 100644 --- a/components/hab/src/cli.rs +++ b/components/hab/src/cli.rs @@ -1054,7 +1054,11 @@ pub fn sub_sup_run(feature_flags: FeatureFlag) -> App<'static, 'static> { "The interval (seconds) on which to run health checks [default: 30]") ); - maybe_add_event_stream_options(sub, feature_flags) + if feature_flags.contains(FeatureFlag::EVENT_STREAM) { + add_event_stream_options(sub) + } else { + sub + } } pub fn sub_sup_sh() -> App<'static, 'static> { @@ -1172,80 +1176,55 @@ fn sub_svc_unload(feature_flags: FeatureFlag) -> App<'static, 'static> { // // For now, though, these at least provide a place to supply the // information; we can revise as we go. -fn maybe_add_event_stream_options(mut app: App<'static, 'static>, - feature_flags: FeatureFlag) - -> App<'static, 'static> { - if feature_flags.contains(FeatureFlag::EVENT_STREAM) { - app = app.arg(Arg::with_name("EVENT_STREAM_APPLICATION").help("The name of the \ - application for event \ - stream purposes. This is \ - distinct from the \ - `--application` flag \ - (which may be going \ - away), and will be \ - attached to all events \ - generated by this \ - Supervisor.") - .long("event-stream-application") - .required(true) - .takes_value(true) - .validator(non_empty)); - app = app.arg(Arg::with_name("EVENT_STREAM_ENVIRONMENT").help("The name of the \ - environment for event \ - stream purposes. This is \ - distinct from the \ - `--environment` flag \ - (which may be going \ - away), and will be \ - attached to all events \ - generated by this \ - Supervisor.") - .long("event-stream-environment") - .required(true) - .takes_value(true) - .validator(non_empty)); - app = app.arg(Arg::with_name("EVENT_STREAM_URL").help("The event stream connection \ - string (host:port) used by this \ - Supervisor to send events to a \ - messaging server.") - .long("event-stream-url") - .required(true) - .takes_value(true) - .validator(non_empty)); - app = app.arg(Arg::with_name("EVENT_STREAM_SITE").help("The name of the site where this \ - Supervisor is running. It is \ - used for event stream purposes.") - .long("event-stream-site") +fn add_event_stream_options(app: App<'static, 'static>) -> App<'static, 'static> { + app.arg(Arg::with_name("EVENT_STREAM_APPLICATION").help("The name of the application for \ + event stream purposes. This is \ + distinct from the `--application` \ + flag (which may be going away), and \ + will be attached to all events \ + generated by this Supervisor.") + .long("event-stream-application") + .required(true) + .takes_value(true) + .validator(non_empty)) + .arg(Arg::with_name("EVENT_STREAM_ENVIRONMENT").help("The name of the environment for \ + event stream purposes. This is \ + distinct from the `--environment` \ + flag (which may be going away), and \ + will be attached to all events \ + generated by this Supervisor.") + .long("event-stream-environment") + .required(true) + .takes_value(true) + .validator(non_empty)) + .arg(Arg::with_name("EVENT_STREAM_URL").help("The event stream connection string \ + (host:port) used by this Supervisor to send \ + events to a messaging server.") + .long("event-stream-url") + .required(true) + .takes_value(true) + .validator(non_empty)) + .arg(Arg::with_name("EVENT_STREAM_SITE").help("The name of the site where this Supervisor \ + is running. It is used for event stream \ + purposes.") + .long("event-stream-site") + .takes_value(true) + .validator(non_empty)) + .arg(Arg::with_name(AutomateAuthToken::ARG_NAME).help("An authentication token for \ + streaming events to an messaging \ + server.") + .long("event-stream-token") + .required(true) + .takes_value(true) + .validator(AutomateAuthToken::validate) + .env(AutomateAuthToken::ENVVAR)) + .arg(Arg::with_name(EventStreamMetadata::ARG_NAME).help("An arbitrary key-value pair to \ + add to each event generated by \ + this Supervisor") + .long("event-meta") .takes_value(true) - .validator(non_empty)); - app = app.arg( - Arg::with_name(AutomateAuthToken::ARG_NAME) - .help( - "An authentication token for \ - streaming events to an \ - messaging server.", - ) - .long("event-stream-token") - .required(true) - .takes_value(true) - .validator(AutomateAuthToken::validate) - .env(AutomateAuthToken::ENVVAR), - ); - app = app.arg( - Arg::with_name(EventStreamMetadata::ARG_NAME) - .help( - "An arbitrary key-value pair \ - to add to each event \ - generated by this Supervisor", - ) - .long("event-meta") - .takes_value(true) - .multiple(true) - .validator(EventStreamMetadata::validate), - ); - } - - app + .multiple(true) + .validator(EventStreamMetadata::validate)) } // CLAP Validation Functions diff --git a/components/sup/src/event.rs b/components/sup/src/event.rs index 10a5335aba..29277334f9 100644 --- a/components/sup/src/event.rs +++ b/components/sup/src/event.rs @@ -98,10 +98,10 @@ pub fn init_stream(config: EventStreamConfig, event_core: EventCore) -> Result<( pub struct EventStreamConfig { environment: String, application: String, + site: Option, meta: EventStreamMetadata, token: AutomateAuthToken, url: String, - site: Option, } impl<'a> From<&'a ArgMatches<'a>> for EventStreamConfig { @@ -112,12 +112,12 @@ impl<'a> From<&'a ArgMatches<'a>> for EventStreamConfig { application: m.value_of("EVENT_STREAM_APPLICATION") .map(str::to_string) .expect("Required option for EventStream feature"), + site: m.value_of("EVENT_STREAM_SITE").map(str::to_string), meta: EventStreamMetadata::from(m), token: AutomateAuthToken::from(m), url: m.value_of("EVENT_STREAM_URL") .map(str::to_string) - .expect("Required option for EventStream feature"), - site: m.value_of("EVENT_STREAM_SITE").map(str::to_string), } + .expect("Required option for EventStream feature"), } } } @@ -167,9 +167,9 @@ impl EventCore { EventCore { supervisor_id: sys.member_id.clone(), ip_address: sys.gossip_listen(), fqdn, - site: config.site.clone(), environment: config.environment.clone(), application: config.application.clone(), + site: config.site.clone(), meta: config.meta.clone() } } }