feat(config): verify DB connection before saving set-db
Some checks failed
Secrets CLI - Build & Release / 版本 & Release (push) Successful in 2s
Secrets CLI - Build & Release / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Secrets CLI - Build & Release / Build (aarch64-apple-darwin) (push) Has been cancelled
Secrets CLI - Build & Release / Build (x86_64-pc-windows-msvc) (push) Has been cancelled
Secrets CLI - Build & Release / 发布草稿 Release (push) Has been cancelled
Secrets CLI - Build & Release / 质量检查 (fmt / clippy / test) (push) Has been cancelled
Some checks failed
Secrets CLI - Build & Release / 版本 & Release (push) Successful in 2s
Secrets CLI - Build & Release / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Secrets CLI - Build & Release / Build (aarch64-apple-darwin) (push) Has been cancelled
Secrets CLI - Build & Release / Build (x86_64-pc-windows-msvc) (push) Has been cancelled
Secrets CLI - Build & Release / 发布草稿 Release (push) Has been cancelled
Secrets CLI - Build & Release / 质量检查 (fmt / clippy / test) (push) Has been cancelled
- Check connection with create_pool before writing to config - Show 'Database connection failed' on error, do not overwrite config - Update AGENTS.md and README.md Made-with: Cursor
This commit is contained in:
@@ -115,6 +115,8 @@ secrets config show # 查看当前配置(密码脱敏)
|
|||||||
secrets config path # 打印配置文件路径
|
secrets config path # 打印配置文件路径
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`set-db` 会先验证连接可用,成功后才写入配置文件;连接失败时提示 "Database connection failed" 且不修改配置。
|
||||||
|
|
||||||
配置文件:`~/.config/secrets/config.toml`,权限 0600。`--db-url` 参数可一次性覆盖。
|
配置文件:`~/.config/secrets/config.toml`,权限 0600。`--db-url` 参数可一次性覆盖。
|
||||||
|
|
||||||
## 主密钥与加密
|
## 主密钥与加密
|
||||||
@@ -406,7 +408,7 @@ secrets run -n refining --kind service --name gitea -- printenv
|
|||||||
### config — 配置管理(无需主密钥)
|
### config — 配置管理(无需主密钥)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 设置数据库连接(每台设备执行一次,之后永久生效)
|
# 设置数据库连接(每台设备执行一次,之后永久生效;先验证连接可用再写入)
|
||||||
secrets config set-db "postgres://postgres:<password>@<host>:<port>/secrets"
|
secrets config set-db "postgres://postgres:<password>@<host>:<port>/secrets"
|
||||||
|
|
||||||
# 查看当前配置(密码脱敏)
|
# 查看当前配置(密码脱敏)
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ cargo build --release
|
|||||||
## 首次使用(每台设备各执行一次)
|
## 首次使用(每台设备各执行一次)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 1. 配置数据库连接
|
# 1. 配置数据库连接(会先验证连接可用再写入)
|
||||||
secrets config set-db "postgres://postgres:<password>@<host>:<port>/secrets"
|
secrets config set-db "postgres://postgres:<password>@<host>:<port>/secrets"
|
||||||
|
|
||||||
# 2. 初始化主密钥(提示输入主密码,派生后存入 OS 钥匙串)
|
# 2. 初始化主密钥(提示输入主密码,派生后存入 OS 钥匙串)
|
||||||
@@ -131,7 +131,7 @@ secrets delete -n refining --kind service --name legacy-mqtt
|
|||||||
secrets init # 主密钥初始化(每台设备一次,主密码派生后存钥匙串)
|
secrets init # 主密钥初始化(每台设备一次,主密码派生后存钥匙串)
|
||||||
|
|
||||||
# ── config ───────────────────────────────────────────────────────────────────
|
# ── config ───────────────────────────────────────────────────────────────────
|
||||||
secrets config set-db "postgres://postgres:<password>@<host>:<port>/secrets"
|
secrets config set-db "postgres://postgres:<password>@<host>:<port>/secrets" # 先验证再写入
|
||||||
secrets config show # 密码脱敏展示
|
secrets config show # 密码脱敏展示
|
||||||
secrets config path # 打印配置文件路径
|
secrets config path # 打印配置文件路径
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,12 @@ use anyhow::Result;
|
|||||||
pub async fn run(action: crate::ConfigAction) -> Result<()> {
|
pub async fn run(action: crate::ConfigAction) -> Result<()> {
|
||||||
match action {
|
match action {
|
||||||
crate::ConfigAction::SetDb { url } => {
|
crate::ConfigAction::SetDb { url } => {
|
||||||
|
// Verify connection before writing config
|
||||||
|
let pool = crate::db::create_pool(&url)
|
||||||
|
.await
|
||||||
|
.map_err(|e| anyhow::anyhow!("Database connection failed: {}", e))?;
|
||||||
|
drop(pool);
|
||||||
|
|
||||||
let cfg = Config {
|
let cfg = Config {
|
||||||
database_url: Some(url.clone()),
|
database_url: Some(url.clone()),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user