Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): expand sync path argument #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 64 additions & 23 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions edu-sync-cli/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use dialoguer::Password;
use edu_sync::{
account::Account,
config::{AccountConfig, Config},
config::{check_dir, AccountConfig, Config},
};
use tokio::task;
use url::Url;
Expand Down Expand Up @@ -47,8 +47,9 @@ impl Subcommand {
.await??
.parse()?
};
let real_path = check_dir(&self.path)?;

let account_config = AccountConfig::new(self.url, token, self.path, self.lang).await?;
let account_config = AccountConfig::new(self.url, token, real_path, self.lang).await?;
let mut config = config_task.await??;
let account_name = account_config.to_string();
config
Expand Down
1 change: 1 addition & 0 deletions edu-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tokio = { version = "1", default-features = false, features = ["fs"] }
toml = "0.8"
tracing = "0.1"
url = "2.2"
shellexpand = { version = "3.1.0", features = ["path"] }
29 changes: 28 additions & 1 deletion edu-sync/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use edu_ws::{
ws,
};
use reqwest::Url;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::{serde_as, serde_conv, DisplayFromStr};
use thiserror::Error;
use tokio::{
Expand Down Expand Up @@ -105,6 +105,32 @@ impl CourseConfigs {
}
}

// Expand Tilde to home folder, create the directory if it does not exist and
// then canonicalize the path.
pub fn check_dir(path: &PathBuf) -> Result<PathBuf, std::io::Error> {
let expanded_path = shellexpand::path::tilde(&path).into_owned();
if !expanded_path.try_exists()? {
std::fs::create_dir(&expanded_path)?;
}
std::fs::canonicalize(expanded_path)
}

// Custom deserializer function to check if the path is absolute and to
// canonicalize the path
fn deserialize_absolute_path<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
where
D: Deserializer<'de>,
{
let path_str = String::deserialize(deserializer)?;
let path = PathBuf::from(path_str);

if !path.is_absolute() {
return Err(serde::de::Error::custom("Path must be absolute"));
}

check_dir(&path).map_err(|io_err| serde::de::Error::custom(format!("{io_err}")))
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct AccountConfig {
Expand All @@ -113,6 +139,7 @@ pub struct AccountConfig {
#[serde(flatten)]
pub id: Id,
pub token: Token,
#[serde(deserialize_with = "deserialize_absolute_path")]
pub path: PathBuf,
#[serde(default)]
pub courses: CourseConfigs,
Expand Down