Update to add git pull

This commit is contained in:
Maix0 2024-06-27 14:15:50 +02:00
parent 72b3eb30c3
commit 7a6f5d3ad3
2 changed files with 55 additions and 14 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

View file

@ -483,17 +483,25 @@ async fn stop(_user: UserLoggedIn) -> Redirect {
#[derive(Serialize, Deserialize)]
struct BotConfig {
piscine: Vec<String>,
pc_tut: Vec<String>,
id_server: String,
id_channel_alerte: String,
id_rote: String,
mois: String,
annee: String,
}
async fn get_config(_user: UserLoggedIn) -> Result<Json<BotConfig>, (StatusCode, String)> {
info!("Requested config");
let Ok(mut file) = tokio::fs::File::open(std::env::var("CONFIG_PATH").map_err(|e| {
let Ok(mut file) = tokio::fs::File::open(
std::env::var("BOTLOC_DIR").map_err(|e| {
error!("Failed to open config file: {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
"please set env CONFIG_PATH".to_string(),
)
})?)
})? + "/config.json",
)
.await
.map_err(|e| {
error!("Failed to open config file: {e}");
@ -502,8 +510,8 @@ async fn get_config(_user: UserLoggedIn) -> Result<Json<BotConfig>, (StatusCode,
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!(
"failed to open config file: {}",
std::env::var("CONFIG_PATH").unwrap()
"failed to open config file: {}/config.json",
std::env::var("BOTLOC_DIR").unwrap()
),
));
};
@ -521,8 +529,8 @@ async fn get_config(_user: UserLoggedIn) -> Result<Json<BotConfig>, (StatusCode,
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!(
"Failed to read config file at {}",
std::env::var("CONFIG_PATH").unwrap()
"Failed to read config file at {}/config.json",
std::env::var("BOTLOC_DIR").unwrap()
),
));
};
@ -533,8 +541,8 @@ async fn get_config(_user: UserLoggedIn) -> Result<Json<BotConfig>, (StatusCode,
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!(
"Failed to read config file as json at {}",
std::env::var("CONFIG_PATH").unwrap()
"Failed to read config file as json at {}/config.json",
std::env::var("BOTLOC_DIR").unwrap()
),
));
};
@ -559,3 +567,35 @@ async fn status() -> Result<String, StatusCode> {
StatusCode::INTERNAL_SERVER_ERROR
})
}
async fn git_pull() -> Result<String, (StatusCode, &'static str)> {
info!("Requested to pull");
let mut output = tokio::process::Command::new("git")
.current_dir(std::env::var("BOTLOC_DIR").map_err(|e| {
error!("Error with git pull command {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
"Please set the BOTLOC_DIR variable",
)
})?)
.args(["pull"])
.output()
.await
// let mut output = child.wait_with_output().await
.map_err(|e| {
error!("Error with git pull command {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
"Error with the git pull command!",
)
})?;
output.stdout.push(b'\n');
output.stdout.append(&mut output.stderr);
String::from_utf8(output.stdout).map_err(|e| {
error!("Error with git pull output {e}");
(
StatusCode::INTERNAL_SERVER_ERROR,
"Error with the git pull output!",
)
})
}