1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use super::*;
use anyhow::Result;
use clap::{Parser, ValueEnum};
#[cfg(feature = "docker")]
use docker::options::DockerBuilderOptions;
use std::sync::Arc;

#[derive(ValueEnum, PartialEq, Debug, Clone, Copy)]
enum StrategyName {
    #[cfg(feature = "docker")]
    Docker,
}

/// Command-line options for builder strategy.
#[derive(Parser, PartialEq, Debug)]
pub struct StrategyOptions {
    #[clap(long, default_value = "docker")]
    strategy: StrategyName,

    #[cfg(feature = "docker")]
    #[clap(flatten)]
    docker: DockerBuilderOptions,
}

#[test]
fn can_parse_default_options() {
    use std::iter::empty;

    #[cfg(feature = "docker")]
    assert_eq!(
        StrategyOptions::parse_from(empty::<String>()),
        StrategyOptions {
            strategy: StrategyName::Docker,
            docker: DockerBuilderOptions::parse_from(empty::<String>()),
        }
    );
}

impl StrategyOptions {
    /// Build strategy
    pub async fn build(&self) -> Result<DynStrategy> {
        let strategy: DynStrategy = match self.strategy {
            #[cfg(feature = "docker")]
            StrategyName::Docker => Arc::new(self.docker.build().await?),
        };

        Ok(strategy)
    }
}