support_kit/
args.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use clap::{Parser, Subcommand};

use crate::{
    Color, ConfigFile, Environment, ServiceCommand, ServiceConfig, ServiceManagerKind, ServiceName,
};

mod boilerplate_args;
mod deployment_args;
mod service_args;

pub use boilerplate_args::*;
pub use deployment_args::DeploymentArgs;
pub use service_args::ServiceArgs;

#[derive(Clone, Debug, Default, Parser)]
pub struct Args {
    /// The verbosity level to use. Defaults to off.
    #[arg(short, long, action = clap::ArgAction::Count, global = true)]
    pub verbose: u8,

    /// The host to bind to.
    #[arg(short = 'H', long, global = true)]
    pub host: Option<String>,

    /// The port to bind to.
    #[arg(short = 'P', long, global = true)]
    pub port: Option<i32>,

    /// The environment to use.
    #[arg(short, long, global = true)]
    pub environment: Option<Environment>,

    /// The service label to use. Defaults to the binary name.
    #[clap(long, short, global = true)]
    pub name: Option<ServiceName>,

    /// The kind of service manager to use. Defaults to system native.
    #[clap(long, value_enum, global = true)]
    pub service_manager: Option<ServiceManagerKind>,

    /// Install system-wide. If not set, attempts to install for the current user.
    #[clap(long, global = true)]
    pub system: bool,

    /// Color output.
    #[clap(long, global = true, default_value = "auto")]
    pub color: Color,

    /// The path to the configuration file.
    #[clap(long, short)]
    pub config_file: Option<ConfigFile>,

    #[command(subcommand)]
    pub command: Option<Commands>,
}

#[derive(Clone, Debug, Subcommand, PartialEq)]
#[clap(rename_all = "kebab-case")]
pub enum Commands {
    Service(ServiceArgs),
    Deploy(DeploymentArgs),
    Generate(BoilerplateArgs),
    Container(DeploymentArgs),
}

impl From<ServiceArgs> for Commands {
    fn from(args: ServiceArgs) -> Self {
        Commands::Service(args)
    }
}

impl From<ServiceCommand> for Commands {
    fn from(command: ServiceCommand) -> Self {
        ServiceArgs::from(command).into()
    }
}

impl Args {
    pub fn config(&self) -> ConfigFile {
        let service_config = self.service();

        self.config_file
            .clone()
            .unwrap_or_else(|| service_config.name().into())
    }

    pub fn service(&self) -> ServiceConfig {
        ServiceConfig::builder()
            .maybe_name(self.name.clone())
            .maybe_service_manager(self.service_manager)
            .system(self.system)
            .build()
    }
}

#[test]
fn setting_verbosity_with_args() -> Result<(), Box<dyn std::error::Error>> {
    use crate::{Configuration, Verbosity};

    let expectations = [
        ("app", Verbosity::Off),
        ("app -v", Verbosity::Error),
        ("app -vv", Verbosity::Warn),
        ("app -vvv", Verbosity::Info),
        ("app -vvvv", Verbosity::Debug),
        ("app -vvvvv", Verbosity::Trace),
    ];

    for (input, expected) in expectations.iter() {
        let args = Args::try_parse_from(input.split_whitespace())?;

        assert_eq!(
            Configuration::from(args),
            Configuration::builder().verbosity(*expected).build()
        );
    }
    Ok(())
}

#[test]
fn config_file() -> Result<(), Box<dyn std::error::Error>> {
    let expectations = [
        ("app", "support-kit"),
        ("app --config-file custom.config", "custom.config"),
    ];

    for (input, expected) in expectations {
        let args = Args::try_parse_from(input.split_whitespace())?;

        assert_eq!(args.config(), expected.into());
    }

    figment::Jail::expect_with(|jail| {
        jail.set_env("CARGO_PKG_NAME", "custom-package");

        let args = Args::try_parse_from("app".split_whitespace()).unwrap();

        assert_eq!(args.config(), "custom-package".into());

        Ok(())
    });

    Ok(())
}

#[test]
fn setting_environment_with_args() -> Result<(), Box<dyn std::error::Error>> {
    use crate::Configuration;

    let expectations = [
        ("app", None),
        (
            "app --environment development",
            Some(Environment::Development),
        ),
        (
            "app --environment production",
            Some(Environment::Production),
        ),
        ("app --environment test", Some(Environment::Test)),
    ];

    for (input, expected) in expectations {
        let args = Args::try_parse_from(input.split_whitespace())?;

        assert_eq!(
            Configuration::from(args),
            Configuration::builder().maybe_environment(expected).build()
        );
    }
    Ok(())
}

#[test]
fn setting_color_with_args() -> Result<(), Box<dyn std::error::Error>> {
    use crate::Configuration;

    let expectations = [
        ("app", Color::Auto),
        ("app --color always", Color::Always),
        ("app --color never", Color::Never),
        ("app --color auto", Color::Auto),
    ];

    for (input, expected) in expectations {
        let args = Args::try_parse_from(input.split_whitespace())?;

        assert_eq!(
            Configuration::from(args),
            Configuration::builder().color(expected).build()
        );
    }
    Ok(())
}

#[test]
fn setting_server_with_args() -> Result<(), Box<dyn std::error::Error>> {
    use crate::{Configuration, NetworkConfig};
    let expectations = [
        ("app", NetworkConfig::default()),
        ("app -H localhost", NetworkConfig::from("localhost")),
        ("app -P 8080", NetworkConfig::builder().port(8080).build()),
        (
            "app -H localhost -P 8080",
            NetworkConfig::from(("localhost", 8080)),
        ),
    ];

    for (input, expected) in expectations {
        let args = Args::try_parse_from(input.split_whitespace())?;

        assert_eq!(
            Configuration::from(args),
            Configuration::builder().server(expected.clone()).build()
        );
    }
    Ok(())
}