|
| 1 | +use crate::cli::Command as DynCommand; |
| 2 | +use crate::sys; |
| 3 | + |
| 4 | +use anyhow::{Context, Result}; |
| 5 | + |
| 6 | + |
| 7 | +pub struct Command; |
| 8 | + |
| 9 | +impl DynCommand for Command { |
| 10 | + fn name(&self) -> &'static str { |
| 11 | + "profile" |
| 12 | + } |
| 13 | + |
| 14 | + fn build(&self) -> clap::App<'static, 'static> { |
| 15 | + use clap::{AppSettings, Arg, SubCommand}; |
| 16 | + |
| 17 | + SubCommand::with_name(self.name()) |
| 18 | + .about("Control or query the current platform profile") |
| 19 | + .setting(AppSettings::SubcommandRequiredElseHelp) |
| 20 | + .subcommand(SubCommand::with_name("set") |
| 21 | + .about("Set the current platform profile") |
| 22 | + .arg(Arg::with_name("profile") |
| 23 | + .required(true) |
| 24 | + .index(1))) |
| 25 | + .subcommand(SubCommand::with_name("get") |
| 26 | + .about("Get the current platform profile")) |
| 27 | + .subcommand(SubCommand::with_name("list") |
| 28 | + .about("List all available platform profiles")) |
| 29 | + } |
| 30 | + |
| 31 | + fn execute(&self, m: &clap::ArgMatches) -> Result<()> { |
| 32 | + match m.subcommand() { |
| 33 | + ("set", Some(m)) => self.profile_set(m), |
| 34 | + ("get", Some(m)) => self.profile_get(m), |
| 35 | + ("list", Some(m)) => self.profile_list(m), |
| 36 | + _ => unreachable!(), |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl Command { |
| 42 | + fn profile_set(&self, m: &clap::ArgMatches) -> Result<()> { |
| 43 | + let profile = m.value_of("profile").unwrap(); |
| 44 | + |
| 45 | + let dev = sys::profile::Device::open() |
| 46 | + .context("Failed to open platform profile device")?; |
| 47 | + |
| 48 | + let supported = dev.get_supported() |
| 49 | + .context("Failed to get supported platform profiles")?; |
| 50 | + |
| 51 | + if !supported.iter().any(|p| p == profile) { |
| 52 | + anyhow::bail!("Platform profile '{}' is not supported", profile); |
| 53 | + } |
| 54 | + |
| 55 | + let current_profile = dev.get() |
| 56 | + .context("Failed to get current platform profile")?; |
| 57 | + |
| 58 | + if profile != current_profile { |
| 59 | + dev.set(profile) |
| 60 | + .context("Failed to set platform profile")?; |
| 61 | + |
| 62 | + if !m.is_present("quiet") { |
| 63 | + println!("Platform profile set to '{}'", profile); |
| 64 | + } |
| 65 | + |
| 66 | + } else if !m.is_present("quiet") { |
| 67 | + println!("Platform profile already set to '{}', not changing", profile); |
| 68 | + } |
| 69 | + |
| 70 | + Ok(()) |
| 71 | + } |
| 72 | + |
| 73 | + fn profile_get(&self, _m: &clap::ArgMatches) -> Result<()> { |
| 74 | + let dev = sys::profile::Device::open() |
| 75 | + .context("Failed to open platform profile device")?; |
| 76 | + |
| 77 | + let profile = dev.get() |
| 78 | + .context("Failed to get current platform profile")?; |
| 79 | + |
| 80 | + println!("{}", profile); |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | + |
| 84 | + fn profile_list(&self, m: &clap::ArgMatches) -> Result<()> { |
| 85 | + let dev = sys::profile::Device::open() |
| 86 | + .context("Failed to open platform profile device")?; |
| 87 | + |
| 88 | + let supported = dev.get_supported() |
| 89 | + .context("Failed to get supported platform profiles")?; |
| 90 | + |
| 91 | + if !m.is_present("quiet") { |
| 92 | + for profile in supported { |
| 93 | + println!("{}", profile); |
| 94 | + } |
| 95 | + |
| 96 | + } else { |
| 97 | + let text = serde_json::to_string(&supported) |
| 98 | + .context("Failed to serialize data")?; |
| 99 | + |
| 100 | + println!("{}", text); |
| 101 | + } |
| 102 | + |
| 103 | + Ok(()) |
| 104 | + } |
| 105 | +} |
0 commit comments