Skip to content

Commit d84d584

Browse files
authored
add: support YAML configuration format (#39)
* add: support YAML configuration format * chore: var name --------- Co-authored-by: queil <queil@users.noreply.github.com>
1 parent 5569f35 commit d84d584

File tree

6 files changed

+111
-36
lines changed

6 files changed

+111
-36
lines changed

Cargo.lock

+23-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rooz"
3-
version = "0.60.0"
3+
version = "0.61.0"
44
edition = "2021"
55

66
[dependencies]
@@ -19,6 +19,7 @@ log = "0.4.20"
1919
rand = "0.8.5"
2020
regex = "1.7.1"
2121
serde = "1.0.188"
22+
serde_yaml = "0.9.34"
2223
termion = "2.0.1"
2324
tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros"] }
2425
toml = "0.7.8"

src/api/workspace.rs

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ impl<'a> WorkspaceApi<'a> {
216216
root: bool,
217217
ephemeral: bool,
218218
) -> Result<(), AnyError> {
219-
220219
println!("{}", termion::clear::All);
221220

222221
let enter_labels = Labels::new(Some(workspace_key), None)

src/git.rs

+40-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{
2-
api::{container, GitApi},
2+
api::{container, ExecApi, GitApi},
33
id,
44
labels::Labels,
55
model::{
6-
config::RoozCfg,
6+
config::{FileFormat, RoozCfg},
77
types::{AnyError, ContainerResult, GitCloneSpec, RunSpec},
88
volume::RoozVolume,
99
},
@@ -26,6 +26,35 @@ fn get_clone_dir(root_dir: &str, git_ssh_url: &str) -> String {
2626
work_dir
2727
}
2828

29+
impl<'a> ExecApi<'a> {
30+
pub async fn read_rooz_config(
31+
&self,
32+
container_id: &str,
33+
clone_dir: &str,
34+
file_format: FileFormat,
35+
) -> Result<(FileFormat, String), AnyError> {
36+
let file_path = &format!("{}/.rooz.{}", clone_dir, file_format.to_string());
37+
38+
let config = self
39+
.output(
40+
"rooz-cfg",
41+
&container_id,
42+
None,
43+
Some(vec![
44+
"sh",
45+
"-c",
46+
format!(
47+
"ls {} > /dev/null 2>&1 && cat `ls {} | head -1`",
48+
file_path, file_path
49+
)
50+
.as_ref(),
51+
]),
52+
)
53+
.await?;
54+
Ok((file_format, config))
55+
}
56+
}
57+
2958
impl<'a> GitApi<'a> {
3059
pub async fn clone_repo(
3160
&self,
@@ -96,24 +125,16 @@ impl<'a> GitApi<'a> {
96125
.await?;
97126
};
98127

99-
let rooz_cfg = self
128+
let (file_format, rooz_cfg) = self
100129
.api
101130
.exec
102-
.output(
103-
"rooz-toml",
104-
&container_id,
105-
None,
106-
Some(vec![
107-
"sh",
108-
"-c",
109-
format!(
110-
"ls {}/.rooz.toml > /dev/null 2>&1 && cat {}/.rooz.toml",
111-
clone_dir, clone_dir
112-
)
113-
.as_ref(),
114-
]),
115-
)
116-
.await?;
131+
.read_rooz_config(container_id, &clone_dir, FileFormat::Toml)
132+
.await
133+
.or(self
134+
.api
135+
.exec
136+
.read_rooz_config(container_id, &clone_dir, FileFormat::Yaml)
137+
.await)?;
117138

118139
log::debug!("Repo config result: {}", &rooz_cfg);
119140

@@ -123,7 +144,7 @@ impl<'a> GitApi<'a> {
123144
if rooz_cfg.is_empty() {
124145
None
125146
} else {
126-
Some(RoozCfg::from_string(rooz_cfg))
147+
Some(RoozCfg::from_string(rooz_cfg, file_format))
127148
},
128149
GitCloneSpec { dir: clone_dir },
129150
))

src/main.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ async fn main() -> Result<(), AnyError> {
3636

3737
let args = Cli::parse();
3838

39-
let connection = if let Cli{env_remote:Some(true), command:_ } = args {
39+
let connection = if let Cli {
40+
env_remote: Some(true),
41+
command: _,
42+
} = args
43+
{
4044
Docker::connect_with_ssl_defaults()
4145
} else {
4246
Docker::connect_with_local_defaults()
4347
};
4448

4549
let docker = connection.expect("Docker API connection established");
46-
50+
4751
log::debug!("Client ver: {}", &docker.client_version());
4852

4953
let version = &docker.version().await?;
@@ -211,7 +215,13 @@ async fn main() -> Result<(), AnyError> {
211215
.await?
212216
}
213217

214-
Cli {command:System(cli::System{command:cli::SystemCommands::Completion(CompletionParams{shell}),}), env_remote: _ } => {
218+
Cli {
219+
command:
220+
System(cli::System {
221+
command: cli::SystemCommands::Completion(CompletionParams { shell }),
222+
}),
223+
env_remote: _,
224+
} => {
215225
let mut cli = Cli::command()
216226
.disable_help_flag(true)
217227
.disable_help_subcommand(true);

src/model/config.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{cli::WorkParams, constants};
22
use serde::{Deserialize, Serialize};
3-
use std::{collections::HashMap, fs};
3+
use std::{collections::HashMap, ffi::OsStr, fs, path::Path};
44

55
use super::types::AnyError;
66

@@ -28,18 +28,42 @@ pub struct RoozCfg {
2828
pub privileged: Option<bool>,
2929
}
3030

31-
impl RoozCfg {
32-
pub fn from_file(path: &str) -> Result<RoozCfg, Box<dyn std::error::Error + 'static>> {
33-
Self::from_string(fs::read_to_string(path)?)
31+
#[derive(Debug, Clone)]
32+
pub enum FileFormat {
33+
Toml,
34+
Yaml,
35+
}
36+
37+
impl FileFormat {
38+
pub fn to_string(&self) -> String {
39+
match self {
40+
FileFormat::Toml => "toml".into(),
41+
FileFormat::Yaml => "y*ml".into(),
42+
}
3443
}
3544

36-
pub fn from_string(config: String) -> Result<RoozCfg, Box<dyn std::error::Error + 'static>> {
37-
let f = RoozCfg::deserialize(toml::de::Deserializer::new(&config));
38-
match f {
39-
Ok(val) => Ok(val),
40-
Err(e) => Err(Box::new(e)),
45+
pub fn from_path(path: &str) -> FileFormat {
46+
match Path::new(path).extension().and_then(OsStr::to_str) {
47+
Some("yaml") => FileFormat::Yaml,
48+
Some("yml") => FileFormat::Yaml,
49+
Some("toml") => FileFormat::Toml,
50+
Some(other) => panic!("Config file format: {} is not supported", other),
51+
None => panic!("Only toml and yaml config file formats are supported."),
4152
}
4253
}
54+
}
55+
56+
impl RoozCfg {
57+
pub fn from_file(path: &str) -> Result<RoozCfg, AnyError> {
58+
Self::from_string(fs::read_to_string(path)?, FileFormat::from_path(&path))
59+
}
60+
61+
pub fn from_string(config: String, file_format: FileFormat) -> Result<RoozCfg, AnyError> {
62+
Ok(match file_format {
63+
FileFormat::Yaml => RoozCfg::deserialize(serde_yaml::Deserializer::from_str(&config))?,
64+
FileFormat::Toml => RoozCfg::deserialize(toml::de::Deserializer::new(&config))?,
65+
})
66+
}
4367

4468
fn extend_if_any<A, T: Extend<A> + IntoIterator<Item = A>>(
4569
target: Option<T>,

0 commit comments

Comments
 (0)