kata-ctl: clippy: Resolve warnings and reformat

Resolved a couple of clippy warnings and applied standard `rustfmt`.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2022-10-10 09:46:36 +01:00
parent 133690434c
commit c23584994a
5 changed files with 45 additions and 53 deletions

View File

@ -7,8 +7,8 @@
pub use arch_specific::*; pub use arch_specific::*;
mod arch_specific { mod arch_specific {
use anyhow::{anyhow, Result};
use crate::check; use crate::check;
use anyhow::{anyhow, Result};
const PROC_CPUINFO: &str = "/proc/cpuinfo"; const PROC_CPUINFO: &str = "/proc/cpuinfo";
const CPUINFO_DELIMITER: &str = "\nprocessor"; const CPUINFO_DELIMITER: &str = "\nprocessor";
@ -30,7 +30,10 @@ mod arch_specific {
// TODO: Add more information to output (see kata-check in go tool); adjust formatting // TODO: Add more information to output (see kata-check in go tool); adjust formatting
let missing_cpu_attributes = check::check_cpu_attribs(&cpu_info, CPU_ATTRIBS_INTEL)?; let missing_cpu_attributes = check::check_cpu_attribs(&cpu_info, CPU_ATTRIBS_INTEL)?;
if missing_cpu_attributes.len() > 0 { if missing_cpu_attributes.len() > 0 {
eprintln!("WARNING: Missing CPU attributes {:?}", missing_cpu_attributes); eprintln!(
"WARNING: Missing CPU attributes {:?}",
missing_cpu_attributes
);
} }
let missing_cpu_flags = check::check_cpu_flags(&cpu_flags, CPU_FLAGS_INTEL)?; let missing_cpu_flags = check::check_cpu_flags(&cpu_flags, CPU_FLAGS_INTEL)?;
if missing_cpu_flags.len() > 0 { if missing_cpu_flags.len() > 0 {
@ -40,7 +43,6 @@ mod arch_specific {
Ok(()) Ok(())
} }
pub fn check() -> Result<()> { pub fn check() -> Result<()> {
println!("INFO: check: x86_64"); println!("INFO: check: x86_64");

View File

@ -3,16 +3,12 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use clap::{ use clap::{Args, Parser, Subcommand};
Args,
Parser,
Subcommand
};
use thiserror::Error; use thiserror::Error;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(name = "kata-ctl", author, about="Kata Containers control tool")] #[clap(name = "kata-ctl", author, about = "Kata Containers control tool")]
pub struct KataCtlCli { pub struct KataCtlCli {
#[clap(subcommand)] #[clap(subcommand)]
pub command: Commands, pub command: Commands,
@ -45,7 +41,7 @@ pub enum Commands {
Version, Version,
} }
#[derive(Debug,Args,Error)] #[derive(Debug, Args, Error)]
#[error("Argument is not valid")] #[error("Argument is not valid")]
pub struct CheckArgument { pub struct CheckArgument {
#[clap(subcommand)] #[clap(subcommand)]
@ -64,7 +60,7 @@ pub enum CheckSubCommand {
CheckVersionOnly, CheckVersionOnly,
} }
#[derive(Debug,Args)] #[derive(Debug, Args)]
pub struct MetricsCommand { pub struct MetricsCommand {
#[clap(subcommand)] #[clap(subcommand)]
pub metrics_cmd: MetricsSubCommand, pub metrics_cmd: MetricsSubCommand,
@ -73,7 +69,7 @@ pub struct MetricsCommand {
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum MetricsSubCommand { pub enum MetricsSubCommand {
/// Arguments for metrics /// Arguments for metrics
MetricsArgs, MetricsArgs,
} }
// #[derive(Parser, Debug)] // #[derive(Parser, Debug)]

View File

@ -5,14 +5,14 @@
// Contains checks that are not architecture-specific // Contains checks that are not architecture-specific
use anyhow::{anyhow, Result};
use std::fs; use reqwest::header::{CONTENT_TYPE, USER_AGENT};
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
use anyhow::{anyhow, Result}; use std::fs;
use reqwest::header::{CONTENT_TYPE,USER_AGENT};
const KATA_GITHUB_URL: &str = "https://api.github.com/repos/kata-containers/kata-containers/releases/latest"; const KATA_GITHUB_URL: &str =
"https://api.github.com/repos/kata-containers/kata-containers/releases/latest";
fn get_cpu_info(cpu_info_file: &str) -> Result<String> { fn get_cpu_info(cpu_info_file: &str) -> Result<String> {
let contents = fs::read_to_string(cpu_info_file)?; let contents = fs::read_to_string(cpu_info_file)?;
@ -64,9 +64,9 @@ pub fn get_cpu_flags(cpu_info: &str, cpu_flags_tag: &str) -> Result<String> {
// get_missing_strings searches for required (strings) in data and returns // get_missing_strings searches for required (strings) in data and returns
// a vector containing the missing strings // a vector containing the missing strings
fn get_missing_strings(data: &str, required: &'static [&'static str]) -> Result<Vec<String>> { fn get_missing_strings(data: &str, required: &'static [&'static str]) -> Result<Vec<String>> {
let data_vec: Vec <&str> = data.split_whitespace().collect(); let data_vec: Vec<&str> = data.split_whitespace().collect();
let mut missing: Vec <String> = Vec::new(); let mut missing: Vec<String> = Vec::new();
for item in required { for item in required {
if !data_vec.contains(&item) { if !data_vec.contains(&item) {
@ -77,13 +77,19 @@ fn get_missing_strings(data: &str, required: &'static [&'static str]) -> Result<
Ok(missing) Ok(missing)
} }
pub fn check_cpu_flags(retrieved_flags: &str, required_flags: &'static [&'static str]) -> Result<Vec<String>> { pub fn check_cpu_flags(
retrieved_flags: &str,
required_flags: &'static [&'static str],
) -> Result<Vec<String>> {
let missing_flags = get_missing_strings(retrieved_flags, required_flags)?; let missing_flags = get_missing_strings(retrieved_flags, required_flags)?;
Ok(missing_flags) Ok(missing_flags)
} }
pub fn check_cpu_attribs(cpu_info: &str, required_attribs: &'static [&'static str]) -> Result<Vec<String>> { pub fn check_cpu_attribs(
cpu_info: &str,
required_attribs: &'static [&'static str],
) -> Result<Vec<String>> {
let mut cpu_info_processed = cpu_info.replace("\t", ""); let mut cpu_info_processed = cpu_info.replace("\t", "");
cpu_info_processed = cpu_info_processed.replace("\n", " "); cpu_info_processed = cpu_info_processed.replace("\n", " ");
@ -146,7 +152,10 @@ mod tests {
let actual = get_cpu_info("").err().unwrap().to_string(); let actual = get_cpu_info("").err().unwrap().to_string();
assert_eq!(expected, actual); assert_eq!(expected, actual);
let actual = get_single_cpu_info("", "\nprocessor").err().unwrap().to_string(); let actual = get_single_cpu_info("", "\nprocessor")
.err()
.unwrap()
.to_string();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
@ -162,7 +171,7 @@ mod tests {
const TEST_URL: &str = "http:"; const TEST_URL: &str = "http:";
let expected = "builder error: empty host"; let expected = "builder error: empty host";
let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string(); let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
#[test] #[test]
@ -170,7 +179,7 @@ mod tests {
const TEST_URL: &str = "_localhost_"; const TEST_URL: &str = "_localhost_";
let expected = "builder error: relative URL without a base"; let expected = "builder error: relative URL without a base";
let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string(); let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
#[test] #[test]
@ -178,16 +187,16 @@ mod tests {
const TEST_URL: &str = "http://localhost :80"; const TEST_URL: &str = "http://localhost :80";
let expected = "builder error: invalid domain character"; let expected = "builder error: invalid domain character";
let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string(); let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
#[test] #[test]
fn check_latest_version() { fn check_latest_version() {
let version = get_kata_version_by_url(KATA_GITHUB_URL).unwrap(); let version = get_kata_version_by_url(KATA_GITHUB_URL).unwrap();
let v = Version::parse(&version).unwrap(); let v = Version::parse(&version).unwrap();
assert!(!v.major.to_string().is_empty()); assert!(!v.major.to_string().is_empty());
assert!(!v.minor.to_string().is_empty()); assert!(!v.minor.to_string().is_empty());
assert!(!v.patch.to_string().is_empty()); assert!(!v.patch.to_string().is_empty());
} }
} }

View File

@ -3,33 +3,23 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
mod args;
mod arch; mod arch;
mod args;
mod check; mod check;
mod ops; mod ops;
use clap::Parser;
use anyhow::Result; use anyhow::Result;
use clap::Parser;
use std::process::exit; use std::process::exit;
use args::{ use args::{Commands, KataCtlCli};
KataCtlCli,
Commands
};
use ops::check_ops::{ use ops::check_ops::{
handle_check, handle_check, handle_check_volume, handle_env, handle_exec, handle_factory, handle_iptables,
handle_check_volume, handle_metrics, handle_version,
handle_env,
handle_exec,
handle_factory,
handle_iptables,
handle_metrics,
handle_version
}; };
fn real_main() -> Result<()> { fn real_main() -> Result<()> {
let args = KataCtlCli::parse(); let args = KataCtlCli::parse();
match args.command { match args.command {

View File

@ -7,12 +7,7 @@ use crate::arch;
use crate::check; use crate::check;
use crate::ops::version; use crate::ops::version;
use crate::args::{ use crate::args::{CheckArgument, CheckSubCommand, IptablesCommand, MetricsCommand};
CheckArgument,
CheckSubCommand,
IptablesCommand,
MetricsCommand
};
use anyhow::Result; use anyhow::Result;
@ -60,11 +55,11 @@ pub fn handle_factory() -> Result<()> {
Ok(()) Ok(())
} }
pub fn handle_iptables(args: IptablesCommand) -> Result<()> { pub fn handle_iptables(_args: IptablesCommand) -> Result<()> {
Ok(()) Ok(())
} }
pub fn handle_metrics(args: MetricsCommand) -> Result<()> { pub fn handle_metrics(_args: MetricsCommand) -> Result<()> {
Ok(()) Ok(())
} }