wallet-core/packages/taler-util/src/talerconfig.ts

834 lines
22 KiB
TypeScript
Raw Normal View History

2020-03-24 12:05:34 +01:00
/*
This file is part of GNU Taler
2023-08-25 11:53:06 +02:00
(C) 2020-2023 Taler Systems S.A.
2020-03-24 12:05:34 +01:00
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Utilities to handle Taler-style configuration files.
*
* @author Florian Dold <dold@taler.net>
*/
/**
* Imports
*/
import { AmountJson } from "./amounts.js";
import { Amounts } from "./amounts.js";
import { Logger } from "./logging.js";
2022-10-31 20:28:28 +01:00
import nodejs_path from "path";
import nodejs_os from "os";
import nodejs_fs from "fs";
2021-08-02 14:11:39 +02:00
const logger = new Logger("talerconfig.ts");
2020-03-24 12:05:34 +01:00
export class ConfigError extends Error {
constructor(message: string) {
super();
Object.setPrototypeOf(this, ConfigError.prototype);
this.name = "ConfigError";
this.message = message;
}
}
2023-08-24 18:29:54 +02:00
enum EntryOrigin {
2023-08-25 11:53:06 +02:00
/**
* From a default file.
*/
DefaultFile = 1,
/**
* From a system/installation specific default value.
*/
DefaultSystem = 2,
/**
* Loaded from file or string
*/
Loaded = 3,
/**
* Changed after loading
*/
Changed = 4,
2023-08-24 18:29:54 +02:00
}
2021-08-02 14:11:39 +02:00
interface Entry {
value: string;
sourceLine: number;
sourceFile: string;
2023-08-24 18:29:54 +02:00
origin: EntryOrigin;
2021-08-02 14:11:39 +02:00
}
interface Section {
secretFilename?: string;
inaccessible: boolean;
entries: { [optionName: string]: Entry };
}
type SectionMap = { [sectionName: string]: Section };
2020-03-24 12:05:34 +01:00
export class ConfigValue<T> {
constructor(
private sectionName: string,
private optionName: string,
private value: string | undefined,
2020-03-24 12:05:34 +01:00
private converter: (x: string) => T,
2023-08-28 16:16:28 +02:00
) { }
2020-03-24 12:05:34 +01:00
required(): T {
2021-08-04 18:05:41 +02:00
if (this.value == undefined) {
2020-03-24 12:05:34 +01:00
throw new ConfigError(
`required option [${this.sectionName}]/${this.optionName} not found`,
);
}
2021-08-02 15:20:00 +02:00
return this.converter(this.value);
2020-03-24 12:05:34 +01:00
}
orUndefined(): T | undefined {
2021-08-02 15:20:00 +02:00
if (this.value !== undefined) {
return this.converter(this.value);
} else {
return undefined;
}
}
orDefault(v: T): T | undefined {
2021-08-02 15:20:00 +02:00
if (this.value !== undefined) {
return this.converter(this.value);
} else {
return v;
}
}
isDefined(): boolean {
2021-08-02 15:20:00 +02:00
return this.value !== undefined;
}
2023-08-28 16:16:28 +02:00
getValue(): string | undefined {
return this.value
}
2020-03-24 12:05:34 +01:00
}
2021-08-02 14:11:39 +02:00
/**
* Expand a path by resolving the tilde syntax for home directories
* and by making relative paths absolute based on the current working directory.
*/
export function expandPath(path: string): string {
if (path[0] === "~") {
2022-10-31 20:28:28 +01:00
path = nodejs_path.join(nodejs_os.homedir(), path.slice(1));
2021-08-02 14:11:39 +02:00
}
if (path[0] !== "/") {
2022-10-31 20:28:28 +01:00
path = nodejs_path.join(process.cwd(), path);
2021-08-02 14:11:39 +02:00
}
return path;
}
/**
* Shell-style path substitution.
2020-08-12 09:11:00 +02:00
*
* Supported patterns:
* "$x" (look up "x")
* "${x}" (look up "x")
* "${x:-y}" (look up "x", fall back to expanded y)
*/
export function pathsub(
x: string,
lookup: (s: string, depth: number) => string | undefined,
recursionDepth = 0,
): string {
if (recursionDepth >= 128) {
throw Error("recursion in path substitution");
}
let s = x;
let l = 0;
while (l < s.length) {
if (s[l] === "$") {
if (s[l + 1] === "{") {
let depth = 1;
const start = l;
let p = start + 2;
let insideNamePart = true;
let hasDefault = false;
for (; p < s.length; p++) {
if (s[p] == "}") {
insideNamePart = false;
depth--;
} else if (s[p] === "$" && s[p + 1] === "{") {
insideNamePart = false;
depth++;
}
if (insideNamePart && s[p] === ":" && s[p + 1] === "-") {
hasDefault = true;
}
if (depth == 0) {
break;
}
}
if (depth == 0) {
const inner = s.slice(start + 2, p);
let varname: string;
let defaultValue: string | undefined;
if (hasDefault) {
[varname, defaultValue] = inner.split(":-", 2);
} else {
varname = inner;
defaultValue = undefined;
}
2023-10-08 23:54:15 +02:00
const r = lookup(varname, depth + 1);
if (r !== undefined) {
2023-08-25 11:53:06 +02:00
s = s.substring(0, start) + r + s.substring(p + 1);
l = start + r.length;
continue;
} else if (defaultValue !== undefined) {
const resolvedDefault = pathsub(defaultValue, lookup, depth + 1);
2023-08-25 11:53:06 +02:00
s = s.substring(0, start) + resolvedDefault + s.substring(p + 1);
l = start + resolvedDefault.length;
continue;
}
}
l = p;
continue;
} else {
const m = /^[a-zA-Z-_][a-zA-Z0-9-_]*/.exec(s.substring(l + 1));
if (m && m[0]) {
const r = lookup(m[0], recursionDepth + 1);
if (r !== undefined) {
2023-08-25 11:53:06 +02:00
s = s.substring(0, l) + r + s.substring(l + 1 + m[0].length);
l = l + r.length;
continue;
}
}
}
}
l++;
}
return s;
}
2021-08-02 14:11:39 +02:00
export interface LoadOptions {
filename?: string;
banDirectives?: boolean;
}
export interface StringifyOptions {
diagnostics?: boolean;
2023-08-24 18:29:54 +02:00
excludeDefaults?: boolean;
2021-08-02 14:11:39 +02:00
}
export interface LoadedFile {
filename: string;
level: number;
}
/**
* Check for a simple wildcard match.
* Only asterisks are allowed.
* Asterisks match everything, including slashes.
*
* @param pattern pattern with wildcards
* @param str string to match against
* @returns true on match, false otherwise
*/
function globMatch(pattern: string, str: string): boolean {
/* Position in the input string */
let strPos = 0;
/* Position in the pattern */
let patPos = 0;
/* Backtrack position in string */
let strBt = -1;
/* Backtrack position in pattern */
let patBt = -1;
2023-08-28 16:16:28 +02:00
for (; ;) {
2021-08-02 14:11:39 +02:00
if (pattern[patPos] === "*") {
strBt = strPos;
patBt = patPos++;
} else if (patPos === pattern.length && strPos === str.length) {
return true;
} else if (pattern[patPos] === str[strPos]) {
strPos++;
patPos++;
} else {
if (patBt < 0) {
return false;
}
strPos = strBt + 1;
if (strPos >= str.length) {
return false;
}
patPos = patBt;
}
}
}
function normalizeInlineFilename(parentFile: string, f: string): string {
if (f[0] === "/") {
return f;
}
2022-10-31 20:28:28 +01:00
const resolvedParentDir = nodejs_path.dirname(
nodejs_fs.realpathSync(parentFile),
2021-08-02 14:11:39 +02:00
);
2022-10-31 20:28:28 +01:00
return nodejs_path.join(resolvedParentDir, f);
2021-08-02 14:11:39 +02:00
}
/**
* Crude implementation of the which(1) shell command.
2022-09-21 20:43:35 +02:00
*
* Tries to locate the location of an executable based on the
* "PATH" environment variable.
*/
function which(name: string): string | undefined {
const paths = process.env["PATH"]?.split(":");
if (!paths) {
return undefined;
}
for (const path of paths) {
2022-10-31 20:28:28 +01:00
const filename = nodejs_path.join(path, name);
if (nodejs_fs.existsSync(filename)) {
return filename;
}
}
return undefined;
}
2020-03-24 12:05:34 +01:00
export class Configuration {
private sectionMap: SectionMap = {};
2021-08-02 14:11:39 +02:00
private hintEntrypoint: string | undefined;
private loadedFiles: LoadedFile[] = [];
private nestLevel = 0;
2023-08-24 18:29:54 +02:00
private loadFromFilename(
filename: string,
isDefaultSource: boolean,
opts: LoadOptions = {},
): void {
2021-08-02 14:11:39 +02:00
filename = expandPath(filename);
const checkCycle = () => {
let level = this.nestLevel;
const fns = [...this.loadedFiles].reverse();
for (const lf of fns) {
if (lf.level >= level) {
continue;
}
level = lf.level;
if (lf.filename === filename) {
throw Error(`cyclic inline ${lf.filename} -> ${filename}`);
}
}
};
checkCycle();
2022-10-31 20:28:28 +01:00
const s = nodejs_fs.readFileSync(filename, "utf-8");
2021-08-02 14:11:39 +02:00
this.loadedFiles.push({
filename: filename,
level: this.nestLevel,
});
const oldNestLevel = this.nestLevel;
this.nestLevel += 1;
try {
2023-08-24 18:29:54 +02:00
this.internalLoadFromString(s, isDefaultSource, {
2021-08-02 14:11:39 +02:00
...opts,
filename: filename,
});
} finally {
this.nestLevel = oldNestLevel;
}
}
2023-08-24 18:29:54 +02:00
private loadGlob(
parentFilename: string,
isDefaultSource: boolean,
fileglob: string,
): void {
2022-10-31 20:28:28 +01:00
const resolvedParent = nodejs_fs.realpathSync(parentFilename);
const parentDir = nodejs_path.dirname(resolvedParent);
2021-08-02 14:11:39 +02:00
let fullFileglob: string;
if (fileglob.startsWith("/")) {
fullFileglob = fileglob;
} else {
2022-10-31 20:28:28 +01:00
fullFileglob = nodejs_path.join(parentDir, fileglob);
2021-08-02 14:11:39 +02:00
}
fullFileglob = expandPath(fullFileglob);
2022-10-31 20:28:28 +01:00
const head = nodejs_path.dirname(fullFileglob);
const tail = nodejs_path.basename(fullFileglob);
2021-08-02 14:11:39 +02:00
2022-10-31 20:28:28 +01:00
const files = nodejs_fs.readdirSync(head);
2021-08-02 14:11:39 +02:00
for (const f of files) {
if (globMatch(tail, f)) {
2022-10-31 20:28:28 +01:00
const fullPath = nodejs_path.join(head, f);
2023-08-24 18:29:54 +02:00
this.loadFromFilename(fullPath, isDefaultSource);
2021-08-02 14:11:39 +02:00
}
}
}
2023-08-24 18:29:54 +02:00
private loadSecret(
sectionName: string,
filename: string,
isDefaultSource: boolean,
): void {
2021-08-02 14:11:39 +02:00
const sec = this.provideSection(sectionName);
sec.secretFilename = filename;
const otherCfg = new Configuration();
try {
2022-10-31 20:28:28 +01:00
nodejs_fs.accessSync(filename, nodejs_fs.constants.R_OK);
2021-08-02 14:11:39 +02:00
} catch (err) {
sec.inaccessible = true;
return;
}
2023-08-24 18:29:54 +02:00
otherCfg.loadFromFilename(filename, isDefaultSource, {
2021-08-02 14:11:39 +02:00
banDirectives: true,
});
const otherSec = otherCfg.provideSection(sectionName);
for (const opt of Object.keys(otherSec.entries)) {
this.setString(sectionName, opt, otherSec.entries[opt].value);
}
}
2023-08-24 18:29:54 +02:00
private internalLoadFromString(
s: string,
isDefaultSource: boolean,
opts: LoadOptions = {},
): void {
2021-08-02 14:11:39 +02:00
let lineNo = 0;
const fn = opts.filename ?? "<input>";
2020-03-24 12:05:34 +01:00
const reComment = /^\s*#.*$/;
const reSection = /^\s*\[\s*([^\]]*)\s*\]\s*$/;
const reParam = /^\s*([^=]+?)\s*=\s*(.*?)\s*$/;
2021-08-02 14:11:39 +02:00
const reDirective = /^\s*@([a-zA-Z-_]+)@\s*(.*?)\s*$/;
2020-03-24 12:05:34 +01:00
const reEmptyLine = /^\s*$/;
let currentSection: string | undefined = undefined;
const lines = s.split("\n");
for (const line of lines) {
2021-08-02 14:11:39 +02:00
lineNo++;
2020-03-24 12:05:34 +01:00
if (reEmptyLine.test(line)) {
continue;
}
if (reComment.test(line)) {
continue;
}
2021-08-02 14:11:39 +02:00
const directiveMatch = line.match(reDirective);
if (directiveMatch) {
if (opts.banDirectives) {
throw Error(
`invalid configuration, directive in ${fn}:${lineNo} forbidden`,
);
}
const directive = directiveMatch[1].toLowerCase();
switch (directive) {
case "inline": {
if (!opts.filename) {
throw Error(
`invalid configuration, @inline-matching@ directive in ${fn}:${lineNo} can only be used from a file`,
);
}
const arg = directiveMatch[2].trim();
2023-08-24 18:29:54 +02:00
this.loadFromFilename(
normalizeInlineFilename(opts.filename, arg),
isDefaultSource,
);
2021-08-02 14:11:39 +02:00
break;
}
case "inline-secret": {
if (!opts.filename) {
throw Error(
`invalid configuration, @inline-matching@ directive in ${fn}:${lineNo} can only be used from a file`,
);
}
const arg = directiveMatch[2].trim();
const sp = arg.split(" ").map((x) => x.trim());
if (sp.length != 2) {
throw Error(
`invalid configuration, @inline-secret@ directive in ${fn}:${lineNo} requires two arguments`,
);
}
const secretFilename = normalizeInlineFilename(
opts.filename,
sp[1],
);
2023-08-24 18:29:54 +02:00
this.loadSecret(sp[0], secretFilename, isDefaultSource);
2021-08-02 14:11:39 +02:00
break;
}
case "inline-matching": {
const arg = directiveMatch[2].trim();
if (!opts.filename) {
throw Error(
`invalid configuration, @inline-matching@ directive in ${fn}:${lineNo} can only be used from a file`,
);
}
2023-08-24 18:29:54 +02:00
this.loadGlob(opts.filename, isDefaultSource, arg);
2021-08-02 14:11:39 +02:00
break;
}
default:
throw Error(
`invalid configuration, unsupported directive in ${fn}:${lineNo}`,
);
}
continue;
}
2020-03-24 12:05:34 +01:00
const secMatch = line.match(reSection);
if (secMatch) {
currentSection = secMatch[1];
continue;
}
if (currentSection === undefined) {
2021-08-02 14:11:39 +02:00
throw Error(
`invalid configuration, expected section header in ${fn}:${lineNo}`,
);
2020-03-24 12:05:34 +01:00
}
currentSection = currentSection.toUpperCase();
2020-03-24 12:05:34 +01:00
const paramMatch = line.match(reParam);
if (paramMatch) {
const optName = paramMatch[1].toUpperCase();
2020-03-24 12:05:34 +01:00
let val = paramMatch[2];
if (val.startsWith('"') && val.endsWith('"')) {
val = val.slice(1, val.length - 1);
}
2021-08-02 14:11:39 +02:00
const sec = this.provideSection(currentSection);
sec.entries[optName] = {
value: val,
sourceFile: opts.filename ?? "<unknown>",
sourceLine: lineNo,
2023-08-25 11:53:06 +02:00
origin: isDefaultSource
? EntryOrigin.DefaultFile
: EntryOrigin.Loaded,
2021-08-02 14:11:39 +02:00
};
2020-03-24 12:05:34 +01:00
continue;
}
throw Error(
2021-08-02 14:11:39 +02:00
`invalid configuration, expected section header, option assignment or directive in ${fn}:${lineNo}`,
2020-03-24 12:05:34 +01:00
);
}
}
2020-03-24 12:05:34 +01:00
2023-08-24 18:29:54 +02:00
loadFromString(s: string, opts: LoadOptions = {}): void {
return this.internalLoadFromString(s, false, opts);
}
2021-08-02 14:11:39 +02:00
private provideSection(section: string): Section {
const secNorm = section.toUpperCase();
if (this.sectionMap[secNorm]) {
return this.sectionMap[secNorm];
}
const newSec: Section = {
entries: {},
inaccessible: false,
};
this.sectionMap[secNorm] = newSec;
return newSec;
}
private findEntry(section: string, option: string): Entry | undefined {
const secNorm = section.toUpperCase();
2021-08-02 14:11:39 +02:00
const optNorm = option.toUpperCase();
return this.sectionMap[secNorm]?.entries[optNorm];
}
setString(section: string, option: string, value: string): void {
const sec = this.provideSection(section);
sec.entries[option.toUpperCase()] = {
value,
sourceLine: 0,
sourceFile: "<unknown>",
2023-08-24 18:29:54 +02:00
origin: EntryOrigin.Changed,
2021-08-02 14:11:39 +02:00
};
2020-03-24 12:05:34 +01:00
}
2023-08-25 11:53:06 +02:00
/**
* Set a string value to a value from default locations.
*/
private setStringSystemDefault(
section: string,
option: string,
value: string,
): void {
const sec = this.provideSection(section);
sec.entries[option.toUpperCase()] = {
value,
sourceLine: 0,
sourceFile: "<unknown>",
origin: EntryOrigin.DefaultSystem,
};
}
/**
2021-08-05 22:43:14 +02:00
* Get upper-cased section names.
*/
getSectionNames(): string[] {
2021-08-04 23:16:08 +02:00
return Object.keys(this.sectionMap).map((x) => x.toUpperCase());
}
2020-03-24 12:05:34 +01:00
getString(section: string, option: string): ConfigValue<string> {
const secNorm = section.toUpperCase();
const optNorm = option.toUpperCase();
2021-08-02 14:11:39 +02:00
const val = this.findEntry(secNorm, optNorm)?.value;
return new ConfigValue(secNorm, optNorm, val, (x) => x);
}
getPath(section: string, option: string): ConfigValue<string> {
const secNorm = section.toUpperCase();
const optNorm = option.toUpperCase();
2021-08-02 14:11:39 +02:00
const val = this.findEntry(secNorm, optNorm)?.value;
return new ConfigValue(secNorm, optNorm, val, (x) =>
pathsub(x, (v, d) => this.lookupVariable(v, d + 1)),
);
}
getYesNo(section: string, option: string): ConfigValue<boolean> {
const secNorm = section.toUpperCase();
const optNorm = option.toUpperCase();
2021-08-02 14:11:39 +02:00
const val = this.findEntry(secNorm, optNorm)?.value;
const convert = (x: string): boolean => {
x = x.toLowerCase();
if (x === "yes") {
return true;
} else if (x === "no") {
return false;
}
2020-08-12 09:11:00 +02:00
throw Error(
`invalid config value for [${secNorm}]/${optNorm}, expected yes/no`,
);
};
return new ConfigValue(secNorm, optNorm, val, convert);
}
getNumber(section: string, option: string): ConfigValue<number> {
const secNorm = section.toUpperCase();
const optNorm = option.toUpperCase();
2021-08-02 14:11:39 +02:00
const val = this.findEntry(secNorm, optNorm)?.value;
const convert = (x: string): number => {
try {
return Number.parseInt(x, 10);
} catch (e) {
2020-08-12 09:11:00 +02:00
throw Error(
`invalid config value for [${secNorm}]/${optNorm}, expected number`,
);
}
};
return new ConfigValue(secNorm, optNorm, val, convert);
}
lookupVariable(x: string, depth: number = 0): string | undefined {
// We loop up options in PATHS in upper case, as option names
// are case insensitive
const val = this.findEntry("PATHS", x)?.value;
if (val !== undefined) {
return pathsub(val, (v, d) => this.lookupVariable(v, d), depth);
}
// Environment variables can be case sensitive, respect that.
const envVal = process.env[x];
if (envVal !== undefined) {
return envVal;
}
logger.warn(`unable to resolve variable '${x}'`);
return;
2020-03-24 12:05:34 +01:00
}
getAmount(section: string, option: string): ConfigValue<AmountJson> {
const val = this.findEntry(section, option)?.value;
return new ConfigValue(section, option, val, (x) =>
2020-03-30 12:39:32 +02:00
Amounts.parseOrThrow(x),
);
2020-03-24 12:05:34 +01:00
}
2023-08-24 18:29:54 +02:00
loadDefaultsFromDir(dirname: string): void {
2022-10-31 20:28:28 +01:00
const files = nodejs_fs.readdirSync(dirname);
2021-08-02 14:11:39 +02:00
for (const f of files) {
2022-10-31 20:28:28 +01:00
const fn = nodejs_path.join(dirname, f);
2023-08-24 18:29:54 +02:00
this.loadFromFilename(fn, true);
2021-08-02 14:11:39 +02:00
}
}
private loadDefaults(): void {
2023-08-25 11:53:06 +02:00
let baseConfigDir = process.env["TALER_BASE_CONFIG"];
if (!baseConfigDir) {
/* Try to locate the configuration based on the location
* of the taler-config binary. */
const path = which("taler-config");
if (path) {
2023-08-25 11:53:06 +02:00
baseConfigDir = nodejs_fs.realpathSync(
2022-10-31 20:28:28 +01:00
nodejs_path.dirname(path) + "/../share/taler/config.d",
);
}
}
2023-08-25 11:53:06 +02:00
if (!baseConfigDir) {
baseConfigDir = "/usr/share/taler/config.d";
}
let installPrefix = process.env["TALER_PREFIX"];
if (!installPrefix) {
/* Try to locate install path based on the location
* of the taler-config binary. */
const path = which("taler-config");
if (path) {
installPrefix = nodejs_fs.realpathSync(
nodejs_path.dirname(path) + "/..",
);
}
}
if (!installPrefix) {
installPrefix = "/usr";
2021-08-02 14:11:39 +02:00
}
2023-08-25 11:53:06 +02:00
this.setStringSystemDefault(
"PATHS",
"LIBEXECDIR",
`${installPrefix}/taler/libexec/`,
);
this.setStringSystemDefault(
"PATHS",
"DOCDIR",
`${installPrefix}/share/doc/taler/`,
);
this.setStringSystemDefault(
"PATHS",
"ICONDIR",
`${installPrefix}/share/icons/`,
);
this.setStringSystemDefault(
"PATHS",
"LOCALEDIR",
`${installPrefix}/share/locale/`,
);
2023-08-25 11:53:06 +02:00
this.setStringSystemDefault("PATHS", "PREFIX", `${installPrefix}/`);
this.setStringSystemDefault("PATHS", "BINDIR", `${installPrefix}/bin`);
this.setStringSystemDefault(
"PATHS",
"LIBDIR",
`${installPrefix}/lib/taler/`,
);
this.setStringSystemDefault(
"PATHS",
"DATADIR",
`${installPrefix}/share/taler/`,
);
2023-08-25 11:53:06 +02:00
this.loadDefaultsFromDir(baseConfigDir);
2021-08-02 14:11:39 +02:00
}
getDefaultConfigFilename(): string | undefined {
const xdg = process.env["XDG_CONFIG_HOME"];
const home = process.env["HOME"];
let fn: string | undefined;
if (xdg) {
2022-10-31 20:28:28 +01:00
fn = nodejs_path.join(xdg, "taler.conf");
2021-08-02 14:11:39 +02:00
} else if (home) {
2022-10-31 20:28:28 +01:00
fn = nodejs_path.join(home, ".config/taler.conf");
2021-08-02 14:11:39 +02:00
}
2022-10-31 20:28:28 +01:00
if (fn && nodejs_fs.existsSync(fn)) {
2021-08-02 14:11:39 +02:00
return fn;
}
const etc1 = "/etc/taler.conf";
2022-10-31 20:28:28 +01:00
if (nodejs_fs.existsSync(etc1)) {
2021-08-02 14:11:39 +02:00
return etc1;
}
const etc2 = "/etc/taler/taler.conf";
2022-10-31 20:28:28 +01:00
if (nodejs_fs.existsSync(etc2)) {
2021-08-02 14:11:39 +02:00
return etc2;
}
return undefined;
}
static load(filename?: string): Configuration {
const cfg = new Configuration();
2021-08-02 14:11:39 +02:00
cfg.loadDefaults();
if (filename) {
2023-08-24 18:29:54 +02:00
cfg.loadFromFilename(filename, false);
2021-08-02 14:11:39 +02:00
} else {
const fn = cfg.getDefaultConfigFilename();
if (fn) {
2023-08-24 18:29:54 +02:00
// It's the default filename for the main config file,
// but we don't consider the values default values.
cfg.loadFromFilename(fn, false);
2021-08-02 14:11:39 +02:00
}
}
cfg.hintEntrypoint = filename;
return cfg;
}
2021-08-02 14:11:39 +02:00
stringify(opts: StringifyOptions = {}): string {
let s = "";
2021-08-02 14:11:39 +02:00
if (opts.diagnostics) {
s += "# Configuration file diagnostics\n";
s += "#\n";
s += `# Entry point: ${this.hintEntrypoint ?? "<none>"}\n`;
s += "#\n";
s += "# Loaded files:\n";
for (const f of this.loadedFiles) {
s += `# ${f.filename}\n`;
}
s += "#\n\n";
}
for (const sectionName of Object.keys(this.sectionMap)) {
2021-08-02 14:11:39 +02:00
const sec = this.sectionMap[sectionName];
2023-08-24 18:29:54 +02:00
let headerWritten = false;
2021-08-02 14:11:39 +02:00
for (const optionName of Object.keys(sec.entries)) {
const entry = this.sectionMap[sectionName].entries[optionName];
2023-08-25 11:53:06 +02:00
if (
opts.excludeDefaults &&
(entry.origin === EntryOrigin.DefaultSystem ||
entry.origin === EntryOrigin.DefaultFile)
) {
2023-08-24 18:29:54 +02:00
continue;
}
if (!headerWritten) {
if (opts.diagnostics && sec.secretFilename) {
s += `# Secret section from ${sec.secretFilename}\n`;
s += `# Secret accessible: ${!sec.inaccessible}\n`;
}
s += `[${sectionName}]\n`;
headerWritten = true;
}
2021-08-02 14:11:39 +02:00
if (entry !== undefined) {
if (opts.diagnostics) {
2023-08-25 11:53:06 +02:00
switch (entry.origin) {
case EntryOrigin.DefaultFile:
case EntryOrigin.Changed:
case EntryOrigin.Loaded:
s += `# ${entry.sourceFile}:${entry.sourceLine}\n`;
break;
case EntryOrigin.DefaultSystem:
s += `# (system/installation default)\n`;
break;
}
2021-08-02 14:11:39 +02:00
}
s += `${optionName} = ${entry.value}\n`;
}
}
2023-08-24 18:29:54 +02:00
if (headerWritten) {
s += "\n";
}
}
2021-08-02 14:11:39 +02:00
return s;
}
2023-08-24 18:29:54 +02:00
write(filename: string, opts: { excludeDefaults?: boolean } = {}): void {
nodejs_fs.writeFileSync(
filename,
this.stringify({ excludeDefaults: opts.excludeDefaults }),
);
}
2020-03-24 12:05:34 +01:00
}