mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 04:55:42 +09:00
refactor(svc): tidy the rust services and build tooling (#2735)
This commit is contained in:
1 parent
33737e0f79
commit
6af33c7188
41 files changed
+863
-1304
No files matched your search
@@ -6,6 +6,7 @@ use crate::common::{
|
||||
remove_dir_if_exists, require_env, resolve_calver, run_command, runner_temp, s3_client,
|
||||
trim_option, upload_s3_plan_append_only,
|
||||
};
|
||||
use crate::functions::sha256_reader;
|
||||
use anyhow::{Context, Result, anyhow, ensure};
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD as BASE64;
|
||||
@@ -13,11 +14,9 @@ use chrono::Utc;
|
||||
use clap::{Args, ValueEnum};
|
||||
use reqwest::Client;
|
||||
use serde_json::{Map, Value, json};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::env;
|
||||
use std::fs::{self, File};
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
@@ -586,20 +585,8 @@ fn asset_tree_digests(root: &Path) -> Result<BTreeMap<String, String>> {
|
||||
}
|
||||
|
||||
fn sha256_file(path: &Path) -> Result<String> {
|
||||
let mut file =
|
||||
File::open(path).with_context(|| format!("Failed to open {}", path.display()))?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0u8; 64 * 1024];
|
||||
loop {
|
||||
let read = file
|
||||
.read(&mut buffer)
|
||||
.with_context(|| format!("Failed to read {}", path.display()))?;
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buffer[..read]);
|
||||
}
|
||||
Ok(hex::encode(hasher.finalize()))
|
||||
let file = File::open(path).with_context(|| format!("Failed to open {}", path.display()))?;
|
||||
sha256_reader(file).with_context(|| format!("Failed to read {}", path.display()))
|
||||
}
|
||||
|
||||
fn tree_differences(
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use crate::common::{
|
||||
CALVER_SCHEME, CalverEnv, append_github_env, append_github_output, parse_version_instant,
|
||||
resolve_calver, trim_option,
|
||||
CALVER_SCHEME, CalverEnv, append_github_env, append_github_output, micro_segment,
|
||||
month_day_segment, parse_version_instant, resolve_calver, trim_option,
|
||||
};
|
||||
use anyhow::Result;
|
||||
use chrono::{Datelike, Timelike, Utc};
|
||||
@@ -75,14 +75,10 @@ fn calver_outputs(version: &str) -> Result<CalverOutputs> {
|
||||
instant.minute(),
|
||||
instant.second()
|
||||
);
|
||||
let micro = time
|
||||
.parse::<u32>()
|
||||
.expect("HHMMSS time segment should parse")
|
||||
.to_string();
|
||||
Ok(CalverOutputs {
|
||||
version: version.to_string(),
|
||||
time,
|
||||
micro,
|
||||
micro: micro_segment(instant),
|
||||
date: format!(
|
||||
"{:04}{:02}{:02}",
|
||||
instant.year(),
|
||||
@@ -92,7 +88,7 @@ fn calver_outputs(version: &str) -> Result<CalverOutputs> {
|
||||
year: instant.year().to_string(),
|
||||
month: instant.month().to_string(),
|
||||
day: format!("{:02}", instant.day()),
|
||||
month_day: format!("{}{:02}", instant.month(), instant.day()),
|
||||
month_day: month_day_segment(instant),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+17
-25
@@ -365,20 +365,12 @@ fn format_calver(instant: DateTime<Utc>) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
fn month_day_segment(instant: DateTime<Utc>) -> String {
|
||||
pub(crate) fn month_day_segment(instant: DateTime<Utc>) -> String {
|
||||
format!("{}{:02}", instant.month(), instant.day())
|
||||
}
|
||||
|
||||
fn micro_segment(instant: DateTime<Utc>) -> String {
|
||||
format!(
|
||||
"{:02}{:02}{:02}",
|
||||
instant.hour(),
|
||||
instant.minute(),
|
||||
instant.second()
|
||||
)
|
||||
.parse::<u32>()
|
||||
.expect("HHMMSS time segment should parse")
|
||||
.to_string()
|
||||
pub(crate) fn micro_segment(instant: DateTime<Utc>) -> String {
|
||||
(instant.hour() * 10_000 + instant.minute() * 100 + instant.second()).to_string()
|
||||
}
|
||||
|
||||
pub(crate) fn parse_version_instant(version: &str) -> Result<DateTime<Utc>> {
|
||||
@@ -1312,13 +1304,13 @@ pub(crate) fn collect_files(root: &Path) -> Result<Vec<PathBuf>> {
|
||||
if !root.exists() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let mut files = WalkDir::new(root)
|
||||
.into_iter()
|
||||
.collect::<std::result::Result<Vec<_>, _>>()?
|
||||
.into_iter()
|
||||
.filter(|entry| entry.file_type().is_file())
|
||||
.map(|entry| entry.into_path())
|
||||
.collect::<Vec<_>>();
|
||||
let mut files = Vec::new();
|
||||
for entry in WalkDir::new(root) {
|
||||
let entry = entry?;
|
||||
if entry.file_type().is_file() {
|
||||
files.push(entry.into_path());
|
||||
}
|
||||
}
|
||||
files.sort();
|
||||
Ok(files)
|
||||
}
|
||||
@@ -1328,13 +1320,13 @@ pub(crate) fn count_files(root: &Path) -> Result<usize> {
|
||||
}
|
||||
|
||||
pub(crate) fn count_files_min_depth(root: &Path, min_depth: usize) -> Result<usize> {
|
||||
Ok(WalkDir::new(root)
|
||||
.min_depth(min_depth)
|
||||
.into_iter()
|
||||
.collect::<std::result::Result<Vec<_>, _>>()?
|
||||
.into_iter()
|
||||
.filter(|entry| entry.file_type().is_file())
|
||||
.count())
|
||||
let mut count = 0;
|
||||
for entry in WalkDir::new(root).min_depth(min_depth) {
|
||||
if entry?.file_type().is_file() {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
pub(crate) fn title_case(value: &str) -> String {
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Serialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
pub(crate) fn remove_file_if_exists(path: &Path) -> Result<()> {
|
||||
@@ -32,6 +33,19 @@ pub(crate) fn write_json_pretty<T: Serialize + ?Sized>(path: &Path, value: &T) -
|
||||
fs::write(path, bytes).with_context(|| format!("Failed to write {}", path.display()))
|
||||
}
|
||||
|
||||
pub(crate) fn sha256_reader(mut reader: impl Read) -> io::Result<String> {
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0u8; 64 * 1024];
|
||||
loop {
|
||||
let read = reader.read(&mut buffer)?;
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buffer[..read]);
|
||||
}
|
||||
Ok(hex::encode(hasher.finalize()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
+3
-15
@@ -1,14 +1,13 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use crate::common::{CommandSpec, output_text, parse_version_instant, run_command};
|
||||
use crate::functions::sha256_reader;
|
||||
use anyhow::{Context, Result, bail, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use clap::{Args, Subcommand};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::fs::{self, File};
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
@@ -753,20 +752,9 @@ fn local_release_assets(
|
||||
}
|
||||
|
||||
fn sha256_file(path: &Path) -> Result<String> {
|
||||
let mut file = File::open(path)
|
||||
let file = File::open(path)
|
||||
.with_context(|| format!("Failed to open release asset {}", path.display()))?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0u8; 64 * 1024];
|
||||
loop {
|
||||
let read = file
|
||||
.read(&mut buffer)
|
||||
.with_context(|| format!("Failed to read release asset {}", path.display()))?;
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buffer[..read]);
|
||||
}
|
||||
Ok(hex::encode(hasher.finalize()))
|
||||
sha256_reader(file).with_context(|| format!("Failed to read release asset {}", path.display()))
|
||||
}
|
||||
|
||||
fn create_draft_release(
|
||||
|
||||
@@ -46,9 +46,6 @@ fn find_import_start(source: &str) -> Option<usize> {
|
||||
}
|
||||
offset += line.len();
|
||||
}
|
||||
if source[offset..].starts_with("import ") {
|
||||
return Some(offset);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user