mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-30 17:22:33 +00:00
kata-env: Pass cmd option for file path
Add ability to write the environment information to a file or stdout if file path is absent. Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
This commit is contained in:
parent
b1920198be
commit
b908a780a0
@ -74,6 +74,9 @@ pub struct EnvArgument {
|
|||||||
/// Format output as JSON
|
/// Format output as JSON
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub json: bool,
|
pub json: bool,
|
||||||
|
/// File to write env output to
|
||||||
|
#[arg(short = 'f', long = "file")]
|
||||||
|
pub file: Option<String>,
|
||||||
}
|
}
|
||||||
#[derive(Debug, Args)]
|
#[derive(Debug, Args)]
|
||||||
pub struct MetricsCommand {
|
pub struct MetricsCommand {
|
||||||
|
@ -14,6 +14,8 @@ use kata_types::config::TomlConfig;
|
|||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, Write};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use sys_info;
|
use sys_info;
|
||||||
|
|
||||||
@ -439,16 +441,24 @@ pub fn get_env_info(toml_config: &TomlConfig) -> Result<EnvInfo> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_env(env_args: EnvArgument) -> Result<()> {
|
pub fn handle_env(env_args: EnvArgument) -> Result<()> {
|
||||||
|
let mut file: Box<dyn Write> = if let Some(path) = env_args.file {
|
||||||
|
Box::new(
|
||||||
|
File::create(path.as_str()).with_context(|| format!("Error creating file {}", path))?,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Box::new(io::stdout())
|
||||||
|
};
|
||||||
|
|
||||||
let (toml_config, _) = TomlConfig::load_raw_from_file("").context("load toml config")?;
|
let (toml_config, _) = TomlConfig::load_raw_from_file("").context("load toml config")?;
|
||||||
|
|
||||||
let env_info = get_env_info(&toml_config)?;
|
let env_info = get_env_info(&toml_config)?;
|
||||||
|
|
||||||
if env_args.json {
|
if env_args.json {
|
||||||
let serialized_json = serde_json::to_string_pretty(&env_info)?;
|
let serialized_json = serde_json::to_string_pretty(&env_info)?;
|
||||||
println!("{}", serialized_json);
|
write!(file, "{}", serialized_json)?;
|
||||||
} else {
|
} else {
|
||||||
let toml = toml::to_string(&env_info)?;
|
let toml = toml::to_string(&env_info)?;
|
||||||
println!("{}", toml);
|
write!(file, "{}", toml)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user