Skip to content

Commit

Permalink
feat: 配置初始化失败时,备份配置文件 (#10)
Browse files Browse the repository at this point in the history
* feat: update ci

* feat: 配置初始化失败时,备份配置文件
  • Loading branch information
nashaofu authored Sep 7, 2024
1 parent ced7dbf commit 322b7a9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 56 deletions.
80 changes: 38 additions & 42 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,71 @@ jobs:
settings:
- target: x86_64-apple-darwin
host: macos-latest
use-cross: false
post-build: |
build: |
cargo build --release --target x86_64-apple-darwin && \
mkdir build && \
cp target/x86_64-apple-darwin/release/wol build/wol && \
strip -x build/wol
- target: aarch64-apple-darwin
host: macos-latest
use-cross: false
post-build: |
build: |
cargo build --release --target aarch64-apple-darwin && \
mkdir build && \
cp target/aarch64-apple-darwin/release/wol build/wol && \
strip -x build/wol
- target: x86_64-pc-windows-msvc
host: windows-latest
use-cross: false
post-build: |
build: |
cargo build --release --target x86_64-pc-windows-msvc
md build
copy target/x86_64-pc-windows-msvc/release/wol.exe build/wol.exe
- target: aarch64-pc-windows-msvc
host: windows-latest
use-cross: false
post-build: |
build: |
cargo build --release --target aarch64-pc-windows-msvc
md build
copy target/aarch64-pc-windows-msvc/release/wol.exe build/wol.exe
- target: x86_64-unknown-linux-gnu
host: ubuntu-latest
use-cross: true
post-build: |
build: |
cargo install cross --git https://github.com/cross-rs/cross && \
cross build --release --target x86_64-unknown-linux-gnu && \
mkdir build && \
cp target/x86_64-unknown-linux-gnu/release/wol build/wol && \
strip -x build/wol
- target: x86_64-unknown-linux-musl
host: ubuntu-latest
use-cross: true
post-build: |
build: |
cargo install cross --git https://github.com/cross-rs/cross && \
cross build --release --target x86_64-unknown-linux-musl && \
mkdir build && \
cp target/x86_64-unknown-linux-musl/release/wol build/wol && \
strip -x build/wol
- target: aarch64-unknown-linux-gnu
host: ubuntu-latest
use-cross: true
post-build: |
build: |
cargo install cross --git https://github.com/cross-rs/cross && \
cross build --release --target aarch64-unknown-linux-gnu && \
mkdir build && \
cp target/aarch64-unknown-linux-gnu/release/wol build/wol
- target: aarch64-unknown-linux-musl
host: ubuntu-latest
use-cross: true
post-build: |
build: |
cargo install cross --git https://github.com/cross-rs/cross && \
cross build --release --target aarch64-unknown-linux-musl && \
mkdir build && \
cp target/aarch64-unknown-linux-musl/release/wol build/wol
name: build ${{ matrix.settings.target }}
runs-on: ${{ matrix.settings.host }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Cache cargo
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
Expand All @@ -95,14 +99,13 @@ jobs:
target/
key: ${{ runner.os }}-${{ matrix.settings.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: ${{ matrix.settings.target }}
override: true

- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: latest
cache: "yarn"
Expand All @@ -115,16 +118,10 @@ jobs:
run: mv web/dist/* www

- name: Build server
uses: actions-rs/cargo@v1
with:
use-cross: ${{ matrix.settings.use-cross }}
command: build
args: --release --target ${{ matrix.settings.target }}
- name: Post build
run: ${{ matrix.settings.post-build }}
run: ${{ matrix.settings.build }}

- name: Upload artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: wol-${{ matrix.settings.target }}
path: build/
Expand All @@ -134,11 +131,12 @@ jobs:
runs-on: ubuntu-latest
needs:
- build
if: startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Download all artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Display structure of downloaded files
Expand All @@ -151,29 +149,27 @@ jobs:
run: ls -R
working-directory: artifacts
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v2
with:
files: artifacts/*.zip

docker:
runs-on: ubuntu-latest
needs:
- publish
if: startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup QEMU
uses: docker/setup-qemu-action@v2
# Workaround: https://github.com/docker/build-push-action/issues/461
uses: docker/setup-qemu-action@v3

- name: Setup docker buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Login to registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
- name: Login to ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
Expand All @@ -184,5 +180,5 @@ jobs:
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
push: true
tags: ${{ env.REGISTRY }}/nashaofu/wol:latest
20 changes: 10 additions & 10 deletions Cargo.lock

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

29 changes: 25 additions & 4 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::args::ARGS;

use std::{env, fs, path::PathBuf, sync::RwLock};

use anyhow::Result;
use anyhow::{anyhow, Result};
use config::{Config, File};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -43,9 +43,30 @@ impl Settings {
.add_source(File::with_name(&CONFIG_FILE.display().to_string()).required(false))
.build()?;

let devices = config
.get::<Vec<Device>>("devices")
.unwrap_or(Vec::default());
let devices = match config.get::<Vec<Device>>("devices") {
Ok(devices) => devices,
Err(err) => {
log::error!("Failed get devices from config: {}", err);

if CONFIG_FILE.exists() {
let filename = CONFIG_FILE
.file_name()
.and_then(|name| name.to_str())
.ok_or(anyhow!("Failed get config file name"))?;

let target = CONFIG_FILE
.parent()
.ok_or(anyhow!("Failed get config file parent dirname"))?
.join(format!("{}.backup", filename));

log::info!("Backup config file {} to {}", CONFIG_FILE.display(), target.display());

fs::copy(CONFIG_FILE.as_path(), target)?;
}

Vec::new()
}
};

let auth = config.get::<Auth>("auth").ok();

Expand Down
1 change: 1 addition & 0 deletions wol.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ devices:
- name: Windows
mac: 00:00:00:00:00:00
ip: 192.168.1.1
netmask: 255.255.255.0
port: 9

0 comments on commit 322b7a9

Please sign in to comment.