-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmod.rs
264 lines (238 loc) · 7.6 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
mod postgres_config;
mod redis_config;
use crate::error::Result;
use clap::{Args, Parser, Subcommand, ValueEnum};
use derivative::Derivative;
use editoast_derive::EditoastError;
pub use postgres_config::PostgresConfig;
pub use redis_config::RedisConfig;
use std::{env, path::PathBuf};
use thiserror::Error;
use url::Url;
#[derive(Parser, Debug)]
#[command(author, version)]
pub struct Client {
#[command(flatten)]
pub postgres_config: PostgresConfig,
#[command(flatten)]
pub redis_config: RedisConfig,
#[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(ElectricalProfilesCommands),
ImportRollingStock(ImportRollingStockArgs),
OsmToRailjson(OsmToRailjsonArgs),
#[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 = "Trains related commands")]
Trains(TrainsCommands),
}
#[derive(Subcommand, Debug)]
pub enum TrainsCommands {
Import(ImportTrainArgs),
}
#[derive(Args, Debug, Derivative)]
#[derivative(Default)]
#[command(about, long_about = "Import a train given a JSON file")]
pub struct ImportTrainArgs {
#[arg(long, help = "The timetable id on which attach the trains to")]
pub timetable: Option<i64>,
pub path: PathBuf,
}
#[derive(Subcommand, Debug)]
pub enum ElectricalProfilesCommands {
Import(ImportProfileSetArgs),
Delete(DeleteProfileSetArgs),
List(ListProfileSetArgs),
}
#[derive(Subcommand, Debug)]
pub enum SearchCommands {
List,
MakeMigration(MakeMigrationArgs),
Refresh(RefreshArgs),
}
#[derive(Subcommand, Debug)]
pub enum InfraCommands {
Clone(InfraCloneArgs),
Clear(ClearArgs),
Generate(GenerateArgs),
ImportRailjson(ImportRailjsonArgs),
}
#[derive(Args, Debug, Derivative, Clone)]
#[derivative(Default)]
pub struct MapLayersConfig {
#[derivative(Default(value = "18"))]
#[arg(long, env, default_value_t = 18)]
pub max_zoom: u64,
/// Number maximum of tiles before we consider invalidating full Redis cache is required
#[derivative(Default(value = "250_000"))]
#[arg(long, env, default_value_t = 250_000)]
pub max_tiles: u64,
}
#[derive(Args, Debug, Derivative)]
#[derivative(Default)]
#[command(about, long_about = "Launch the server")]
pub struct RunserverArgs {
#[command(flatten)]
pub map_layers_config: MapLayersConfig,
#[derivative(Default(value = "8090"))]
#[arg(long, env = "EDITOAST_PORT", default_value_t = 8090)]
pub port: u16,
#[derivative(Default(value = r#""0.0.0.0".into()"#))]
#[arg(long, env = "EDITOAST_ADDRESS", default_value_t = String::from("0.0.0.0"))]
pub address: String,
#[derivative(Default(value = r#""http://localhost:8080".into()"#))]
#[clap(long, env = "OSRD_BACKEND_URL", default_value_t = String::from("http://localhost:8080"))]
pub backend_url: String,
#[clap(long, env = "OSRD_BACKEND_TOKEN", default_value_t = String::from(""))]
pub backend_token: String,
#[arg(long, env = "SENTRY_DSN")]
pub sentry_dsn: Option<String>,
#[arg(long, env = "SENTRY_ENV")]
pub sentry_env: Option<String>,
#[derivative(Default(value = r#""".into()"#))]
#[clap(long, env = "ROOT_PATH", default_value_t = String::new())]
pub root_path: String,
#[clap(long)]
pub workers: Option<usize>,
}
#[derive(Args, Debug)]
#[command(about, long_about = "Refresh infra generated data")]
pub struct GenerateArgs {
/// List of infra ids
pub infra_ids: Vec<u64>,
#[arg(short, long)]
/// Force the refresh of an infra (even if the generated version is up to date)
pub force: bool,
}
#[derive(Args, Debug)]
#[command(about, long_about = "Clear infra generated data")]
pub struct ClearArgs {
/// List of infra ids
pub infra_ids: Vec<u64>,
}
#[derive(Args, Debug, Clone)]
#[command(about, long_about = "Import an infra given a railjson file")]
pub struct ImportRailjsonArgs {
/// Infra name
pub infra_name: String,
/// Railjson file path
pub railjson_path: PathBuf,
/// Whether the import should refresh generated data
#[arg(short = 'g', long)]
pub generate: bool,
}
#[derive(Args, Debug)]
#[command(about, long_about = "Add a set of electrical profiles")]
pub struct ImportProfileSetArgs {
/// Electrical profile set name
pub name: String,
/// Electrical profile set file path
pub electrical_profile_set_path: PathBuf,
}
#[derive(Args, Debug, Clone)]
#[command(about, long_about = "Clone an infrastructure")]
pub struct InfraCloneArgs {
/// Infrastructure ID
pub id: u64,
/// Infrastructure new name
pub new_name: Option<String>,
}
#[derive(Args, Debug)]
#[command(
about,
long_about = "Delete electrical profile sets corresponding to the given ids"
)]
pub struct DeleteProfileSetArgs {
/// List of infra ids
pub profile_set_ids: Vec<i64>,
}
#[derive(Args, Debug)]
#[command(
about,
long_about = "List electrical profile sets in the database, <id> - <name>"
)]
pub struct ListProfileSetArgs {
// Wether to display the list in a ready to parse format
#[arg(long, default_value_t = false)]
pub quiet: bool,
}
#[derive(Args, Debug)]
#[command(about, long_about = "Import a rolling stock given a json file")]
pub struct ImportRollingStockArgs {
/// Rolling stock file path
pub rolling_stock_path: Vec<PathBuf>,
}
#[derive(Args, Debug)]
#[command(about, long_about = "Extracts a railjson from OpenStreetMap data")]
pub struct OsmToRailjsonArgs {
/// Input file in the OSM PBF format
pub osm_pbf_in: PathBuf,
/// Output file in Railjson format
pub railjson_out: PathBuf,
}
#[derive(Args, Debug)]
#[command(
about,
long_about = "Generate a migration's up.sql and down.sql content for a search object"
)]
pub struct MakeMigrationArgs {
/// The search object to generate a migration for
pub object: String,
/// The directory of the migration
pub migration: PathBuf,
#[arg(short, long)]
/// Overwrites the existing up.sql and down.sql files' content
pub force: bool,
}
#[derive(Args, Debug)]
#[command(about, long_about = "Updates the content of the search cache tables")]
pub struct RefreshArgs {
/// The search objects to refresh. If none, all search objects are refreshed
pub objects: Vec<String>,
}
/// 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_assets_path() -> PathBuf {
env::var("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 },
}