-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# VanBlog CLI | ||
|
||
这个包存放了一些命令行工具。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "vanblog-cli", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"mongodb": "^5.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env node | ||
|
||
const {MongoClient} = require('mongodb'); | ||
|
||
|
||
const uri = "mongodb://mongo:27017/vanBlog?authSource=admin"; | ||
|
||
const readString = (prompt) => { | ||
process.stdout.write(prompt); | ||
return new Promise((resolve, reject) => { | ||
process.stdin.once('data', (data) => { | ||
resolve(data.toString().trim()); | ||
}); | ||
}); | ||
} | ||
|
||
const parseDBfromURI = (uri) => { | ||
const obj = new URL(uri); | ||
return obj.pathname.slice(1); | ||
|
||
} | ||
|
||
|
||
const main = async () => { | ||
const uriFromUser = await readString('输入 MongoDB 连接 URL(如果看不懂或者使用的默认配置,请直接按回车): \n '); | ||
const uriToUse = uriFromUser || uri; | ||
const db = parseDBfromURI(uriToUse) | ||
console.log("使用的 MongoDB 连接 URL: ", uriToUse, "数据库:", db); | ||
|
||
const client = new MongoClient(uriToUse); | ||
console.log("尝试连接数据库...") | ||
try { | ||
await tryConnectDB(client); | ||
console.log("连接数据库成功") | ||
}catch(err) { | ||
console.log("连接数据库失败:", err); | ||
process.exit(1); | ||
} | ||
await resetHttps(client,db); | ||
await client.close(); | ||
process.exit(0); | ||
}; | ||
|
||
const tryConnectDB = (client) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(()=>{ | ||
reject(new Error("连接数据库超时")) | ||
},5000) | ||
client.connect().then(resolve).catch(err => reject(err)) | ||
}) | ||
} | ||
|
||
const resetHttps = async (client,dbName) => { | ||
try { | ||
await client.connect(); | ||
|
||
const db = client.db(dbName); | ||
const col = db.collection("settings"); | ||
const result = await col.deleteOne({type:"https"}) | ||
console.log("删除 HTTPS 设置成功,删除的条目数:", result.deletedCount) | ||
console.log("重启 vanblog 后生效") | ||
}catch (err) { | ||
console.log("重制 HTTPS 出错:", err); | ||
} | ||
} | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters