-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmod.rs
142 lines (130 loc) · 4.18 KB
/
mod.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
pub mod electrical_profiles_commands;
pub mod group;
pub mod healthcheck;
pub mod import_rolling_stock;
pub mod infra_commands;
mod postgres_config;
pub mod roles;
pub mod runserver;
pub mod search_commands;
pub mod stdcm_search_env_commands;
mod telemetry_config;
pub mod timetables_commands;
pub mod user;
mod valkey_config;
use std::env;
use std::path::PathBuf;
use clap::Parser;
use clap::Subcommand;
use clap::ValueEnum;
use derivative::Derivative;
use editoast_derive::EditoastError;
use group::GroupCommand;
use import_rolling_stock::ImportRollingStockArgs;
use infra_commands::InfraCommands;
pub use postgres_config::PostgresConfig;
use roles::RolesCommand;
use runserver::CoreArgs;
use runserver::RunserverArgs;
use search_commands::SearchCommands;
use stdcm_search_env_commands::StdcmSearchEnvCommands;
pub use telemetry_config::TelemetryConfig;
pub use telemetry_config::TelemetryKind;
use thiserror::Error;
use timetables_commands::TimetablesCommands;
use url::Url;
use user::UserCommand;
pub use valkey_config::ValkeyConfig;
use crate::error::Result;
use crate::views::OpenApiRoot;
#[derive(Parser, Debug)]
#[command(author, version)]
pub struct Client {
#[command(flatten)]
pub postgres_config: PostgresConfig,
#[command(flatten)]
pub valkey_config: ValkeyConfig,
#[command(flatten)]
pub telemetry_config: TelemetryConfig,
#[arg(long, env, value_enum, default_value_t = Color::Auto)]
pub color: Color,
#[command(subcommand)]
pub command: Commands,
}
#[derive(ValueEnum, Debug, Derivative, Clone)]
#[derivative(Default)]
pub enum Color {
Never,
Always,
#[derivative(Default)]
Auto,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
Runserver(RunserverArgs),
#[command(
subcommand,
about,
long_about = "Commands related to electrical profile sets"
)]
ElectricalProfiles(electrical_profiles_commands::ElectricalProfilesCommands),
ImportRollingStock(ImportRollingStockArgs),
ImportTowedRollingStock(ImportRollingStockArgs),
#[command(about, long_about = "Prints the OpenApi of the service")]
Openapi,
#[command(subcommand, about, long_about = "Search engine related commands")]
Search(SearchCommands),
#[command(subcommand, about, long_about = "Infrastructure related commands")]
Infra(InfraCommands),
#[command(subcommand, about, long_about = "Timetables related commands")]
Timetables(TimetablesCommands),
#[command(
subcommand,
about,
long_about = "STDCM search environment management commands"
)]
STDCMSearchEnv(StdcmSearchEnvCommands),
#[command(subcommand, about, long_about = "Roles related commands")]
Roles(RolesCommand),
#[command(subcommand, about, long_about = "Group related commands")]
Group(GroupCommand),
#[command(subcommand, about, long_about = "User related commands")]
User(UserCommand),
#[command(about, long_about = "Healthcheck")]
Healthcheck(CoreArgs),
}
/// Prints the OpenApi to stdout
pub fn print_openapi() {
let openapi = OpenApiRoot::build_openapi();
print!("{}", serde_yaml::to_string(&openapi).unwrap());
}
/// Retrieve the ROOT_URL env var. If not found returns default local url.
pub fn get_root_url() -> Result<Url> {
let url = env::var("ROOT_URL").unwrap_or(String::from("http://localhost:8090"));
let parsed_url = Url::parse(&url).map_err(|_| EditoastUrlError::InvalidUrl { url })?;
Ok(parsed_url)
}
/// Retrieve the app version (git describe)
pub fn get_app_version() -> Option<String> {
env::var("OSRD_GIT_DESCRIBE").ok()
}
/// Retrieve the assets path
pub fn get_dynamic_assets_path() -> PathBuf {
env::var("DYNAMIC_ASSETS_PATH")
.unwrap_or(String::from("./assets"))
.into()
}
#[derive(Debug, Error, EditoastError)]
#[editoast_error(base_id = "url")]
pub enum EditoastUrlError {
#[error("Invalid url '{url}'")]
#[editoast_error(status = 500)]
InvalidUrl { url: String },
}
#[cfg(test)]
pub fn generate_temp_file<T: serde::Serialize>(object: &T) -> tempfile::NamedTempFile {
use std::io::Write as _;
let mut tmp_file = tempfile::NamedTempFile::new().unwrap();
write!(tmp_file, "{}", serde_json::to_string(object).unwrap()).unwrap();
tmp_file
}