summaryrefslogtreecommitdiffhomepage
path: root/tools/unitctl/unit-client-rs/src/runtime_flags.rs
diff options
context:
space:
mode:
authorAva Hahn <a.hahn@f5.com>2024-04-22 13:26:34 +0100
committeravahahn <110854134+avahahn@users.noreply.github.com>2024-04-30 09:14:55 -0700
commitdb3cf3e42d93112278f5e86cca2c886627ef48b2 (patch)
tree3c546922404965b3fc7773d319388b9241f9896b /tools/unitctl/unit-client-rs/src/runtime_flags.rs
parentb26c119f4e535f617c9ffb10076f15c4ee54414d (diff)
downloadunit-db3cf3e42d93112278f5e86cca2c886627ef48b2.tar.gz
unit-db3cf3e42d93112278f5e86cca2c886627ef48b2.tar.bz2
tools: Add unitctl CLI
* Pull in entire unit-rust-sdk project * not included: CLA, COC, License * not included: duplicate openapi spec * not included: CI workflows * not included: changelog tooling * not included: commitsar tooling * not included: OpenAPI Web UI feature * update links in unitctl manpage * remove IDE configuration from .gitignore * rename Containerfile.debian to Dockerfile * simplify call to uname * keep Readmes and Makefiles to 80 character lines * outline specifically how to build unitctl for any desired target, and where to then find the binary for use * remove a section on the vision of the CLI which was superfluous given the state of completeness of the code and its use in unit * remove out of date feature proposals from readme * makefile: do not run when Rustup is not present * bump mio version to latest * generate openapi client library on demand * generate-openapi only runs when not present * generate-openapi now a dependency of binary build targets * deleted autogenerated code * reverted readme and Cargo document to autogenerated state * add additional build requirement to Readme Co-developed-by: Elijah Zupancic <e.zupancic@f5.com> Signed-off-by: Elijah Zupancic <e.zupancic@f5.com> Signed-off-by: Ava Hahn <a.hahn@f5.com> Reviewed-by: Andrew Clayton <a.clayton@nginx.com> # non rust stuff [ tools/cli => tools/unitctl and subject tweak - Andrew ] Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'tools/unitctl/unit-client-rs/src/runtime_flags.rs')
-rw-r--r--tools/unitctl/unit-client-rs/src/runtime_flags.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/tools/unitctl/unit-client-rs/src/runtime_flags.rs b/tools/unitctl/unit-client-rs/src/runtime_flags.rs
new file mode 100644
index 00000000..7b31274d
--- /dev/null
+++ b/tools/unitctl/unit-client-rs/src/runtime_flags.rs
@@ -0,0 +1,90 @@
+use std::borrow::Cow;
+use std::fmt;
+use std::fmt::Display;
+use std::path::{Path, PathBuf};
+
+#[derive(Debug, Clone)]
+pub struct RuntimeFlags {
+ pub flags: Cow<'static, str>,
+}
+
+impl Display for RuntimeFlags {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.flags)
+ }
+}
+
+impl RuntimeFlags {
+ pub fn new<S>(flags: S) -> RuntimeFlags
+ where
+ S: Into<String>,
+ {
+ RuntimeFlags {
+ flags: Cow::from(flags.into()),
+ }
+ }
+
+ pub fn has_flag(&self, flag_name: &str) -> bool {
+ self.flags.contains(format!("--{}", flag_name).as_str())
+ }
+
+ pub fn get_flag_value(&self, flag_name: &str) -> Option<String> {
+ let flag_parts = self.flags.split_ascii_whitespace().collect::<Vec<&str>>();
+ for (i, flag) in flag_parts.iter().enumerate() {
+ if let Some(name) = flag.strip_prefix("--") {
+ /* If there is no flag value after the current one, there is by definition no
+ * flag value for the current flag. */
+ let index_lt_len = flag_parts.len() > i + 1;
+ if index_lt_len {
+ let next_value_isnt_flag = !flag_parts[i + 1].starts_with("--");
+ if name.eq(flag_name) && next_value_isnt_flag {
+ return Some(flag_parts[i + 1].to_string());
+ }
+ }
+ }
+ }
+ None
+ }
+
+ pub fn control_api_socket_address(&self) -> Option<String> {
+ self.get_flag_value("control")
+ }
+
+ pub fn pid_path(&self) -> Option<Box<Path>> {
+ self.get_flag_value("pid")
+ .map(PathBuf::from)
+ .map(PathBuf::into_boxed_path)
+ }
+
+ pub fn log_path(&self) -> Option<Box<Path>> {
+ self.get_flag_value("log")
+ .map(PathBuf::from)
+ .map(PathBuf::into_boxed_path)
+ }
+
+ pub fn modules_directory(&self) -> Option<Box<Path>> {
+ self.get_flag_value("modules")
+ .map(PathBuf::from)
+ .map(PathBuf::into_boxed_path)
+ }
+
+ pub fn state_directory(&self) -> Option<Box<Path>> {
+ self.get_flag_value("state")
+ .map(PathBuf::from)
+ .map(PathBuf::into_boxed_path)
+ }
+
+ pub fn tmp_directory(&self) -> Option<Box<Path>> {
+ self.get_flag_value("tmp")
+ .map(PathBuf::from)
+ .map(PathBuf::into_boxed_path)
+ }
+
+ pub fn user(&self) -> Option<String> {
+ self.get_flag_value("user").map(String::from)
+ }
+
+ pub fn group(&self) -> Option<String> {
+ self.get_flag_value("group").map(String::from)
+ }
+}