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
|
// nixos-config - My current NixOS configuration
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of my nixos-config.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use core::fmt;
use std::{ffi::OsStr, process::Command};
use clap::{Parser, ValueEnum};
use clap_complete::{CompletionCandidate, engine::ArgValueCompleter};
/// This is a Nix flake update manager.
#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct CliArgs {
/// The branch to update from.
#[arg(short, long, add = ArgValueCompleter::new(get_branches))]
pub branch: Option<String>,
/// The mode to update with.
#[arg(short, long, default_value_t = Mode::Switch)]
pub mode: Mode,
/// Don't actually make changes.
#[arg(short, long)]
pub dry_run: bool,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Mode {
Switch,
Boot,
Test,
Build,
DryBuild,
DryActivate,
Edit,
Repl,
BuildVm,
BuildVmWithBootloader,
}
impl fmt::Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", format!("{self:?}").to_lowercase())
}
}
fn get_branches(current: &OsStr) -> Vec<CompletionCandidate> {
let mut output = vec![];
let Some(current) = current.to_str() else {
return output;
};
let Ok(git_output) = Command::new("git")
.args(["branch", "--list", "--format", "'%(refname:short)'"])
.current_dir("/etc/nixos")
.output()
else {
return output;
};
if !git_output.status.success() {
return output;
}
let Ok(possible) = String::from_utf8(git_output.stdout) else {
return output;
};
for possible in possible.trim().split('\n') {
if possible.starts_with(current) {
output.push(CompletionCandidate::new(possible));
}
}
output
}
|