Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ name: CI

on:
pull_request:
push:
branches: [master]
workflow_call:

env:
CARGO_TERM_COLOR: always
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ permissions:
contents: write

jobs:
test:
uses: ./.github/workflows/ci.yml
build:
name: Build (${{ matrix.os }})
runs-on: ${{ matrix.os }}
Expand Down
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tempfile = "3.27.0"
imagesize = "0.15.0"
base64 = "0.23.1"
image = { version = "0.25.10", default-features = false, features = ["png", "jpeg", "webp"] }

tracing-appender = "0.2.5"
ureq = { version = "3.4.2", features = ["json"] }

# Fast iteration: skip most optimization, keep debug info light,
Expand Down
3 changes: 3 additions & 0 deletions locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ label:
search_images: Search Images
file_type: File Type
ok: OK
export_logs: Export Logs
download: Download
description:
use_attach: Attach to running browser instead of starting a new browser, you might want to use this mode if you want specific browser settings to be applied.
min_height: Height must be greater than %{min}.
Expand Down Expand Up @@ -145,3 +147,4 @@ error:
failed_to_select_save_to: Failed to select save to folder
selected_folder_not_in_project: Selected folder is not in the project
cannot_save_to_existing_file: Cannot save to existing file
failed_to_export_logs: Failed to export logs
40 changes: 24 additions & 16 deletions src/config/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ impl AppConfig {
pub fn load() -> Self {
Self::load_from(&Self::config_path())
}
pub fn log_dir() -> PathBuf {
Self::config_dir().join("logs")
}

pub fn save(&self) {
self.save_to(&Self::config_path());
}
fn load_from(config_path: &Path) -> Self {
tracing::info!("Loading app config");

match fs::read_to_string(&config_path) {
match fs::read_to_string(config_path) {
Ok(contents) => match serde_json::from_str(&contents) {
Ok(config) => config,
Err(error) => {
Expand All @@ -56,32 +59,34 @@ impl AppConfig {
"Could not parse app config; using defaults"
);
let config = Self::default();
config.save();
config.save_to(config_path);
config
}
},
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
let config = Self::default();
config.save();
config
}

Err(error) => {
tracing::warn!(
?error,
path = %config_path.display(),
"Could not read app config; using defaults"
);
Self::default()
let config = Self::default();
config.save_to(config_path);
config
}
}
}

fn save_to(&self, config_path: &Path) {
let app_dir = Self::config_dir();
if let Err(error) = fs::create_dir_all(app_dir)
.and_then(|_| serde_json::to_string_pretty(self).map_err(std::io::Error::other))
.and_then(|contents| fs::write(config_path, contents))
{
let result = (|| -> std::io::Result<()> {
if let Some(parent) = config_path.parent() {
fs::create_dir_all(parent)?;
}
let contents = serde_json::to_string_pretty(self).map_err(std::io::Error::other)?;
fs::write(config_path, contents)
})();

if let Err(error) = result {
tracing::warn!(
?error,
path = %config_path.display(),
Expand All @@ -94,7 +99,7 @@ impl AppConfig {
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{json, Value};
use serde_json::{Value, json};
use tempfile::tempdir;

// Compare via JSON so the tests don't need PartialEq on the nested types.
Expand Down Expand Up @@ -213,7 +218,10 @@ mod tests {
let config = AppConfig::load_from(&path);

assert_eq!(as_json(&config), as_json(&AppConfig::default()));
assert!(path.is_dir(), "load must not clobber the path on read errors");
assert!(
path.is_dir(),
"load must not clobber the path on read errors"
);
}

#[test]
Expand All @@ -240,4 +248,4 @@ mod tests {
let contents = fs::read_to_string(&path).unwrap();
assert!(serde_json::from_str::<Value>(&contents).is_ok());
}
}
}
16 changes: 12 additions & 4 deletions src/config/image_format.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use headless_chrome::protocol::cdp::Page::CaptureScreenshotFormatOption;
use serde::{Deserialize, Serialize};

Expand All @@ -17,15 +19,15 @@ impl ImageFormat {
ImageFormat::Webp => CaptureScreenshotFormatOption::Webp,
}
}

pub fn all_str() -> &'static [&'static str] {
&["png", "jpeg", "webp"]
}

pub fn all() -> &'static [ImageFormat] {
&[ImageFormat::Png, ImageFormat::Jpeg, ImageFormat::Webp]
}

pub fn to_value(&self) -> &str {
match self {
ImageFormat::Png => "png",
Expand All @@ -41,7 +43,13 @@ impl ImageFormat {
_ => ImageFormat::Png,
}
}
pub fn to_string(&self) -> String {
self.to_value().to_uppercase()
// pub fn to_string(&self) -> String {
// self.to_value().to_uppercase().clone()
// }
}

impl fmt::Display for ImageFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_value().to_uppercase())
}
}
11 changes: 7 additions & 4 deletions src/config/project_settings_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fs, path::PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};

use serde::{Deserialize, Serialize};

Expand All @@ -22,12 +25,12 @@ impl Default for ProjectSettingsData {
}
}
impl ProjectSettingsData {
pub fn save(&self, project_path: &PathBuf) {
pub fn save(&self, project_path: &Path) {
let config_path = project_path.join(".dockeep");
let contents = serde_json::to_string(self).unwrap();
fs::write(&config_path, contents).unwrap();
}
pub fn load(project_path: &PathBuf) -> Self {
pub fn load(project_path: &Path) -> Self {
let config_path = project_path.join(".dockeep");
if config_path.exists() {
match fs::read_to_string(&config_path) {
Expand Down Expand Up @@ -55,6 +58,6 @@ impl ProjectSettingsData {
}
tracing::info!("Loaded default project settings ",);
// any error, or config not found, return default
return Self::default();
Self::default()
}
}
18 changes: 10 additions & 8 deletions src/files/files_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
mod tests {

use crate::config::ImageFormat;
use crate::files::files::*;
use crate::files::prelude::*;

use super::*;
use image::{DynamicImage, ImageFormat as ImgFmt};
use std::collections::BTreeSet;
use std::fs;
Expand All @@ -30,7 +29,10 @@ mod tests {
}

fn names(paths: Vec<PathBuf>) -> BTreeSet<String> {
paths.iter().map(file_name_with_extension).collect()
paths
.iter()
.map(|ee| file_name_with_extension(ee))
.collect()
}

const URL: &str = "https://example.com/page?a=1&b=2";
Expand Down Expand Up @@ -329,7 +331,7 @@ mod tests {
fs::write(dir.path().join("sub/b.txt"), b"").unwrap();
fs::write(dir.path().join("sub/deeper/c.txt"), b"").unwrap();

let got = names(read_dir(&dir.path().to_path_buf()));
let got = names(read_dir(dir.path()));
assert_eq!(
got,
["a.txt", "b.txt", "c.txt"]
Expand All @@ -347,7 +349,7 @@ mod tests {
fs::write(dir.path().join("keep.png"), b"").unwrap();
fs::write(dir.path().join(".dockeepignore"), "skip\n").unwrap();

let got = names(read_dir(&dir.path().to_path_buf()));
let got = names(read_dir(dir.path()));
assert!(got.contains("keep.png"));
assert!(!got.contains("hidden.png"));
}
Expand All @@ -359,7 +361,7 @@ mod tests {
fs::write(dir.path().join("b.tmp"), b"").unwrap();
fs::write(dir.path().join(".gitignore"), "*.tmp\n").unwrap();

let got = names(read_dir(&dir.path().to_path_buf()));
let got = names(read_dir(dir.path()));
assert!(got.contains("a.png"));
assert!(!got.contains("b.tmp"));
}
Expand All @@ -386,7 +388,7 @@ mod tests {
fs::write(p.join("anim.gif"), b"GIF89a....").unwrap(); // header ok, ext not allowed
fs::write(p.join("noext"), encoded(ImgFmt::Png)).unwrap(); // no ext

let got = names(read_images(&p.to_path_buf()));
let got = names(read_images(p));
let want: BTreeSet<String> = [
"real.png",
"real.jpg",
Expand All @@ -408,7 +410,7 @@ mod tests {
fs::write(dir.path().join("y.png"), encoded(ImgFmt::Png)).unwrap();
fs::write(dir.path().join(".dockeepignore"), "cache\n").unwrap();

let got = names(read_images(&dir.path().to_path_buf()));
let got = names(read_images(dir.path()));
assert_eq!(got, ["y.png".to_string()].into_iter().collect());
}
}
2 changes: 1 addition & 1 deletion src/files/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod files_test;
pub mod files;
pub mod prelude;
Loading
Loading