mps-rs 1.7.0

MPS — plain-text personal productivity CLI (Rust)
Documentation
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
mod api;
mod cli;
mod commands;
mod config;
mod constants;
mod date_parse;
mod elements;
mod error;
mod meta;
mod parser;
mod ref_resolver;
mod store;
mod time_parse;

use anyhow::{Context, Result};
use chrono::NaiveDate;
use clap::Parser as ClapParser;
use cli::{Cli, Commands};
use config::{default_config_path, Config};
use meta::MetaShared;
use std::collections::HashMap;
use std::path::PathBuf;

fn main() {
    if let Err(e) = run() {
        eprintln!("error: {:#}", e);
        std::process::exit(1);
    }
}

fn run() -> Result<()> {
    let raw_args: Vec<String> = std::env::args().collect();

    // Load command aliases before clap parsing so we can substitute them in args.
    // We do a best-effort early config load (silently falls back to empty map on failure).
    let early_config_path = extract_config_path_arg(&raw_args).unwrap_or_else(default_config_path);

    // Best-effort early load: YAML aliases + meta aliases merged, for pre-clap substitution.
    let command_aliases: HashMap<String, String> = if early_config_path.exists() {
        let mut aliases = Config::load(&early_config_path)
            .map(|c| c.command_aliases)
            .unwrap_or_default();
        // Also pull command_aliases from .mps.meta if storage_dir is resolvable.
        if let Ok(cfg) = Config::load(&early_config_path) {
            let meta = MetaShared::load(&cfg.storage_dir);
            for (k, v) in meta.config.command_aliases {
                aliases.entry(k).or_insert(v);
            }
        }
        aliases
    } else {
        HashMap::new()
    };
    let cli_args = resolve_command_alias_in_args(raw_args, &command_aliases);

    let cli = Cli::parse_from(cli_args);

    let config_path = cli
        .config_path
        .as_deref()
        .map(PathBuf::from)
        .unwrap_or_else(default_config_path);

    if !config_path.exists() || cli.force {
        Config::init(&config_path)
            .with_context(|| format!("failed to init config at {}", config_path.display()))?;
    }

    let mut config = Config::load(&config_path)
        .with_context(|| format!("failed to load config from {}", config_path.display()))?;

    config
        .ensure_dirs()
        .context("failed to create MPS directories")?;

    // Load .mps.meta and merge machine-agnostic config overrides.
    let meta_shared = MetaShared::load(&config.storage_dir);
    config.merge_meta(&meta_shared.config);

    // Resolve the default command from config for bare invocations.
    let default_cmd = config.default_command.to_lowercase();

    let command = cli.command.unwrap_or(match default_cmd.as_str() {
        "list" => Commands::List {
            datesign: None,
            r#type: None,
            tag: None,
            status: None,
            since: None,
            refs: false,
            all: false,
            name: None,
        },
        _ => Commands::Open { datesign: None },
    });

    match command {
        Commands::Version => {
            println!("mps (v{})", env!("CARGO_PKG_VERSION"));
        }

        Commands::Open { datesign } => {
            let date = resolve_date(datesign.as_deref().unwrap_or("today"))?;
            commands::open::run(&config, date)?;
        }

        Commands::List {
            datesign,
            r#type,
            tag,
            status,
            since,
            refs,
            all,
            name,
        } => {
            let date = resolve_date(datesign.as_deref().unwrap_or("today"))?;
            let dates = resolve_date_range(since.as_deref(), date)?;
            commands::list::run(&config, date, dates, r#type, tag, status, name, refs, all)?;
        }

        Commands::Append {
            kind,
            body,
            tags,
            status,
            at,
            start_time,
            end_time,
            name,
        } => {
            let body_str = body.join(" ");
            let kind_resolved = resolve_alias(&kind, &config);
            let tags_vec: Vec<String> = tags
                .as_deref()
                .unwrap_or("")
                .split(',')
                .map(|t| t.trim().to_string())
                .filter(|t| !t.is_empty())
                .collect();
            let date = chrono::Local::now().date_naive();
            commands::append::run(
                &config,
                &kind_resolved,
                &body_str,
                tags_vec,
                status,
                at,
                start_time,
                end_time,
                name,
                date,
            )?;
        }

        Commands::Edit { ref_path, date } => {
            let d = resolve_date(date.as_deref().unwrap_or("today"))?;
            commands::edit::run(&config, &ref_path, d)?;
        }

        Commands::Delete {
            ref_path,
            date,
            yes,
        } => {
            let d = resolve_date(date.as_deref().unwrap_or("today"))?;
            commands::delete::run(&config, &ref_path, d, yes)?;
        }

        Commands::Update {
            ref_path,
            status,
            start_time,
            end_time,
            at,
            date,
        } => {
            commands::update::run(&config, &ref_path, status, start_time, end_time, at, date)?;
        }

        Commands::Done { ref_path, date } => {
            commands::update::run_done(&config, &ref_path, date)?;
        }

        Commands::Search {
            query,
            r#type,
            tag,
            since,
            name,
        } => {
            let since_date = since.as_deref().map(date_parse::parse_date).transpose()?;
            commands::search::run(&config, &query, r#type, tag, name, since_date)?;
        }

        Commands::Stats {
            datesign,
            since,
            all,
        } => {
            let date = resolve_date(datesign.as_deref().unwrap_or("today"))?;
            let dates = resolve_date_range(since.as_deref(), date)?;
            commands::stats::run(&config, dates, all)?;
        }

        Commands::Tags {
            datesign,
            r#type,
            status,
            since,
            all,
            name,
        } => {
            let date = resolve_date(datesign.as_deref().unwrap_or("today"))?;
            let dates = resolve_date_range(since.as_deref(), date)?;
            commands::tags::run(&config, dates, all, r#type, status, name)?;
        }

        Commands::Export {
            datesign,
            format,
            r#type,
            since,
        } => {
            let date = resolve_date(datesign.as_deref().unwrap_or("today"))?;
            let dates = resolve_date_range(since.as_deref(), date)?;
            commands::export::run(&config, dates, &format, r#type)?;
        }

        Commands::Config { subcommand } => {
            commands::config_cmd::run(&config, &config_path, subcommand.as_deref())?;
        }

        Commands::Git { args } => {
            commands::git::run_git(&config, &args)?;
        }

        Commands::Autogit => {
            commands::git::run_autogit(&config)?;
        }

        Commands::Cmd { args } => {
            commands::git::run_cmd(&config, &args)?;
        }

        Commands::Notify {
            dry_run,
            window,
            force,
        } => {
            commands::notify::run(&config, dry_run, window, force)?;
        }

        Commands::Daemon { subcommand } => {
            commands::daemon::run(&config, &subcommand)?;
        }

        Commands::Meta { args } => {
            commands::meta_cmd::run(&config, &config_path, &args)?;
        }

        Commands::Serve { port, host, token } => {
            if let Some(p) = port {
                config.serve.port = p;
            }
            if let Some(h) = host {
                config.serve.host = h;
            }
            if let Some(t) = token {
                config.serve.token = t;
            }
            let rt = tokio::runtime::Runtime::new()?;
            rt.block_on(commands::serve::run(config, config_path))?;
        }
    }

    Ok(())
}

/// Resolve a type string through the config's type_aliases map.
/// e.g. "t" → "task" if type_aliases contains {"t": "task"}.
fn resolve_alias(kind: &str, config: &Config) -> String {
    let normalized = kind.to_lowercase();
    config
        .type_aliases
        .get(&normalized)
        .cloned()
        .unwrap_or(normalized)
}

/// Scan raw args for --config-path <value> or --config-path=<value>.
/// Used to load the config before clap parses it (for command alias resolution).
fn extract_config_path_arg(args: &[String]) -> Option<PathBuf> {
    let mut iter = args.iter().skip(1);
    while let Some(arg) = iter.next() {
        if arg == "--config-path" {
            return iter.next().map(PathBuf::from);
        }
        if let Some(val) = arg.strip_prefix("--config-path=") {
            return Some(PathBuf::from(val));
        }
    }
    None
}

/// Replace the subcommand token in args with its command alias, if any.
/// Only the first positional argument (the subcommand) is substituted.
/// Flag tokens (starting with `-`) and their values are skipped.
fn resolve_command_alias_in_args(
    mut args: Vec<String>,
    command_aliases: &HashMap<String, String>,
) -> Vec<String> {
    if command_aliases.is_empty() {
        return args;
    }
    let mut i = 1usize;
    while i < args.len() {
        let arg = &args[i];
        if arg == "--config-path" {
            i += 2; // skip flag and its value argument
            continue;
        }
        if arg.starts_with('-') {
            i += 1; // skip boolean flags
            continue;
        }
        // First positional token is the subcommand — substitute if aliased
        let lower = arg.to_lowercase();
        if let Some(resolved) = command_aliases.get(&lower) {
            args[i] = resolved.clone();
        }
        break;
    }
    args
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_config_path_arg_present() {
        let args = vec![
            "mps".into(),
            "--config-path".into(),
            "/tmp/cfg.yaml".into(),
            "list".into(),
        ];
        assert_eq!(
            extract_config_path_arg(&args),
            Some(PathBuf::from("/tmp/cfg.yaml"))
        );
    }

    #[test]
    fn test_extract_config_path_arg_equals_form() {
        let args = vec![
            "mps".into(),
            "--config-path=/tmp/cfg.yaml".into(),
            "list".into(),
        ];
        assert_eq!(
            extract_config_path_arg(&args),
            Some(PathBuf::from("/tmp/cfg.yaml"))
        );
    }

    #[test]
    fn test_extract_config_path_arg_absent() {
        let args = vec!["mps".into(), "list".into()];
        assert_eq!(extract_config_path_arg(&args), None);
    }

    #[test]
    fn test_resolve_command_alias_basic() {
        let aliases = HashMap::from([("a".to_string(), "append".to_string())]);
        let args = vec!["mps".into(), "a".into(), "note".into(), "body".into()];
        let out = resolve_command_alias_in_args(args, &aliases);
        assert_eq!(out[1], "append");
        assert_eq!(out[2], "note"); // type alias untouched here
    }

    #[test]
    fn test_resolve_command_alias_skips_config_path_flag() {
        let aliases = HashMap::from([("a".to_string(), "append".to_string())]);
        let args = vec![
            "mps".into(),
            "--config-path".into(),
            "/tmp/x".into(),
            "a".into(),
            "n".into(),
        ];
        let out = resolve_command_alias_in_args(args, &aliases);
        assert_eq!(out[3], "append");
        assert_eq!(out[1], "--config-path"); // flag untouched
    }

    #[test]
    fn test_resolve_command_alias_skips_boolean_flag() {
        let aliases = HashMap::from([("a".to_string(), "append".to_string())]);
        let args = vec!["mps".into(), "--force".into(), "a".into(), "note".into()];
        let out = resolve_command_alias_in_args(args, &aliases);
        assert_eq!(out[2], "append");
    }

    #[test]
    fn test_resolve_command_alias_no_match_unchanged() {
        let aliases = HashMap::from([("a".to_string(), "append".to_string())]);
        let args = vec!["mps".into(), "list".into()];
        let out = resolve_command_alias_in_args(args.clone(), &aliases);
        assert_eq!(out, args);
    }

    #[test]
    fn test_resolve_command_alias_empty_map() {
        let aliases = HashMap::new();
        let args = vec!["mps".into(), "x".into()];
        let out = resolve_command_alias_in_args(args.clone(), &aliases);
        assert_eq!(out, args);
    }

    #[test]
    fn test_resolve_command_alias_symbol_like() {
        let aliases = HashMap::from([("+".to_string(), "append".to_string())]);
        let args = vec!["mps".into(), "+".into(), "note".into()];
        let out = resolve_command_alias_in_args(args, &aliases);
        assert_eq!(out[1], "append");
    }

    #[test]
    fn test_resolve_command_alias_case_insensitive() {
        let aliases = HashMap::from([("a".to_string(), "append".to_string())]);
        let args = vec!["mps".into(), "A".into(), "note".into()];
        let out = resolve_command_alias_in_args(args, &aliases);
        assert_eq!(out[1], "append");
    }
}

fn resolve_date(datesign: &str) -> Result<NaiveDate> {
    date_parse::parse_date(datesign).with_context(|| format!("cannot parse date '{}'", datesign))
}

fn resolve_date_range(since: Option<&str>, to_date: NaiveDate) -> Result<Vec<NaiveDate>> {
    match since {
        None => Ok(vec![to_date]),
        Some(s) => {
            let since_date = resolve_date(s)?;
            let mut dates = Vec::new();
            let mut d = since_date;
            while d <= to_date {
                dates.push(d);
                d += chrono::Duration::days(1);
            }
            Ok(dates)
        }
    }
}