Skip to content

Commit 3c86968

Browse files
committed
trigger: Decouple Trigger from clap
- Rename `Trigger`'s associated `CliArgs` type to `GlobalConfig` - Move that `clap::Args` bound into `FactorsTriggerCommand` - Fix up related code Signed-off-by: Lann Martin <[email protected]>
1 parent 416434b commit 3c86968

File tree

7 files changed

+43
-30
lines changed

7 files changed

+43
-30
lines changed

crates/trigger-http/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) type TriggerInstanceBuilder<'a, F> =
3838
spin_trigger::TriggerInstanceBuilder<'a, HttpTrigger, F>;
3939

4040
#[derive(Args)]
41-
pub struct CliArgs {
41+
pub struct GlobalConfig {
4242
/// IP address and port to listen on
4343
#[clap(long = "listen", env = "SPIN_HTTP_LISTEN_ADDR", default_value = "127.0.0.1:3000", value_parser = parse_listen_addr)]
4444
pub address: SocketAddr,
@@ -52,7 +52,7 @@ pub struct CliArgs {
5252
pub tls_key: Option<PathBuf>,
5353
}
5454

55-
impl CliArgs {
55+
impl GlobalConfig {
5656
fn into_tls_config(self) -> Option<TlsConfig> {
5757
match (self.tls_cert, self.tls_key) {
5858
(Some(cert_path), Some(key_path)) => Some(TlsConfig {
@@ -78,11 +78,11 @@ pub struct HttpTrigger {
7878
impl<F: RuntimeFactors> Trigger<F> for HttpTrigger {
7979
const TYPE: &'static str = "http";
8080

81-
type CliArgs = CliArgs;
81+
type GlobalConfig = GlobalConfig;
8282
type InstanceState = ();
8383

84-
fn new(cli_args: Self::CliArgs, app: &spin_app::App) -> anyhow::Result<Self> {
85-
Self::new(app, cli_args.address, cli_args.into_tls_config())
84+
fn new(cfg: Self::GlobalConfig, app: &spin_app::App) -> anyhow::Result<Self> {
85+
Self::new(app, cfg.address, cfg.into_tls_config())
8686
}
8787

8888
async fn run(self, trigger_app: TriggerApp<F>) -> anyhow::Result<()> {

crates/trigger-redis/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use redis::{Client, Msg};
66
use serde::Deserialize;
77
use spin_factor_variables::VariablesFactor;
88
use spin_factors::RuntimeFactors;
9-
use spin_trigger::{cli::NoCliArgs, App, Trigger, TriggerApp};
9+
use spin_trigger::{cli::NoGlobalConfig, App, Trigger, TriggerApp};
1010
use spin_world::exports::fermyon::spin::inbound_redis;
1111
use tracing::{instrument, Level};
1212

@@ -34,11 +34,11 @@ struct TriggerConfig {
3434
impl<F: RuntimeFactors> Trigger<F> for RedisTrigger {
3535
const TYPE: &'static str = "redis";
3636

37-
type CliArgs = NoCliArgs;
37+
type GlobalConfig = NoGlobalConfig;
3838

3939
type InstanceState = ();
4040

41-
fn new(_cli_args: Self::CliArgs, _app: &App) -> anyhow::Result<Self> {
41+
fn new(_cfg: Self::GlobalConfig, _app: &App) -> anyhow::Result<Self> {
4242
Ok(Self)
4343
}
4444

crates/trigger/src/cli.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ pub const SPIN_WORKING_DIR: &str = "SPIN_WORKING_DIR";
4141
usage = "spin [COMMAND] [OPTIONS]",
4242
next_help_heading = help_heading::<T, B::Factors>()
4343
)]
44-
pub struct FactorsTriggerCommand<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> {
44+
pub struct FactorsTriggerCommand<T, B>
45+
where
46+
T: Trigger<B::Factors>,
47+
T::GlobalConfig: Args,
48+
B: RuntimeFactorsBuilder,
49+
{
4550
/// Log directory for the stdout and stderr of components. Setting to
4651
/// the empty string disables logging to disk.
4752
#[clap(
@@ -110,7 +115,7 @@ pub struct FactorsTriggerCommand<T: Trigger<B::Factors>, B: RuntimeFactorsBuilde
110115
pub state_dir: Option<String>,
111116

112117
#[clap(flatten)]
113-
pub trigger_args: T::CliArgs,
118+
pub trigger_args: T::GlobalConfig,
114119

115120
#[clap(flatten)]
116121
pub builder_args: B::CliArgs,
@@ -139,12 +144,17 @@ pub struct FactorsConfig {
139144
pub log_dir: UserProvidedPath,
140145
}
141146

142-
/// An empty implementation of clap::Args to be used as TriggerExecutor::RunConfig
143-
/// for executors that do not need additional CLI args.
147+
/// This type may be used as the [`Trigger::GlobalConfig`] for triggers with no
148+
/// CLI args.
144149
#[derive(Args)]
145-
pub struct NoCliArgs;
146-
147-
impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> FactorsTriggerCommand<T, B> {
150+
pub struct NoGlobalConfig;
151+
152+
impl<T, B> FactorsTriggerCommand<T, B>
153+
where
154+
T: Trigger<B::Factors>,
155+
T::GlobalConfig: Args,
156+
B: RuntimeFactorsBuilder,
157+
{
148158
/// Create a new TriggerExecutorBuilder from this TriggerExecutorCommand.
149159
pub async fn run(self) -> Result<()> {
150160
// Handle --help-args-only
@@ -383,10 +393,10 @@ pub mod help {
383393

384394
impl<F: RuntimeFactors> Trigger<F> for HelpArgsOnlyTrigger {
385395
const TYPE: &'static str = "help-args-only";
386-
type CliArgs = NoCliArgs;
396+
type GlobalConfig = NoGlobalConfig;
387397
type InstanceState = ();
388398

389-
fn new(_cli_args: Self::CliArgs, _app: &App) -> anyhow::Result<Self> {
399+
fn new(_cfg: Self::GlobalConfig, _app: &App) -> anyhow::Result<Self> {
390400
Ok(Self)
391401
}
392402

crates/trigger/src/cli/launch_metadata.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::CommandFactory;
1+
use clap::{Args, CommandFactory};
22
use serde::{Deserialize, Serialize};
33
use std::ffi::OsString;
44

@@ -29,7 +29,12 @@ struct LaunchFlag {
2929
}
3030

3131
impl LaunchMetadata {
32-
pub fn infer<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder>() -> Self {
32+
pub fn infer<T, B>() -> Self
33+
where
34+
T: Trigger<B::Factors>,
35+
T::GlobalConfig: Args,
36+
B: RuntimeFactorsBuilder,
37+
{
3338
let all_flags: Vec<_> = FactorsTriggerCommand::<T, B>::command()
3439
.get_arguments()
3540
.map(LaunchFlag::infer)

crates/trigger/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ pub mod loader;
33

44
use std::future::Future;
55

6-
use clap::Args;
76
pub use spin_core::Linker;
87
pub use spin_factors::RuntimeFactors;
98
use spin_factors_executor::{FactorsExecutorApp, FactorsInstanceBuilder};
109

1110
pub use anyhow;
12-
pub use clap::Parser;
1311
pub use spin_app::App;
1412

1513
/// Type alias for a [`spin_factors_executor::FactorsExecutorApp`] specialized to a [`Trigger`].
@@ -33,14 +31,14 @@ pub trait Trigger<F: RuntimeFactors>: Sized + Send {
3331
/// A unique identifier for this trigger.
3432
const TYPE: &'static str;
3533

36-
/// The specific CLI arguments for this trigger.
37-
type CliArgs: Args;
34+
/// Global configuration used to construct this trigger.
35+
type GlobalConfig;
3836

39-
/// The instance state for this trigger.
37+
/// Extra state attached to each instance store for this trigger.
4038
type InstanceState: Send + 'static;
4139

4240
/// Constructs a new trigger.
43-
fn new(cli_args: Self::CliArgs, app: &App) -> anyhow::Result<Self>;
41+
fn new(cfg: Self::GlobalConfig, app: &App) -> anyhow::Result<Self>;
4442

4543
/// Update the [`spin_core::Config`] for this trigger.
4644
///

examples/spin-timer/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ wasmtime::component::bindgen!({
1212
});
1313

1414
#[derive(Args)]
15-
pub struct CliArgs {
15+
pub struct GlobalConfig {
1616
/// If true, run each component once and exit
1717
#[clap(long)]
1818
pub test: bool,
@@ -49,11 +49,11 @@ pub struct TimerTriggerConfig {
4949
impl<F: RuntimeFactors> Trigger<F> for TimerTrigger {
5050
const TYPE: &'static str = "timer";
5151

52-
type CliArgs = CliArgs;
52+
type GlobalConfig = GlobalConfig;
5353

5454
type InstanceState = ();
5555

56-
fn new(cli_args: Self::CliArgs, app: &App) -> anyhow::Result<Self> {
56+
fn new(cfg: Self::GlobalConfig, app: &App) -> anyhow::Result<Self> {
5757
let trigger_type = <Self as Trigger<F>>::TYPE;
5858
let metadata = app
5959
.get_trigger_metadata::<TriggerMetadata>(trigger_type)?
@@ -67,7 +67,7 @@ impl<F: RuntimeFactors> Trigger<F> for TimerTrigger {
6767
.collect();
6868

6969
Ok(Self {
70-
test: cli_args.test,
70+
test: cfg.test,
7171
speedup,
7272
component_timings,
7373
})

tests/testing-framework/src/runtimes/in_process_spin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::sync::Arc;
44

55
use anyhow::Context as _;
6-
use spin_runtime_factors::{FactorsBuilder, TriggerAppArgs, TriggerFactors};
6+
use spin_runtime_factors::{FactorsBuilder, TriggerFactors, TriggerAppArgs};
77
use spin_trigger::{cli::TriggerAppBuilder, loader::ComponentLoader};
88
use spin_trigger_http::{HttpServer, HttpTrigger};
99
use test_environment::{

0 commit comments

Comments
 (0)