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::*;
mod arch_specific {
use anyhow::{anyhow, Result};
use crate::check;
use anyhow::{anyhow, Result};
const PROC_CPUINFO: &str = "/proc/cpuinfo";
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
let missing_cpu_attributes = check::check_cpu_attribs(&cpu_info, CPU_ATTRIBS_INTEL)?;
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)?;
if missing_cpu_flags.len() > 0 {
@ -40,7 +43,6 @@ mod arch_specific {
Ok(())
}
pub fn check() -> Result<()> {
println!("INFO: check: x86_64");

View File

@ -3,16 +3,12 @@
// SPDX-License-Identifier: Apache-2.0
//
use clap::{
Args,
Parser,
Subcommand
};
use clap::{Args, Parser, Subcommand};
use thiserror::Error;
#[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 {
#[clap(subcommand)]
pub command: Commands,
@ -45,7 +41,7 @@ pub enum Commands {
Version,
}
#[derive(Debug,Args,Error)]
#[derive(Debug, Args, Error)]
#[error("Argument is not valid")]
pub struct CheckArgument {
#[clap(subcommand)]
@ -64,7 +60,7 @@ pub enum CheckSubCommand {
CheckVersionOnly,
}
#[derive(Debug,Args)]
#[derive(Debug, Args)]
pub struct MetricsCommand {
#[clap(subcommand)]
pub metrics_cmd: MetricsSubCommand,
@ -73,7 +69,7 @@ pub struct MetricsCommand {
#[derive(Debug, Subcommand)]
pub enum MetricsSubCommand {
/// Arguments for metrics
MetricsArgs,
MetricsArgs,
}
// #[derive(Parser, Debug)]

View File

@ -5,14 +5,14 @@
// Contains checks that are not architecture-specific
use std::fs;
use anyhow::{anyhow, Result};
use reqwest::header::{CONTENT_TYPE, USER_AGENT};
use serde_json::Value;
use std::collections::HashMap;
use anyhow::{anyhow, Result};
use reqwest::header::{CONTENT_TYPE,USER_AGENT};
use std::fs;
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> {
let contents = fs::read_to_string(cpu_info_file)?;
@ -22,7 +22,7 @@ fn get_cpu_info(cpu_info_file: &str) -> Result<String> {
// get_single_cpu_info returns the contents of the first cpu from
// the specified cpuinfo file by parsing based on a specified delimiter
pub fn get_single_cpu_info(cpu_info_file: &str, substring: &str) -> Result<String> {
let contents = get_cpu_info(cpu_info_file)?;
let contents = get_cpu_info(cpu_info_file)?;
if contents.is_empty() {
return Err(anyhow!("cpu_info string is empty"))?;
@ -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
// a vector containing the missing strings
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 {
if !data_vec.contains(&item) {
@ -77,13 +77,19 @@ fn get_missing_strings(data: &str, required: &'static [&'static str]) -> Result<
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)?;
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", "");
cpu_info_processed = cpu_info_processed.replace("\n", " ");
@ -146,7 +152,10 @@ mod tests {
let actual = get_cpu_info("").err().unwrap().to_string();
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);
}
@ -162,7 +171,7 @@ mod tests {
const TEST_URL: &str = "http:";
let expected = "builder error: empty host";
let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string();
assert_eq!(expected, actual);
assert_eq!(expected, actual);
}
#[test]
@ -170,7 +179,7 @@ mod tests {
const TEST_URL: &str = "_localhost_";
let expected = "builder error: relative URL without a base";
let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string();
assert_eq!(expected, actual);
assert_eq!(expected, actual);
}
#[test]
@ -178,16 +187,16 @@ mod tests {
const TEST_URL: &str = "http://localhost :80";
let expected = "builder error: invalid domain character";
let actual = get_kata_version_by_url(TEST_URL).err().unwrap().to_string();
assert_eq!(expected, actual);
assert_eq!(expected, actual);
}
#[test]
fn check_latest_version() {
let version = get_kata_version_by_url(KATA_GITHUB_URL).unwrap();
let v = Version::parse(&version).unwrap();
assert!(!v.major.to_string().is_empty());
assert!(!v.minor.to_string().is_empty());
assert!(!v.patch.to_string().is_empty());
let v = Version::parse(&version).unwrap();
assert!(!v.major.to_string().is_empty());
assert!(!v.minor.to_string().is_empty());
assert!(!v.patch.to_string().is_empty());
}
}

View File

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

View File

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