runc: move runc commandline constants into options.rs

Move runc commandline related constants into options.rs, so options.rs
hosts all commandline parsing related code. And util.rs only hosts
utilities.

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
Liu Jiang 2022-02-14 16:11:30 +08:00
parent cfdbb4dbcd
commit 4b3efbc875
3 changed files with 35 additions and 31 deletions

View File

@ -57,7 +57,6 @@ use crate::error::Error;
#[cfg(feature = "async")]
use crate::monitor::{DefaultMonitor, Exit, ProcessMonitor};
use crate::options::*;
use crate::utils::{JSON, TEXT};
type Result<T> = std::result::Result<T, crate::error::Error>;

View File

@ -39,9 +39,37 @@ use std::time::Duration;
use crate::error::Error;
use crate::io::Io;
use crate::utils::{self, ALL, CONSOLE_SOCKET, DETACH, FORCE, NO_NEW_KEYRING, NO_PIVOT, PID_FILE};
use crate::utils;
use crate::{LogFormat, Runc};
// constants for log format
pub const JSON: &str = "json";
pub const TEXT: &str = "text";
// constants for runc global flags
const DEBUG: &str = "--debug";
const LOG: &str = "--log";
const LOG_FORMAT: &str = "--log-format";
const ROOT: &str = "--root";
const ROOTLESS: &str = "--rootless";
const SYSTEMD_CGROUP: &str = "--systemd-cgroup";
// constants for runc-create/runc-exec flags
const CONSOLE_SOCKET: &str = "--console-socket";
const DETACH: &str = "--detach";
const NO_NEW_KEYRING: &str = "--no-new-keyring";
const NO_PIVOT: &str = "--no-pivot";
const PID_FILE: &str = "--pid-file";
// constants for runc-kill flags
const ALL: &str = "--all";
// constants for runc-delete flags
const FORCE: &str = "--force";
// constant for command
pub const DEFAULT_COMMAND: &str = "runc";
pub trait Args {
type Output;
@ -185,33 +213,33 @@ impl Args for GlobalOpts {
// --root path : Set the root directory to store containers' state.
if let Some(root) = &self.root {
args.push("--root".into());
args.push(ROOT.into());
args.push(utils::abs_string(root)?);
}
// --debug : Enable debug logging.
if self.debug {
args.push("--debug".into());
args.push(DEBUG.into());
}
// --log path : Set the log destination to path. The default is to log to stderr.
if let Some(log_path) = &self.log {
args.push("--log".into());
args.push(LOG.into());
args.push(utils::abs_string(log_path)?);
}
// --log-format text|json : Set the log format (default is text).
args.push("--log-format".into());
args.push(LOG_FORMAT.into());
args.push(self.log_format.to_string());
// --systemd-cgroup : Enable systemd cgroup support.
if self.systemd_cgroup {
args.push("--systemd-cgroup".into());
args.push(SYSTEMD_CGROUP.into());
}
// --rootless true|false|auto : Enable or disable rootless mode.
if let Some(mode) = self.rootless {
let arg = format!("--rootless={}", mode);
let arg = format!("{}={}", ROOTLESS, mode);
args.push(arg);
}

View File

@ -25,29 +25,6 @@ use uuid::Uuid;
use crate::error::Error;
// constants for flags
pub const ALL: &str = "--all";
pub const CONSOLE_SOCKET: &str = "--console-socket";
// pub const CRIU: &str = "--criu";
pub const DEBUG: &str = "--debug";
pub const DETACH: &str = "--detach";
pub const FORCE: &str = "--force";
pub const LOG: &str = "--log";
pub const LOG_FORMAT: &str = "--log-format";
pub const NO_NEW_KEYRING: &str = "--no-new-keyring";
pub const NO_PIVOT: &str = "--no-pivot";
pub const PID_FILE: &str = "--pid-file";
pub const ROOT: &str = "--root";
pub const ROOTLESS: &str = "--rootless";
pub const SYSTEMD_CGROUP: &str = "--systemd-cgroup";
// constants for log format
pub const JSON: &str = "json";
pub const TEXT: &str = "text";
// constant for command
pub const DEFAULT_COMMAND: &str = "runc";
// helper to resolve path (such as path for runc binary, pid files, etc. )
pub fn abs_path_buf<P>(path: P) -> Result<PathBuf, Error>
where