status-desktop/src/constants.nim

66 lines
2.0 KiB
Nim

import os, sequtils, strutils
import # vendor libs
confutils
const sep = when defined(windows): "\\" else: "/"
proc defaultDataDir(): string =
let homeDir = getHomeDir()
let parentDir =
if defined(development):
parentDir(getAppDir())
elif homeDir == "":
getCurrentDir()
elif defined(macosx):
joinPath(homeDir, "Library", "Application Support")
elif defined(windows):
let targetDir = getEnv("LOCALAPPDATA").string
if targetDir == "":
joinPath(homeDir, "AppData", "Local")
else:
targetDir
else:
let targetDir = getEnv("XDG_CONFIG_HOME").string
if targetDir == "":
joinPath(homeDir, ".config")
else:
targetDir
absolutePath(joinPath(parentDir, "Status"))
type StatusDesktopConfig = object
dataDir* {.
defaultValue: defaultDataDir()
desc: "Status Desktop data directory"
abbr: "d" .}: string
uri* {.
defaultValue: ""
desc: "status-im:// URI to open a chat or other"
name: "uri" .}: string
# On macOS the first time when a user gets the "App downloaded from the
# internet" warning, and clicks the Open button, the OS passes a unique process
# serial number (PSN) as -psn_... command-line argument, which we remove before
# processing the arguments with nim-confutils.
# Credit: https://github.com/bitcoin/bitcoin/blame/b6e34afe9735faf97d6be7a90fafd33ec18c0cbb/src/util/system.cpp#L383-L389
var cliParams = commandLineParams()
if defined(macosx):
cliParams.keepIf(proc(p: string): bool = not p.startsWith("-psn_"))
let desktopConfig = StatusDesktopConfig.load(cliParams)
let
baseDir = absolutePath(expandTilde(desktopConfig.dataDir))
OPENURI* = desktopConfig.uri
DATADIR* = baseDir & sep
STATUSGODIR* = joinPath(baseDir, "data") & sep
KEYSTOREDIR* = joinPath(baseDir, "data", "keystore") & sep
TMPDIR* = joinPath(baseDir, "tmp") & sep
LOGDIR* = joinPath(baseDir, "logs") & sep
proc ensureDirectories*(dataDir, tmpDir, logDir: string) =
createDir(dataDir)
createDir(tmpDir)
createDir(logDir)