refactor: some small changes before PR

This commit is contained in:
Jef Roosens 2022-12-19 11:58:35 +01:00
parent b66d1161ed
commit ab81eebd87
Signed by untrusted user: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 31 additions and 34 deletions

View file

@ -86,7 +86,7 @@ fn (mut app App) v1_post_log() web.Result {
}
// Store log in db
log := BuildLog{
mut log := BuildLog{
target_id: target_id
start_time: start_time
end_time: end_time
@ -95,25 +95,20 @@ fn (mut app App) v1_post_log() web.Result {
}
// id of newly created log
log_id := app.db.add_build_log(log)
repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, target_id.str(), arch)
log.id = app.db.add_build_log(log)
log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path())
// Create the logs directory of it doesn't exist
if !os.exists(repo_logs_dir) {
os.mkdir_all(repo_logs_dir) or {
app.lerror("Couldn't create dir '$repo_logs_dir'.")
if !os.exists(os.dir(log_file_path)) {
os.mkdir_all(os.dir(log_file_path)) or {
app.lerror('Error while creating log file: $err.msg()')
return app.status(.internal_server_error)
}
}
// Stream log contents to correct file
file_name := start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
full_path := os.join_path_single(repo_logs_dir, file_name)
if length := app.req.header.get(.content_length) {
util.reader_to_file(mut app.reader, length.int(), full_path) or {
util.reader_to_file(mut app.reader, length.int(), log_file_path) or {
app.lerror('An error occured while receiving logs: $err.msg()')
return app.status(.internal_server_error)
@ -122,16 +117,14 @@ fn (mut app App) v1_post_log() web.Result {
return app.status(.length_required)
}
return app.json(.ok, new_data_response(log_id))
return app.json(.ok, new_data_response(log.id))
}
// v1_delete_log allows removing a build log from the system.
['/api/v1/logs/:id'; auth; delete]
fn (mut app App) v1_delete_log(id int) web.Result {
log := app.db.get_build_log(id) or { return app.status(.not_found) }
file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), log.arch,
file_name)
full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path())
os.rm(full_path) or {
app.lerror('Failed to remove log file $full_path: $err.msg()')