This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
staxman-old/src/route/stack/edit.rs

82 lines
1.9 KiB
Rust
Raw Normal View History

2023-11-29 17:29:19 +01:00
use std::collections::HashMap;
2023-11-25 12:24:06 +01:00
use axum::{
extract::{Path, State},
http::StatusCode,
response::Redirect,
Form,
};
use serde::Deserialize;
use crate::{
http::error::{AppError, Result},
2023-11-29 17:29:19 +01:00
stack::{self, arion, compose, FileList, COMPOSE_FILE},
AppState,
};
2023-11-25 12:24:06 +01:00
#[derive(Deserialize)]
pub(super) struct EditStackForm {
2023-11-29 17:29:19 +01:00
files: HashMap<String, String>,
2023-11-25 12:24:06 +01:00
commit_message: String,
}
pub(super) async fn edit_stack(
Path(stack_name): Path<String>,
State(state): State<AppState>,
Form(form): Form<EditStackForm>,
) -> Result<Redirect> {
2023-11-25 12:24:06 +01:00
let commit_message = if form.commit_message.trim().is_empty() {
format!("Update {}", stack_name)
} else {
form.commit_message
};
2023-11-29 17:29:19 +01:00
if form.files.is_empty() {
2023-11-25 12:24:06 +01:00
return Err(AppError::Client {
status: StatusCode::BAD_REQUEST,
2023-11-29 17:29:19 +01:00
code: "invalid-files",
message: "no files provided".to_string(),
2023-11-25 12:24:06 +01:00
});
2023-11-29 17:29:19 +01:00
}
2023-11-25 12:24:06 +01:00
2023-11-29 17:29:19 +01:00
// Cleanup source (like line endings) in each file
let mut files = FileList::new();
for (name, source) in form.files {
files.insert(name, source.replace("\r\n", "\n"));
}
2023-11-25 12:24:06 +01:00
2023-11-29 17:29:19 +01:00
edit_stack_int(state, &stack_name, &files, &commit_message).await?;
Ok(Redirect::to("./"))
}
pub(super) async fn edit_stack_int(
state: AppState,
stack_name: &str,
2023-11-29 17:29:19 +01:00
files: &FileList,
commit_message: &str,
) -> Result<()> {
2023-11-29 17:29:19 +01:00
// Get compose
if let Some(compose_file) = files.get(COMPOSE_FILE) {
// Make sure file is ok
compose::check(&state.arion_bin, compose_file).await?;
}
2023-11-25 12:24:06 +01:00
// Write compose file
2023-11-29 17:29:19 +01:00
stack::write(&state.stack_dir, stack_name, files).await?;
2023-11-25 12:24:06 +01:00
// Git commit
2023-11-29 17:29:19 +01:00
compose::commit(state.repository, stack_name, files, commit_message)?;
2023-11-25 12:24:06 +01:00
// Update stack
2023-11-25 14:05:21 +01:00
arion::command(
2023-11-25 12:24:06 +01:00
&state.stack_dir,
stack_name,
2023-11-25 12:24:06 +01:00
&state.arion_bin,
2023-11-25 14:05:21 +01:00
arion::StackCommand::Start,
2023-11-25 12:24:06 +01:00
)
.await?;
Ok(())
2023-11-25 12:24:06 +01:00
}