diff options
Diffstat (limited to 'pkgs/by-name/fu/fupdate-sys2/src/cli.rs')
| -rw-r--r-- | pkgs/by-name/fu/fupdate-sys2/src/cli.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/pkgs/by-name/fu/fupdate-sys2/src/cli.rs b/pkgs/by-name/fu/fupdate-sys2/src/cli.rs new file mode 100644 index 00000000..cf8a796e --- /dev/null +++ b/pkgs/by-name/fu/fupdate-sys2/src/cli.rs @@ -0,0 +1,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 +} |
