From 43e7c53dc0fac063fb9318d895e42f33f52d94d9 Mon Sep 17 00:00:00 2001 From: Germano Rizzo <5470600+proofrock@users.noreply.github.com> Date: Tue, 18 Feb 2025 17:25:16 +0100 Subject: [PATCH] :art: Set the workspace path and lang via environment variable (#14148) --- README.md | 1 + README_ja_JP.md | 1 + README_zh_CN.md | 1 + kernel/util/working.go | 29 +++++++++++++++++++++++++---- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5e9f43652..4cd5ee273 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,7 @@ docker run -d \ * `PGID`: Custom group ID (optional, defaults to `1000` if not provided) * `workspace_dir_host`: The workspace folder path on the host * `workspace_dir_container`: The path of the workspace folder in the container, as specified in `--workspace` + * In alternative, it's possible to set the auth code via the `SIYUAN_WORKSPACE_PATH` env variable. The commandline will always have the priority, if both are set. * `accessAuthCode`: Access authorization code (please **be sure to modify**, otherwise anyone can access your data) * In alternative, it's possible to set the auth code via the `SIYUAN_ACCESS_AUTH_CODE` env variable. The commandline will always have the priority, if both are set. diff --git a/README_ja_JP.md b/README_ja_JP.md index 3aa0789a3..57b5cecbf 100644 --- a/README_ja_JP.md +++ b/README_ja_JP.md @@ -206,6 +206,7 @@ docker run -d \ * `PGID`: カスタムグループID(オプション、指定しない場合はデフォルトで `1000`) * `workspace_dir_host`: ホスト上のワークスペースフォルダーのパス * `workspace_dir_container`: コンテナ内のワークスペースフォルダーのパス、`--workspace` で指定されたものと同じ + * また、`SIYUAN_WORKSPACE_PATH` 環境変数を設定することで認証コードを指定することもできます。両方が設定されている場合、コマンドラインの値が優先されます。 * `accessAuthCode`: アクセス認証コード(**必ず変更してください**、そうしないと誰でもデータにアクセスできます) * また、`SIYUAN_ACCESS_AUTH_CODE` 環境変数を設定することで認証コードを指定することもできます。両方が設定されている場合、コマンドラインの値が優先されます。 diff --git a/README_zh_CN.md b/README_zh_CN.md index 6b168ef49..40b96c7d0 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -211,6 +211,7 @@ docker run -d \ * `PGID`: 自定义组 ID(可选,如果未提供,默认为 `1000`) * `workspace_dir_host`:宿主机上的工作空间文件夹路径 * `workspace_dir_container`:容器内工作空间文件夹路径,和后面 `--workspace` 指定成一样的 + * 另外,也可以通过 `SIYUAN_WORKSPACE_PATH` 环境变量设置授权码。如果两者都设置了,命令行的值将优先。 * `accessAuthCode`:访问授权码,请**务必修改**,否则任何人都可以读写你的数据 * 另外,也可以通过 `SIYUAN_ACCESS_AUTH_CODE` 环境变量设置授权码。如果两者都设置了,命令行的值将优先。 diff --git a/kernel/util/working.go b/kernel/util/working.go index 9af60b7c2..881207692 100644 --- a/kernel/util/working.go +++ b/kernel/util/working.go @@ -47,6 +47,11 @@ var Mode = "prod" const ( Ver = "3.1.22" IsInsider = false + + // env vars as fallback for commandline parameters + SIYUAN_ACCESS_AUTH_CODE = "SIYUAN_ACCESS_AUTH_CODE" + SIYUAN_WORKSPACE = "SIYUAN_WORKSPACE_PATH" + SIYUAN_LANG = "SIYUAN_LANG" ) var ( @@ -70,6 +75,19 @@ var ( HttpServing = false // 是否 HTTP 伺服已经可用 ) +// If a commandline parameter is empty, fallback to the env var. +// +// "empty" means the parameter is not set or set to an empty string. +// It returns a pointer to string, to be a drop-in replacement for +// the commandline parameter itself. +func coalesceToEnvVar(fromCLI *string, envVarName string) *string { + if fromCLI == nil || "" == *fromCLI { + ret := os.Getenv(envVarName) + return &ret + } + return fromCLI +} + func Boot() { initEnvVars() IncBootProgress(3, "Booting kernel...") @@ -87,6 +105,13 @@ func Boot() { mode := flag.String("mode", "prod", "dev/prod") flag.Parse() + // Fallback to env vars if commandline args are not set + // valid only for CLI args that default to "", as the + // others have explicit (sane) defaults + workspacePath = coalesceToEnvVar(workspacePath, SIYUAN_WORKSPACE) + accessAuthCode = coalesceToEnvVar(accessAuthCode, SIYUAN_ACCESS_AUTH_CODE) + lang = coalesceToEnvVar(lang, SIYUAN_LANG) + if "" != *wdPath { WorkingDir = *wdPath } @@ -102,10 +127,6 @@ func Boot() { Container = ContainerStd if RunInContainer { Container = ContainerDocker - if "" == AccessAuthCode { - // Priority to commandline; if not set, look into env var - AccessAuthCode = SiyuanAccessAuthCodeViaEnvvar - } if "" == AccessAuthCode { // Still empty? interruptBoot := true