Skip to content

Commit 0c57079

Browse files
committed
fix: 避免更新包覆盖配置文件
1 parent d6051e7 commit 0c57079

8 files changed

Lines changed: 226 additions & 56 deletions

File tree

.github/workflows/release.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,33 @@ jobs:
100100
echo "Build macOS..."
101101
mkdir -p build/darwin-amd64 build/darwin-arm64
102102
GOOS=darwin GOARCH=amd64 go build -ldflags="${STRIP_LDFLAGS}" -trimpath -o build/darwin-amd64/anssl main.go
103-
cp config.example.yaml build/darwin-amd64/config.yaml
104-
tar -C build/darwin-amd64 -czf release/anssl-darwin-amd64.tar.gz anssl config.yaml
103+
cp config.example.yaml build/darwin-amd64/config.example.yaml
104+
tar -C build/darwin-amd64 -czf release/anssl-darwin-amd64.tar.gz anssl config.example.yaml
105105
GOOS=darwin GOARCH=arm64 go build -ldflags="${STRIP_LDFLAGS}" -trimpath -o build/darwin-arm64/anssl main.go
106-
cp config.example.yaml build/darwin-arm64/config.yaml
107-
tar -C build/darwin-arm64 -czf release/anssl-darwin-arm64.tar.gz anssl config.yaml
106+
cp config.example.yaml build/darwin-arm64/config.example.yaml
107+
tar -C build/darwin-arm64 -czf release/anssl-darwin-arm64.tar.gz anssl config.example.yaml
108108
109109
echo "Build Linux..."
110110
mkdir -p build/linux-amd64 build/linux-arm64
111111
GOOS=linux GOARCH=amd64 go build -ldflags="${STRIP_LDFLAGS}" -trimpath -o build/linux-amd64/anssl main.go
112112
echo "UPX compress linux-amd64..."
113113
upx --best build/linux-amd64/anssl || echo "⚠️ ignore UPX failure: linux-amd64"
114-
cp config.example.yaml build/linux-amd64/config.yaml
115-
tar -C build/linux-amd64 -czf release/anssl-linux-amd64.tar.gz anssl config.yaml
114+
cp config.example.yaml build/linux-amd64/config.example.yaml
115+
tar -C build/linux-amd64 -czf release/anssl-linux-amd64.tar.gz anssl config.example.yaml
116116
GOOS=linux GOARCH=arm64 go build -ldflags="${STRIP_LDFLAGS}" -trimpath -o build/linux-arm64/anssl main.go
117117
echo "UPX compress linux-arm64..."
118118
upx --best build/linux-arm64/anssl || echo "⚠️ ignore UPX failure: linux-arm64"
119-
cp config.example.yaml build/linux-arm64/config.yaml
120-
tar -C build/linux-arm64 -czf release/anssl-linux-arm64.tar.gz anssl config.yaml
119+
cp config.example.yaml build/linux-arm64/config.example.yaml
120+
tar -C build/linux-arm64 -czf release/anssl-linux-arm64.tar.gz anssl config.example.yaml
121121
122122
echo "Build Windows..."
123123
mkdir -p build/windows-amd64 build/windows-arm64
124124
GOOS=windows GOARCH=amd64 go build -ldflags="${BASE_LDFLAGS}" -trimpath -o build/windows-amd64/anssl.exe main.go
125-
cp config.example.yaml build/windows-amd64/config.yaml
126-
(cd build/windows-amd64 && zip -q ../../release/anssl-windows-amd64.zip anssl.exe config.yaml)
125+
cp config.example.yaml build/windows-amd64/config.example.yaml
126+
(cd build/windows-amd64 && zip -q ../../release/anssl-windows-amd64.zip anssl.exe config.example.yaml)
127127
GOOS=windows GOARCH=arm64 go build -ldflags="${BASE_LDFLAGS}" -trimpath -o build/windows-arm64/anssl.exe main.go
128-
cp config.example.yaml build/windows-arm64/config.yaml
129-
(cd build/windows-arm64 && zip -q ../../release/anssl-windows-arm64.zip anssl.exe config.yaml)
128+
cp config.example.yaml build/windows-arm64/config.example.yaml
129+
(cd build/windows-arm64 && zip -q ../../release/anssl-windows-arm64.zip anssl.exe config.example.yaml)
130130
131131
- name: Show build info
132132
run: |

Makefile

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
.PHONY: build build-mac build-linux build-windows compress build-compress proto clean install
22

3+
VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null || echo dev)
4+
BASE_LDFLAGS := -X github.com/https-cert/deploy/internal/config.Version=$(VERSION)
5+
STRIP_LDFLAGS := -s -w $(BASE_LDFLAGS)
6+
37
# 默认目标
48
all: proto build
59

@@ -11,50 +15,50 @@ build: build-mac build-linux build-windows
1115
build-mac:
1216
@echo "构建 Mac 版本..."
1317
@mkdir -p bin bin/darwin-amd64 bin/darwin-arm64
14-
@GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o bin/darwin-amd64/anssl main.go
15-
@cp config.example.yaml bin/darwin-amd64/config.yaml
16-
@tar -C bin/darwin-amd64 -czf bin/anssl-darwin-amd64.tar.gz anssl config.yaml
17-
@GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o bin/darwin-arm64/anssl main.go
18-
@cp config.example.yaml bin/darwin-arm64/config.yaml
19-
@tar -C bin/darwin-arm64 -czf bin/anssl-darwin-arm64.tar.gz anssl config.yaml
18+
@GOOS=darwin GOARCH=amd64 go build -ldflags="$(STRIP_LDFLAGS)" -trimpath -o bin/darwin-amd64/anssl main.go
19+
@cp config.example.yaml bin/darwin-amd64/config.example.yaml
20+
@tar -C bin/darwin-amd64 -czf bin/anssl-darwin-amd64.tar.gz anssl config.example.yaml
21+
@GOOS=darwin GOARCH=arm64 go build -ldflags="$(STRIP_LDFLAGS)" -trimpath -o bin/darwin-arm64/anssl main.go
22+
@cp config.example.yaml bin/darwin-arm64/config.example.yaml
23+
@tar -C bin/darwin-arm64 -czf bin/anssl-darwin-arm64.tar.gz anssl config.example.yaml
2024
@rm -rf bin/darwin-amd64 bin/darwin-arm64
2125
@echo "Mac 版本构建完成"
2226

2327
# 构建 Linux 版本(打包为 tar.gz,内部二进制名为 anssl)
2428
build-linux:
2529
@echo "构建 Linux 版本..."
2630
@mkdir -p bin bin/linux-amd64 bin/linux-arm64
27-
@GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o bin/linux-amd64/anssl main.go
31+
@GOOS=linux GOARCH=amd64 go build -ldflags="$(STRIP_LDFLAGS)" -trimpath -o bin/linux-amd64/anssl main.go
2832
@echo "尝试使用 UPX 压缩 linux-amd64 二进制..."
2933
@if command -v upx >/dev/null 2>&1; then \
3034
upx --best bin/linux-amd64/anssl || echo "UPX 压缩失败(linux-amd64),已忽略"; \
3135
else \
3236
echo "UPX 未安装,跳过 linux-amd64 压缩"; \
3337
fi
34-
@cp config.example.yaml bin/linux-amd64/config.yaml
35-
@tar -C bin/linux-amd64 -czf bin/anssl-linux-amd64.tar.gz anssl config.yaml
36-
@GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o bin/linux-arm64/anssl main.go
38+
@cp config.example.yaml bin/linux-amd64/config.example.yaml
39+
@tar -C bin/linux-amd64 -czf bin/anssl-linux-amd64.tar.gz anssl config.example.yaml
40+
@GOOS=linux GOARCH=arm64 go build -ldflags="$(STRIP_LDFLAGS)" -trimpath -o bin/linux-arm64/anssl main.go
3741
@echo "尝试使用 UPX 压缩 linux-arm64 二进制..."
3842
@if command -v upx >/dev/null 2>&1; then \
3943
upx --best bin/linux-arm64/anssl || echo "UPX 压缩失败(linux-arm64),已忽略"; \
4044
else \
4145
echo "UPX 未安装,跳过 linux-arm64 压缩"; \
4246
fi
43-
@cp config.example.yaml bin/linux-arm64/config.yaml
44-
@tar -C bin/linux-arm64 -czf bin/anssl-linux-arm64.tar.gz anssl config.yaml
47+
@cp config.example.yaml bin/linux-arm64/config.example.yaml
48+
@tar -C bin/linux-arm64 -czf bin/anssl-linux-arm64.tar.gz anssl config.example.yaml
4549
@rm -rf bin/linux-amd64 bin/linux-arm64
4650
@echo "Linux 版本构建完成"
4751

4852
# 构建 Windows 版本(打包为 zip,内部二进制名为 anssl.exe)
4953
build-windows:
5054
@echo "构建 Windows 版本..."
5155
@mkdir -p bin bin/windows-amd64 bin/windows-arm64
52-
@GOOS=windows GOARCH=amd64 go build -trimpath -o bin/windows-amd64/anssl.exe main.go
53-
@cp config.example.yaml bin/windows-amd64/config.yaml
54-
@cd bin/windows-amd64 && zip -q ../../bin/anssl-windows-amd64.zip anssl.exe config.yaml
55-
@GOOS=windows GOARCH=arm64 go build -trimpath -o bin/windows-arm64/anssl.exe main.go
56-
@cp config.example.yaml bin/windows-arm64/config.yaml
57-
@cd bin/windows-arm64 && zip -q ../../bin/anssl-windows-arm64.zip anssl.exe config.yaml
56+
@GOOS=windows GOARCH=amd64 go build -ldflags="$(BASE_LDFLAGS)" -trimpath -o bin/windows-amd64/anssl.exe main.go
57+
@cp config.example.yaml bin/windows-amd64/config.example.yaml
58+
@cd bin/windows-amd64 && zip -q ../../bin/anssl-windows-amd64.zip anssl.exe config.example.yaml
59+
@GOOS=windows GOARCH=arm64 go build -ldflags="$(BASE_LDFLAGS)" -trimpath -o bin/windows-arm64/anssl.exe main.go
60+
@cp config.example.yaml bin/windows-arm64/config.example.yaml
61+
@cd bin/windows-arm64 && zip -q ../../bin/anssl-windows-arm64.zip anssl.exe config.example.yaml
5862
@rm -rf bin/windows-amd64 bin/windows-arm64
5963
@echo "Windows 版本构建完成"
6064

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323
wget https://github.com/https-cert/deploy/releases/latest/download/anssl-linux-amd64.tar.gz
2424
tar -xzf anssl-linux-amd64.tar.gz
2525
chmod +x anssl
26+
cp -n config.example.yaml config.yaml
2627
sudo mv anssl /usr/local/bin/
2728
```
2829

2930
### 2. 配置
3031

31-
发布包中已包含 `config.yaml` 模板。启动前请修改其中的 `accessKey` 和需要启用的部署目标。
32-
33-
如果从源码运行,可以复制模板:
32+
发布包中只包含 `config.example.yaml` 模板,不包含真实的 `config.yaml`,避免手动解压更新时覆盖已有配置。首次安装时请复制模板并修改其中的 `accessKey` 和需要启用的部署目标:
3433

3534
```bash
3635
cp config.example.yaml config.yaml
3736
```
3837

38+
后续更新时只替换 `anssl` 可执行文件即可,不需要重新复制配置模板。
39+
3940
`config.yaml` 示例:
4041

4142
```yaml

README_EN.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@ Download the binary for your OS from [GitHub Releases](https://github.com/https-
2323
wget https://github.com/https-cert/deploy/releases/latest/download/anssl-linux-amd64.tar.gz
2424
tar -xzf anssl-linux-amd64.tar.gz
2525
chmod +x anssl
26+
cp -n config.example.yaml config.yaml
2627
sudo mv anssl /usr/local/bin/
2728
```
2829

2930
### 2. Configure
3031

31-
Release archives include a `config.yaml` template. Before starting, edit its `accessKey` and any deployment targets you want to enable.
32-
33-
If you run from source, copy the template first:
32+
Release archives include only `config.example.yaml`, not a real `config.yaml`, so manually extracting an update will not overwrite an existing configuration. On first install, copy the template and edit its `accessKey` and any deployment targets you want to enable:
3433

3534
```bash
3635
cp config.example.yaml config.yaml
3736
```
3837

38+
For later updates, replace only the `anssl` executable; you do not need to copy the configuration template again.
39+
3940
`config.yaml` example:
4041

4142
```yaml

internal/config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
var (
1313
Config *Configuration
14-
Version = "v0.5.6"
1514
URL = URLProd
1615
URLProd = "https://anssl.cn/deploy"
1716
URLLocal = "http://localhost:9000/deploy"

internal/config/version.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package config
2+
3+
// Version is injected by release builds with:
4+
//
5+
// go build -ldflags="-X github.com/https-cert/deploy/internal/config.Version=vX.Y.Z"
6+
//
7+
// Development builds keep "dev" to avoid shipping stale hard-coded versions.
8+
var Version = "dev"

internal/updater/updater.go

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package updater
22

33
import (
4+
"archive/tar"
45
"archive/zip"
56
"compress/gzip"
67
"context"
@@ -18,8 +19,6 @@ import (
1819
"strings"
1920
"time"
2021

21-
"archive/tar"
22-
2322
"github.com/https-cert/deploy/internal/config"
2423
"github.com/https-cert/deploy/pkg/logger"
2524
)
@@ -435,6 +434,14 @@ func getBinaryName() string {
435434
return name
436435
}
437436

437+
// getExecutableName 根据当前系统获取压缩包内的可执行文件名
438+
func getExecutableName() string {
439+
if runtime.GOOS == "windows" {
440+
return "anssl.exe"
441+
}
442+
return "anssl"
443+
}
444+
438445
// downloadFile 下载文件
439446
func downloadFile(ctx context.Context, downloadURL, filepath string) error {
440447
var lastErr error
@@ -494,6 +501,14 @@ func downloadFileOnce(ctx context.Context, downloadURL, filepath string) error {
494501
// extractBinary 从下载的文件中提取可执行文件。
495502
// 支持 .tar.gz、.zip,如果是普通文件则直接返回原路径。
496503
func extractBinary(downloadPath, tempDir string) (string, error) {
504+
return extractBinaryNamed(downloadPath, tempDir, getExecutableName())
505+
}
506+
507+
func extractBinaryNamed(downloadPath, tempDir, executableName string) (string, error) {
508+
if executableName == "" {
509+
return "", fmt.Errorf("可执行文件名不能为空")
510+
}
511+
497512
name := filepath.Base(downloadPath)
498513

499514
// tar.gz 压缩包
@@ -525,21 +540,18 @@ func extractBinary(downloadPath, tempDir string) (string, error) {
525540
continue
526541
}
527542

528-
dstPath := filepath.Join(tempDir, filepath.Base(hdr.Name))
529-
out, err := os.Create(dstPath)
530-
if err != nil {
531-
return "", err
543+
if filepath.Base(hdr.Name) != executableName {
544+
continue
532545
}
533546

534-
if _, err := io.Copy(out, tr); err != nil {
535-
out.Close()
547+
dstPath := filepath.Join(tempDir, executableName)
548+
if err := writeExtractedBinary(dstPath, tr); err != nil {
536549
return "", err
537550
}
538-
out.Close()
539551
return dstPath, nil
540552
}
541553

542-
return "", fmt.Errorf("压缩包中未找到可执行文件")
554+
return "", fmt.Errorf("压缩包中未找到可执行文件: %s", executableName)
543555
}
544556

545557
// zip 压缩包
@@ -555,36 +567,50 @@ func extractBinary(downloadPath, tempDir string) (string, error) {
555567
continue
556568
}
557569

570+
if filepath.Base(f.Name) != executableName {
571+
continue
572+
}
573+
558574
rc, err := f.Open()
559575
if err != nil {
560576
return "", err
561577
}
562578

563-
dstPath := filepath.Join(tempDir, filepath.Base(f.Name))
564-
out, err := os.Create(dstPath)
565-
if err != nil {
579+
dstPath := filepath.Join(tempDir, executableName)
580+
if err := writeExtractedBinary(dstPath, rc); err != nil {
566581
rc.Close()
567582
return "", err
568583
}
569584

570-
if _, err := io.Copy(out, rc); err != nil {
571-
rc.Close()
572-
out.Close()
585+
if err := rc.Close(); err != nil {
573586
return "", err
574587
}
575588

576-
rc.Close()
577-
out.Close()
578589
return dstPath, nil
579590
}
580591

581-
return "", fmt.Errorf("压缩包中未找到可执行文件")
592+
return "", fmt.Errorf("压缩包中未找到可执行文件: %s", executableName)
582593
}
583594

584595
// 普通文件,直接返回
585596
return downloadPath, nil
586597
}
587598

599+
func writeExtractedBinary(dstPath string, src io.Reader) (err error) {
600+
out, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
601+
if err != nil {
602+
return err
603+
}
604+
defer func() {
605+
if closeErr := out.Close(); err == nil {
606+
err = closeErr
607+
}
608+
}()
609+
610+
_, err = io.Copy(out, src)
611+
return err
612+
}
613+
588614
// verifyChecksum 验证文件的 SHA256 校验和
589615
func verifyChecksum(binaryPath, checksumPath, binaryName string) error {
590616
// 读取 checksums.txt

0 commit comments

Comments
 (0)