81 lines
1.9 KiB
Rust
81 lines
1.9 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use axum::{
|
|
extract::{Path, State},
|
|
http::StatusCode,
|
|
response::Redirect,
|
|
Form,
|
|
};
|
|
use serde::Deserialize;
|
|
|
|
use crate::{
|
|
http::error::{AppError, Result},
|
|
stack::{self, arion, compose, FileList, COMPOSE_FILE},
|
|
AppState,
|
|
};
|
|
|
|
#[derive(Deserialize)]
|
|
pub(super) struct EditStackForm {
|
|
files: HashMap<String, String>,
|
|
commit_message: String,
|
|
}
|
|
|
|
pub(super) async fn edit_stack(
|
|
Path(stack_name): Path<String>,
|
|
State(state): State<AppState>,
|
|
Form(form): Form<EditStackForm>,
|
|
) -> Result<Redirect> {
|
|
let commit_message = if form.commit_message.trim().is_empty() {
|
|
format!("Update {}", stack_name)
|
|
} else {
|
|
form.commit_message
|
|
};
|
|
|
|
if form.files.is_empty() {
|
|
return Err(AppError::Client {
|
|
status: StatusCode::BAD_REQUEST,
|
|
code: "invalid-files",
|
|
message: "no files provided".to_string(),
|
|
});
|
|
}
|
|
|
|
// 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"));
|
|
}
|
|
|
|
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,
|
|
files: &FileList,
|
|
commit_message: &str,
|
|
) -> Result<()> {
|
|
// Get compose
|
|
if let Some(compose_file) = files.get(COMPOSE_FILE) {
|
|
// Make sure file is ok
|
|
compose::check(&state.arion_bin, compose_file).await?;
|
|
}
|
|
|
|
// Write compose file
|
|
stack::write(&state.stack_dir, stack_name, files).await?;
|
|
|
|
// Git commit
|
|
compose::commit(state.repository, stack_name, files, commit_message)?;
|
|
|
|
// Update stack
|
|
arion::command(
|
|
&state.stack_dir,
|
|
stack_name,
|
|
&state.arion_bin,
|
|
arion::StackCommand::Start,
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|