-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use snafu::Snafu; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[derive(Debug, Snafu)] | ||
pub enum ConfigError { | ||
#[snafu(display("Unable to read config file {}: {}", path.display(), source))] | ||
ReadFile { | ||
source: std::io::Error, | ||
path: PathBuf, | ||
}, | ||
#[snafu(display("Unable to write config file {}: {}", path.display(), source))] | ||
WriteFile { | ||
source: std::io::Error, | ||
path: PathBuf, | ||
}, | ||
#[snafu(display("Unable to parse file {}: {}", path.display(), source))] | ||
ParseFile { | ||
source: toml::de::Error, | ||
path: PathBuf, | ||
}, | ||
#[snafu(display("The config file could not be serialized"))] | ||
WriteToml { | ||
source: toml::ser::Error, | ||
path: PathBuf, | ||
}, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
pub struct RenkuProjectConfig { | ||
/// The base url to the renku platform. | ||
pub renku_url: String, | ||
|
||
/// Information about the project | ||
pub project: ProjectInfo, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
pub struct ProjectInfo { | ||
pub id: String, | ||
pub namespace: String, | ||
pub slug: String, | ||
} | ||
|
||
impl RenkuProjectConfig { | ||
pub fn read(file: &Path) -> Result<RenkuProjectConfig, ConfigError> { | ||
let cnt = std::fs::read_to_string(file).map_err(|e| ConfigError::ReadFile { | ||
source: e, | ||
path: file.to_path_buf(), | ||
}); | ||
cnt.and_then(|c| { | ||
toml::from_str(&c).map_err(|e| ConfigError::ParseFile { | ||
source: e, | ||
path: file.to_path_buf(), | ||
}) | ||
}) | ||
} | ||
|
||
pub fn write(&self, file: &Path) -> Result<(), ConfigError> { | ||
if !file.exists() { | ||
if let Some(dir) = file.parent() { | ||
std::fs::create_dir_all(dir).map_err(|e| ConfigError::WriteFile { | ||
source: e, | ||
path: file.to_path_buf(), | ||
})?; | ||
} | ||
} | ||
let cnt = toml::to_string(self).map_err(|e| ConfigError::WriteToml { | ||
source: e, | ||
path: file.to_path_buf(), | ||
}); | ||
|
||
cnt.and_then(|c| { | ||
std::fs::write(file, c).map_err(|e| ConfigError::WriteFile { | ||
source: e, | ||
path: file.to_path_buf(), | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
#[test] | ||
fn write_and_read_config() { | ||
let data = RenkuProjectConfig { | ||
renku_url: "http://renkulab.io".into(), | ||
project: ProjectInfo { | ||
id: "abc123".into(), | ||
namespace: "my-ns".into(), | ||
slug: "projecta".into(), | ||
}, | ||
}; | ||
let target: PathBuf = "test.conf".into(); | ||
data.write(&target).unwrap(); | ||
let from_file = RenkuProjectConfig::read(&target).unwrap(); | ||
std::fs::remove_file(&target).unwrap(); | ||
assert_eq!(data, from_file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod cli; | ||
pub mod config; | ||
pub mod error; | ||
pub mod httpclient; | ||
pub mod util; | ||
|