Skip to content

Commit

Permalink
Write renku config when cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
eikek committed Jul 16, 2024
1 parent c104957 commit 591086f
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 1 deletion.
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tokio = { version = "1", features = ["full"] }
futures = { version = "0.3" }
regex = { version = "1.10.5" }
iso8601-timestamp = { version = "0.2.17" }
toml = { version = "0.8.12" }
git2 = "0.19.0"
comrak = { version = "0.24.1", optional = true }

Expand Down
23 changes: 22 additions & 1 deletion src/cli/cmd/project/clone.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::{ConfigError, ProjectInfo, RenkuProjectConfig};
use crate::httpclient::data::ProjectDetails;

use super::Context;
Expand All @@ -9,7 +10,7 @@ use std::sync::Arc;
use clap::Parser;
use git2::{Error as GitError, Repository};
use snafu::{ResultExt, Snafu};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use tokio::task::{JoinError, JoinSet};

/// Clone a project.
Expand Down Expand Up @@ -53,6 +54,9 @@ pub enum Error {

#[snafu(display("Error in task: {}", source))]
TaskJoin { source: JoinError },

#[snafu(display("Error creating config file: {}", source))]
RenkuConfig { source: ConfigError },
}

impl Input {
Expand All @@ -72,6 +76,14 @@ impl Input {
};
if let Some(details) = opt_details {
let target = self.target_dir()?.join(&details.slug);
let renku_project_cfg = RenkuProjectConfig {
renku_url: ctx.renku_url.clone(),
project: ProjectInfo {
id: details.id.clone(),
namespace: details.namespace.clone(),
slug: details.slug.clone(),
},
};
ctx.write_err(&SimpleMessage {
message: format!(
"Cloning {} ({}) into {}...",
Expand All @@ -83,6 +95,8 @@ impl Input {
.await
.context(WriteResultSnafu)?;

write_config(renku_project_cfg, &target).await?;

let ctx = clone_project(ctx, &details, target).await?;
ctx.write_result(&details).await.context(WriteResultSnafu)?;
} else {
Expand Down Expand Up @@ -176,3 +190,10 @@ async fn clone_repository(
}
Ok(())
}

async fn write_config(data: RenkuProjectConfig, local_dir: &Path) -> Result<(), Error> {
let target = local_dir.join(".renku").join("config.toml");
tokio::task::spawn_blocking(move || data.write(&target).context(RenkuConfigSnafu))
.await
.context(TaskJoinSnafu)?
}
97 changes: 97 additions & 0 deletions src/config.rs
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);
}
1 change: 1 addition & 0 deletions src/lib.rs
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;
Expand Down

0 comments on commit 591086f

Please sign in to comment.