1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

xtask: add completion generation

This commit is contained in:
RGBCube 2025-06-14 23:19:54 +03:00
parent a341d08c45
commit 0e07241280
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M
6 changed files with 147 additions and 17 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"

29
Cargo.lock generated
View file

@ -117,6 +117,25 @@ dependencies = [
"strsim",
]
[[package]]
name = "clap_complete"
version = "4.5.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aad5b1b4de04fead402672b48897030eec1f3bfe1550776322f59f6d6e6a5677"
dependencies = [
"clap",
]
[[package]]
name = "clap_complete_nushell"
version = "4.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb8335b398d197fb3176efe9400c6c053a41733c26794316c73423d212b2f3d"
dependencies = [
"clap",
"clap_complete",
]
[[package]]
name = "clap_derive"
version = "4.5.32"
@ -625,6 +644,16 @@ dependencies = [
"memchr",
]
[[package]]
name = "xtask"
version = "0.4.0"
dependencies = [
"clap",
"clap_complete",
"clap_complete_nushell",
"watt",
]
[[package]]
name = "yansi"
version = "1.0.1"

View file

@ -1,5 +1,5 @@
[workspace]
members = [ "watt" ]
members = [ "watt", "xtask" ]
resolver = "3"
[workspace.package]
@ -15,6 +15,8 @@ version = "0.4.0"
anyhow = "1.0"
clap = { version = "4.0", features = [ "derive", "env" ] }
clap-verbosity-flag = "3.0.2"
clap_complete = "4.5.54"
clap_complete_nushell = "4.5.7"
ctrlc = "3.4"
derive_more = { version = "2.0.1", features = [ "full" ] }
env_logger = "0.11"

View file

@ -27,9 +27,8 @@ pub enum Command {
#[command(flatten)]
verbosity: clap_verbosity_flag::Verbosity,
/// The daemon config path.
#[arg(long, env = "WATT_CONFIG")]
config: Option<PathBuf>,
#[clap(flatten)]
command: WattCommand,
},
/// CPU metadata and modification utility.
@ -51,6 +50,13 @@ pub enum Command {
},
}
#[derive(clap::Parser, Debug)]
pub struct WattCommand {
/// The daemon config path.
#[arg(long, env = "WATT_CONFIG")]
config: Option<PathBuf>,
}
#[derive(clap::Parser, Debug)]
pub enum CpuCommand {
/// Modify CPU attributes.
@ -79,7 +85,10 @@ pub fn main() -> anyhow::Result<()> {
.init();
match cli.command {
Command::Watt { config, .. } => {
Command::Watt {
command: WattCommand { config },
..
} => {
let config = config::DaemonConfig::load_from(config.as_deref())
.context("failed to load daemon config")?;

19
xtask/Cargo.toml Normal file
View file

@ -0,0 +1,19 @@
[package]
name = "xtask"
description.workspace = true
version.workspace = true
edition.workspace = true
authors.workspace = true
rust-version.workspace = true
publish = false
[[bin]]
name = "xtask"
path = "main.rs"
[dependencies]
watt.path = "../watt"
clap.workspace = true
clap_complete.workspace = true
clap_complete_nushell.workspace = true

69
xtask/main.rs Normal file
View file

@ -0,0 +1,69 @@
use std::io;
use clap::{
CommandFactory,
Parser as _,
};
#[derive(clap::Parser)]
struct Cli {
#[clap(subcommand)]
command: Command,
}
#[derive(clap::Subcommand)]
enum Command {
/// Generate completions for the specified shell.
GenerateCompletions {
#[arg(long)]
shell: Shell,
#[arg(long)]
binary: Binary,
},
}
#[expect(clippy::enum_variant_names)]
#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
enum Shell {
Bash,
Elvish,
Fish,
PowerShell,
Zsh,
Nushell,
}
#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
enum Binary {
Watt,
Cpu,
Power,
}
fn main() {
let cli = Cli::parse();
match cli.command {
Command::GenerateCompletions { shell, binary } => {
let mut command = match binary {
Binary::Watt => watt::WattCommand::command(),
Binary::Cpu => watt::CpuCommand::command(),
Binary::Power => watt::PowerCommand::command(),
};
command.set_bin_name(format!("{binary:?}").to_lowercase());
command.build();
let shell: &dyn clap_complete::Generator = match shell {
Shell::Bash => &clap_complete::Shell::Bash,
Shell::Elvish => &clap_complete::Shell::Elvish,
Shell::Fish => &clap_complete::Shell::Fish,
Shell::PowerShell => &clap_complete::Shell::PowerShell,
Shell::Zsh => &clap_complete::Shell::Zsh,
Shell::Nushell => &clap_complete_nushell::Nushell,
};
shell.generate(&command, &mut io::stdout());
},
}
}