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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::{collections::HashSet, path::PathBuf};

use clap::CommandFactory;

use crate::{errors::TogetherResult, log, log_err, t_println, terminal};

#[derive(Debug, Clone)]
pub struct StartTogetherOptions {
    pub config: TogetherConfigFile,
    pub working_directory: Option<String>,
    pub active_recipes: Option<Vec<String>>,
    pub config_path: Option<std::path::PathBuf>,
}

pub fn to_start_options(command_args: terminal::TogetherArgs) -> StartTogetherOptions {
    #[derive(Default)]
    struct StartMeta {
        config_path: Option<std::path::PathBuf>,
        recipes: Option<Vec<String>>,
    }
    let (config, meta) = match command_args.command {
        Some(terminal::ArgsCommands::Run(run_opts)) => {
            let mut config_start_opts: commands::ConfigFileStartOptions = run_opts.into();
            let meta = StartMeta::default();
            config_start_opts.quiet_startup = command_args.quiet_startup;
            (TogetherConfigFile::new(config_start_opts), meta)
        }

        Some(terminal::ArgsCommands::Rerun(_)) => {
            if command_args.no_config {
                log_err!("To use rerun, you must have a configuration file");
                std::process::exit(1);
            }
            let config = load();
            let config = config
                .map_err(|e| {
                    log_err!("Failed to load configuration: {}", e);
                    std::process::exit(1);
                })
                .unwrap();
            let config_path: PathBuf = path();
            let meta = StartMeta {
                config_path: Some(config_path),
                ..StartMeta::default()
            };
            (config, meta)
        }

        Some(terminal::ArgsCommands::Load(load)) => {
            if command_args.no_config {
                log_err!("To use rerun, you must have a configuration file");
                std::process::exit(1);
            }
            let config = load_from(&load.path);
            let mut config = config
                .map_err(|e| {
                    log_err!("Failed to load configuration from '{}': {}", load.path, e);
                    std::process::exit(1);
                })
                .unwrap();
            let config_path: PathBuf = load.path.into();
            config.start_options.init_only = load.init_only;
            config.start_options.quiet_startup = command_args.quiet_startup;
            let meta = StartMeta {
                config_path: Some(config_path),
                recipes: load.recipes,
            };
            (config, meta)
        }

        None => (!command_args.no_config)
            .then_some(())
            .and_then(|()| load_from("together.toml").ok())
            .map_or_else(
                || {
                    _ = terminal::TogetherArgs::command().print_long_help();
                    std::process::exit(1);
                },
                |mut config| {
                    let config_start_opts = &mut config.start_options;
                    config_start_opts.init_only = command_args.init_only;
                    config_start_opts.quiet_startup = command_args.quiet_startup;
                    let meta = StartMeta {
                        config_path: Some("together.toml".into()),
                        recipes: command_args.recipes,
                    };
                    (config, meta)
                },
            ),
    };

    StartTogetherOptions {
        config,
        working_directory: command_args.working_directory,
        active_recipes: meta.recipes,
        config_path: meta.config_path,
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TogetherConfigFile {
    #[serde(flatten)]
    pub start_options: commands::ConfigFileStartOptions,
    pub running: Option<Vec<commands::CommandIndex>>,
    pub startup: Option<Vec<commands::CommandIndex>>,
    pub version: Option<String>,
}

impl TogetherConfigFile {
    fn new(start_options: commands::ConfigFileStartOptions) -> Self {
        Self {
            start_options,
            running: None,
            startup: None,
            version: Some(env!("CARGO_PKG_VERSION").to_string()),
        }
    }

    pub fn with_running(self, running: &[impl AsRef<str>]) -> Self {
        let running = running
            .iter()
            .map(|c| {
                self.start_options
                    .commands
                    .iter()
                    .position(|x| x.matches(c.as_ref()))
                    .unwrap()
                    .into()
            })
            .collect();

        Self {
            running: Some(running),
            ..self
        }
    }

    pub fn running_commands(&self) -> Option<Vec<&str>> {
        let running = self
            .running
            .iter()
            .flatten()
            .flat_map(|index| index.retrieve(&self.start_options.commands))
            .chain(self.start_options.commands.iter().filter(|c| c.is_active()))
            .fold(vec![], |mut acc, c| {
                if !acc.contains(&c) {
                    acc.push(c);
                }
                acc
            });

        if running.is_empty() {
            None
        } else {
            Some(running.into_iter().map(|c| c.as_str()).collect())
        }
    }
}

pub fn load_from(config_path: impl AsRef<std::path::Path>) -> TogetherResult<TogetherConfigFile> {
    let config = std::fs::read_to_string(config_path)?;
    let config: TogetherConfigFile = toml::from_str(&config)?;
    check_version(&config);
    Ok(config)
}

pub fn load() -> TogetherResult<TogetherConfigFile> {
    let config_path = path();
    log!("Loading configuration from: {:?}", config_path);
    load_from(config_path)
}

pub fn save(
    config: &TogetherConfigFile,
    config_path: Option<&std::path::Path>,
) -> TogetherResult<()> {
    let default_path = path();
    let config_path = config_path.unwrap_or_else(|| default_path.as_path());
    log!("Saving configuration to: {:?}", config_path);
    let config = toml::to_string_pretty(config)?;
    std::fs::write(config_path, config)?;
    Ok(())
}

pub fn dump(config: &TogetherConfigFile) -> TogetherResult<()> {
    let config = toml::to_string(config)?;
    t_println!("Configuration:");
    t_println!();
    t_println!("{}", config);
    Ok(())
}

pub fn get_running_commands(
    config: &TogetherConfigFile,
    running: &[commands::CommandIndex],
) -> Vec<String> {
    let commands: Vec<String> = running
        .iter()
        .filter_map(|index| {
            index
                .retrieve(&config.start_options.commands)
                .map(|c| c.as_str().to_string())
        })
        .collect();
    commands
}

pub fn get_unique_recipes(start_options: &commands::ConfigFileStartOptions) -> HashSet<&String> {
    start_options
        .commands
        .iter()
        .flat_map(|c| c.recipes())
        .collect::<HashSet<_>>()
}

pub fn collect_commands_by_recipes(
    start_options: &commands::ConfigFileStartOptions,
    recipes: &[impl AsRef<str>],
) -> Vec<String> {
    let selected_commands = start_options
        .commands
        .iter()
        .filter(|c| recipes.iter().any(|r| c.contains_recipe(r.as_ref())))
        .map(|c| c.as_str().to_string())
        .collect();
    selected_commands
}

fn path() -> std::path::PathBuf {
    dirs::config_dir().unwrap().join(".together.toml")
}

fn check_version(config: &TogetherConfigFile) {
    let Some(version) = &config.version else {
        log_err!(
            "The configuration file was created with a different version of together. \
            Please update together to the latest version."
        );
        std::process::exit(1);
    };
    let current_version = env!("CARGO_PKG_VERSION");
    let current_version = semver::Version::parse(current_version).unwrap();
    let config_version = semver::Version::parse(version).unwrap();
    if current_version.major < config_version.major {
        log_err!(
            "The configuration file was created with a more recent version of together (>={config_version}). \
            Please update together to the latest version."
        );
        std::process::exit(1);
    }

    if current_version.minor < config_version.minor {
        log!(
            "Using configuration file created with a more recent version of together (>={config_version}). \
            Some features may not be available."
        );
    }
}

pub mod commands {
    use serde::{Deserialize, Serialize};

    use crate::terminal;

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct ConfigFileStartOptions {
        pub commands: Vec<CommandConfig>,
        #[serde(default)]
        pub all: bool,
        #[serde(default)]
        pub exit_on_error: bool,
        #[serde(default)]
        pub quit_on_completion: bool,
        #[serde(default)]
        pub quiet_startup: bool,
        #[serde(default = "defaults::true_value")]
        pub raw: bool,
        #[serde(skip)]
        pub init_only: bool,
    }

    mod defaults {
        pub fn true_value() -> bool {
            true
        }
    }

    impl From<terminal::RunCommand> for ConfigFileStartOptions {
        fn from(args: terminal::RunCommand) -> Self {
            Self {
                commands: args.commands.iter().map(|c| c.as_str().into()).collect(),
                all: args.all,
                exit_on_error: args.exit_on_error,
                quit_on_completion: args.quit_on_completion,
                quiet_startup: false,
                raw: args.raw,
                init_only: args.init_only,
            }
        }
    }

    impl From<ConfigFileStartOptions> for terminal::RunCommand {
        fn from(config: ConfigFileStartOptions) -> Self {
            Self {
                commands: config
                    .commands
                    .iter()
                    .map(|c| c.as_str().to_string())
                    .collect(),
                all: config.all,
                exit_on_error: config.exit_on_error,
                quit_on_completion: config.quit_on_completion,
                raw: config.raw,
                init_only: config.init_only,
            }
        }
    }

    impl ConfigFileStartOptions {
        pub fn as_commands(&self) -> Vec<String> {
            self.commands
                .iter()
                .map(|c| c.as_str().to_string())
                .collect()
        }
    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    #[serde(untagged)]
    pub enum CommandConfig {
        Simple(String),
        Detailed {
            command: String,
            alias: Option<String>,
            active: Option<bool>,
            recipes: Option<Vec<String>>,
        },
    }

    impl CommandConfig {
        pub fn as_str(&self) -> &str {
            match self {
                Self::Simple(s) => s,
                Self::Detailed { command, .. } => command,
            }
        }

        pub fn alias(&self) -> Option<&str> {
            match self {
                Self::Simple(_) => None,
                Self::Detailed { alias, .. } => alias.as_deref(),
            }
        }

        pub fn is_active(&self) -> bool {
            match self {
                Self::Simple(_) => false,
                Self::Detailed { active, .. } => active.unwrap_or(false),
            }
        }

        pub fn matches(&self, other: &str) -> bool {
            self.as_str() == other || self.alias().map_or(false, |a| a == other)
        }

        pub fn recipes(&self) -> &[String] {
            match self {
                Self::Simple(_) => &[],
                Self::Detailed { recipes, .. } => recipes.as_deref().unwrap_or(&[]),
            }
        }

        pub fn contains_recipe(&self, recipe: &str) -> bool {
            let recipe = recipe.trim();
            match self {
                Self::Simple(_) => false,
                Self::Detailed { recipes, .. } => recipes
                    .as_ref()
                    .map_or(false, |r| r.iter().any(|x| x.eq_ignore_ascii_case(recipe))),
            }
        }
    }

    impl From<&str> for CommandConfig {
        fn from(v: &str) -> Self {
            Self::Simple(v.to_string())
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[serde(untagged)]
    pub enum CommandIndex {
        Simple(usize),
        Alias(String),
    }

    impl CommandIndex {
        pub fn retrieve<'a>(&self, commands: &'a [CommandConfig]) -> Option<&'a CommandConfig> {
            match self {
                Self::Simple(i) => commands.get(*i),
                Self::Alias(alias) => commands
                    .iter()
                    .find(|c| c.alias() == Some(alias))
                    .or_else(|| commands.iter().find(|c| c.as_str() == alias)),
            }
        }
    }

    impl From<usize> for CommandIndex {
        fn from(v: usize) -> Self {
            Self::Simple(v)
        }
    }

    impl From<&str> for CommandIndex {
        fn from(v: &str) -> Self {
            Self::Alias(v.to_string())
        }
    }
}