// nixos-config - My current NixOS configuration // // Copyright (C) 2025 Benedikt Peetz // 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 . 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, /// 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 { 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 }