ghtkn 0.1.1

GitHub token management — OAuth device flow with keyring caching and config-driven app selection
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Configuration management for ghtkn.
//!
//! Handles reading and validating YAML configuration files for GitHub App
//! authentication. The configuration specifies one or more GitHub Apps that
//! can be used for token generation via OAuth device flow.

use std::collections::HashSet;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::error::Error;

/// Top-level configuration containing a list of GitHub Apps.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Config {
    pub apps: Vec<App>,
}

/// A single GitHub App configuration entry.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct App {
    pub name: String,
    pub client_id: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub git_owner: String,
}

impl Config {
    /// Validate the configuration.
    ///
    /// Rules:
    /// - Must have at least one app
    /// - Each app must have non-empty `name` and `client_id`
    /// - App names must be unique
    /// - App `git_owner` values must be unique (when set)
    pub fn validate(&self) -> crate::Result<()> {
        if self.apps.is_empty() {
            return Err(Error::Config("apps is required".into()));
        }

        let mut names = HashSet::new();
        let mut owners = HashSet::new();

        for app in &self.apps {
            app.validate()?;

            if !names.insert(&app.name) {
                return Err(Error::Config(format!(
                    "app name must be unique: {}",
                    app.name
                )));
            }

            if !app.git_owner.is_empty() && !owners.insert(&app.git_owner) {
                return Err(Error::Config(format!(
                    "app git_owner must be unique: {}",
                    app.git_owner
                )));
            }
        }

        Ok(())
    }
}

impl App {
    /// Validate a single app entry.
    fn validate(&self) -> crate::Result<()> {
        if self.name.is_empty() {
            return Err(Error::Config("name is required".into()));
        }
        if self.client_id.is_empty() {
            return Err(Error::Config("client_id is required".into()));
        }
        Ok(())
    }
}

/// Read and parse a YAML configuration file.
///
/// Returns `Ok(None)` if the path is empty (matches Go SDK behavior where an
/// empty config path is a no-op). Returns an error if the file cannot be opened
/// or contains invalid YAML.
pub fn read(path: impl AsRef<std::path::Path>) -> crate::Result<Option<Config>> {
    let path = path.as_ref();
    if path.as_os_str().is_empty() {
        return Ok(None);
    }
    let contents = std::fs::read_to_string(path)
        .map_err(|e| Error::Config(format!("open a configuration file: {e}")))?;
    let cfg: Config = serde_yaml::from_str(&contents)
        .map_err(|e| Error::Config(format!("decode a configuration file as YAML: {e}")))?;
    Ok(Some(cfg))
}

/// Return the default configuration file path for ghtkn.
///
/// Platform-specific resolution:
/// - **Windows**: `%APPDATA%\ghtkn\ghtkn.yaml`
/// - **Linux/macOS**: `$XDG_CONFIG_HOME/ghtkn/ghtkn.yaml`, falling back to
///   `$HOME/.config/ghtkn/ghtkn.yaml`
///
/// The `get_env` closure allows injecting environment variable lookups for
/// testing without touching the real environment.
pub fn get_path<F>(get_env: F, os: &str) -> crate::Result<PathBuf>
where
    F: Fn(&str) -> Option<String>,
{
    if os == "windows" {
        if let Some(app_data) = get_env("APPDATA")
            && !app_data.is_empty()
        {
            return Ok(PathBuf::from(app_data).join("ghtkn").join("ghtkn.yaml"));
        }
        return Err(Error::Config("APPDATA is required on Windows".into()));
    }

    // Linux / macOS
    if let Some(xdg) = get_env("XDG_CONFIG_HOME")
        && !xdg.is_empty()
    {
        return Ok(PathBuf::from(xdg).join("ghtkn").join("ghtkn.yaml"));
    }
    if let Some(home) = get_env("HOME")
        && !home.is_empty()
    {
        return Ok(PathBuf::from(home)
            .join(".config")
            .join("ghtkn")
            .join("ghtkn.yaml"));
    }
    Err(Error::Config(
        "XDG_CONFIG_HOME or HOME is required on Linux and macOS".into(),
    ))
}

/// Select an app from the configuration.
///
/// Priority (matches Go SDK exactly):
/// 1. If `owner` is non-empty, search for an app whose `git_owner` matches.
///    If found, return it. If not found, **fall through** to the next check.
/// 2. If `key` is empty, return the first app in the list.
/// 3. If `key` is non-empty, search for an app whose `name` matches.
///    Return it if found, otherwise return `None`.
pub fn select_app<'a>(cfg: &'a Config, key: &str, owner: &str) -> Option<&'a App> {
    if cfg.apps.is_empty() {
        return None;
    }

    if !owner.is_empty()
        && let Some(app) = cfg.apps.iter().find(|a| a.git_owner == owner)
    {
        return Some(app);
    }

    if key.is_empty() {
        return Some(&cfg.apps[0]);
    }

    cfg.apps.iter().find(|a| a.name == key)
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::PathBuf;

    use super::*;

    // ---------------------------------------------------------------
    // Config::validate
    // ---------------------------------------------------------------

    #[test]
    fn validate_valid_single_app() {
        let cfg = Config {
            apps: vec![App {
                name: "test-app".into(),
                client_id: "xxx".into(),
                git_owner: String::new(),
            }],
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_valid_multiple_apps() {
        let cfg = Config {
            apps: vec![
                App {
                    name: "app1".into(),
                    client_id: "xxx".into(),
                    git_owner: String::new(),
                },
                App {
                    name: "app2".into(),
                    client_id: "yyy".into(),
                    git_owner: String::new(),
                },
            ],
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_empty_apps() {
        let cfg = Config { apps: vec![] };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn validate_app_empty_name() {
        let cfg = Config {
            apps: vec![App {
                name: String::new(),
                client_id: "xxx".into(),
                git_owner: String::new(),
            }],
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn validate_app_empty_client_id() {
        let cfg = Config {
            apps: vec![App {
                name: "app".into(),
                client_id: String::new(),
                git_owner: String::new(),
            }],
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn validate_app_both_empty() {
        let cfg = Config {
            apps: vec![App {
                name: String::new(),
                client_id: String::new(),
                git_owner: String::new(),
            }],
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn validate_duplicate_names() {
        let cfg = Config {
            apps: vec![
                App {
                    name: "dup".into(),
                    client_id: "xxx".into(),
                    git_owner: String::new(),
                },
                App {
                    name: "dup".into(),
                    client_id: "yyy".into(),
                    git_owner: String::new(),
                },
            ],
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn validate_duplicate_git_owners() {
        let cfg = Config {
            apps: vec![
                App {
                    name: "app1".into(),
                    client_id: "xxx".into(),
                    git_owner: "same-owner".into(),
                },
                App {
                    name: "app2".into(),
                    client_id: "yyy".into(),
                    git_owner: "same-owner".into(),
                },
            ],
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn validate_unique_git_owners_with_empty() {
        let cfg = Config {
            apps: vec![
                App {
                    name: "app1".into(),
                    client_id: "xxx".into(),
                    git_owner: "owner1".into(),
                },
                App {
                    name: "app2".into(),
                    client_id: "yyy".into(),
                    git_owner: "owner2".into(),
                },
                App {
                    name: "app3".into(),
                    client_id: "zzz".into(),
                    git_owner: String::new(), // empty is allowed
                },
            ],
        };
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_invalid_app_among_valid_ones() {
        let cfg = Config {
            apps: vec![
                App {
                    name: "valid-app".into(),
                    client_id: "xxx".into(),
                    git_owner: String::new(),
                },
                App {
                    name: String::new(), // invalid
                    client_id: "yyy".into(),
                    git_owner: String::new(),
                },
            ],
        };
        assert!(cfg.validate().is_err());
    }

    // ---------------------------------------------------------------
    // read (YAML parsing)
    // ---------------------------------------------------------------

    #[test]
    fn read_valid_config() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("ghtkn.yaml");
        std::fs::write(
            &path,
            "apps:\n  - name: test-app\n    client_id: Iv1.abc123\n",
        )
        .unwrap();

        let cfg = read(&path).unwrap().unwrap();
        assert_eq!(cfg.apps.len(), 1);
        assert_eq!(cfg.apps[0].name, "test-app");
        assert_eq!(cfg.apps[0].client_id, "Iv1.abc123");
        assert!(cfg.apps[0].git_owner.is_empty());
    }

    #[test]
    fn read_multiple_apps() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("ghtkn.yaml");
        std::fs::write(
            &path,
            "apps:\n  - name: app1\n    client_id: xxx\n  - name: app2\n    client_id: yyy\n",
        )
        .unwrap();

        let cfg = read(&path).unwrap().unwrap();
        assert_eq!(cfg.apps.len(), 2);
    }

    #[test]
    fn read_with_git_owner() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("ghtkn.yaml");
        std::fs::write(
            &path,
            "apps:\n  - name: my-app\n    client_id: Iv1.abc123\n    git_owner: myorg\n",
        )
        .unwrap();

        let cfg = read(&path).unwrap().unwrap();
        assert_eq!(cfg.apps[0].git_owner, "myorg");
    }

    #[test]
    fn read_empty_path() {
        let result = read("").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn read_file_not_found() {
        let result = read("/nonexistent/path/ghtkn.yaml");
        assert!(result.is_err());
    }

    #[test]
    fn read_invalid_yaml() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("ghtkn.yaml");
        std::fs::write(&path, "invalid yaml: [").unwrap();

        let result = read(&path);
        assert!(result.is_err());
    }

    // ---------------------------------------------------------------
    // get_path
    // ---------------------------------------------------------------

    fn make_env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
        let map: HashMap<String, String> = pairs
            .iter()
            .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
            .collect();
        move |key: &str| map.get(key).cloned()
    }

    #[test]
    fn get_path_linux_xdg() {
        let env = make_env(&[("XDG_CONFIG_HOME", "/home/user/.config")]);
        let p = get_path(env, "linux").unwrap();
        assert_eq!(p, PathBuf::from("/home/user/.config/ghtkn/ghtkn.yaml"));
    }

    #[test]
    fn get_path_darwin_xdg() {
        let env = make_env(&[("XDG_CONFIG_HOME", "/custom/config/dir")]);
        let p = get_path(env, "darwin").unwrap();
        assert_eq!(p, PathBuf::from("/custom/config/dir/ghtkn/ghtkn.yaml"));
    }

    #[test]
    fn get_path_linux_home_fallback() {
        let env = make_env(&[("HOME", "/home/user")]);
        let p = get_path(env, "linux").unwrap();
        assert_eq!(p, PathBuf::from("/home/user/.config/ghtkn/ghtkn.yaml"));
    }

    #[test]
    fn get_path_linux_xdg_empty_falls_back_to_home() {
        let env = make_env(&[("XDG_CONFIG_HOME", ""), ("HOME", "/home/user")]);
        let p = get_path(env, "linux").unwrap();
        assert_eq!(p, PathBuf::from("/home/user/.config/ghtkn/ghtkn.yaml"));
    }

    #[test]
    fn get_path_linux_no_vars() {
        let env = make_env(&[]);
        let result = get_path(env, "linux");
        assert!(result.is_err());
    }

    #[test]
    fn get_path_linux_both_empty() {
        let env = make_env(&[("XDG_CONFIG_HOME", ""), ("HOME", "")]);
        let result = get_path(env, "linux");
        assert!(result.is_err());
    }

    #[test]
    fn get_path_windows_appdata() {
        let env = make_env(&[("APPDATA", "C:\\Users\\testuser\\AppData\\Roaming")]);
        let p = get_path(env, "windows").unwrap();
        assert_eq!(
            p,
            PathBuf::from("C:\\Users\\testuser\\AppData\\Roaming")
                .join("ghtkn")
                .join("ghtkn.yaml")
        );
    }

    #[test]
    fn get_path_windows_no_appdata() {
        let env = make_env(&[]);
        let result = get_path(env, "windows");
        assert!(result.is_err());
    }

    #[test]
    fn get_path_windows_empty_appdata() {
        let env = make_env(&[("APPDATA", "")]);
        let result = get_path(env, "windows");
        assert!(result.is_err());
    }

    #[test]
    fn get_path_relative_xdg() {
        let env = make_env(&[("XDG_CONFIG_HOME", "relative/config")]);
        let p = get_path(env, "linux").unwrap();
        assert_eq!(p, PathBuf::from("relative/config/ghtkn/ghtkn.yaml"));
    }

    #[test]
    fn get_path_path_with_spaces() {
        let env = make_env(&[("XDG_CONFIG_HOME", "/path with spaces/config")]);
        let p = get_path(env, "darwin").unwrap();
        assert_eq!(
            p,
            PathBuf::from("/path with spaces/config/ghtkn/ghtkn.yaml")
        );
    }

    // ---------------------------------------------------------------
    // select_app
    // ---------------------------------------------------------------

    fn test_config() -> Config {
        Config {
            apps: vec![
                App {
                    name: "app1".into(),
                    client_id: "xxx".into(),
                    git_owner: "owner1".into(),
                },
                App {
                    name: "app2".into(),
                    client_id: "yyy".into(),
                    git_owner: "owner2".into(),
                },
                App {
                    name: "app3".into(),
                    client_id: "zzz".into(),
                    git_owner: String::new(),
                },
            ],
        }
    }

    #[test]
    fn select_app_empty_config() {
        let cfg = Config { apps: vec![] };
        assert!(select_app(&cfg, "any", "").is_none());
    }

    #[test]
    fn select_app_by_owner() {
        let cfg = test_config();
        let app = select_app(&cfg, "", "owner2").unwrap();
        assert_eq!(app.name, "app2");
    }

    #[test]
    fn select_app_by_name() {
        let cfg = test_config();
        let app = select_app(&cfg, "app3", "").unwrap();
        assert_eq!(app.name, "app3");
    }

    #[test]
    fn select_app_owner_priority_over_name() {
        let cfg = test_config();
        // owner matches app1, key matches app2 -- owner wins
        let app = select_app(&cfg, "app2", "owner1").unwrap();
        assert_eq!(app.name, "app1");
    }

    #[test]
    fn select_app_name_not_found() {
        let cfg = test_config();
        assert!(select_app(&cfg, "nonexistent", "").is_none());
    }

    #[test]
    fn select_app_default_first() {
        let cfg = test_config();
        let app = select_app(&cfg, "", "").unwrap();
        assert_eq!(app.name, "app1");
    }

    #[test]
    fn select_app_owner_not_found_falls_through_to_default() {
        // Matches Go SDK: owner miss falls through, key is empty => first app
        let cfg = test_config();
        let app = select_app(&cfg, "", "nonexistent-owner").unwrap();
        assert_eq!(app.name, "app1");
    }

    #[test]
    fn select_app_owner_not_found_falls_through_to_key() {
        // Matches Go SDK: owner miss falls through, key matches => that app
        let cfg = test_config();
        let app = select_app(&cfg, "app3", "nonexistent-owner").unwrap();
        assert_eq!(app.name, "app3");
    }

    #[test]
    fn select_app_owner_not_found_key_not_found() {
        let cfg = test_config();
        assert!(select_app(&cfg, "nonexistent", "nonexistent-owner").is_none());
    }
}