taler-util: set [PATH] defaults
This commit is contained in:
parent
3d6cff9c84
commit
896841aec5
@ -170,6 +170,39 @@ const sandcastleCli = testingCli.subcommand("sandcastleArgs", "sandcastle", {
|
|||||||
help: "Subcommands for handling GNU Taler sandcastle deployments.",
|
help: "Subcommands for handling GNU Taler sandcastle deployments.",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const configCli = testingCli.subcommand("configArgs", "config", {
|
||||||
|
help: "Subcommands for handling the Taler configuration.",
|
||||||
|
});
|
||||||
|
|
||||||
|
configCli.subcommand("show", "show").action(async (args) => {
|
||||||
|
const config = Configuration.load();
|
||||||
|
const cfgStr = config.stringify({
|
||||||
|
diagnostics: true,
|
||||||
|
});
|
||||||
|
console.log(cfgStr);
|
||||||
|
});
|
||||||
|
|
||||||
|
configCli
|
||||||
|
.subcommand("get", "get")
|
||||||
|
.requiredArgument("section", clk.STRING)
|
||||||
|
.requiredArgument("option", clk.STRING)
|
||||||
|
.flag("file", ["-f"])
|
||||||
|
.action(async (args) => {
|
||||||
|
const config = Configuration.load();
|
||||||
|
let res;
|
||||||
|
if (args.get.file) {
|
||||||
|
res = config.getString(args.get.section, args.get.option);
|
||||||
|
} else {
|
||||||
|
res = config.getPath(args.get.section, args.get.option);
|
||||||
|
}
|
||||||
|
if (res.isDefined()) {
|
||||||
|
console.log(res.value);
|
||||||
|
} else {
|
||||||
|
console.warn("not found");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const deploymentCli = testingCli.subcommand("deploymentArgs", "deployment", {
|
const deploymentCli = testingCli.subcommand("deploymentArgs", "deployment", {
|
||||||
help: "Subcommands for handling GNU Taler deployments.",
|
help: "Subcommands for handling GNU Taler deployments.",
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
This file is part of GNU Taler
|
This file is part of GNU Taler
|
||||||
(C) 2020 Taler Systems S.A.
|
(C) 2020-2023 Taler Systems S.A.
|
||||||
|
|
||||||
GNU Taler is free software; you can redistribute it and/or modify it under the
|
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
|
terms of the GNU General Public License as published by the Free Software
|
||||||
@ -40,12 +40,22 @@ export class ConfigError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum EntryOrigin {
|
enum EntryOrigin {
|
||||||
// From a default file
|
/**
|
||||||
Default = 1,
|
* From a default file.
|
||||||
// Loaded from file or string
|
*/
|
||||||
Loaded = 2,
|
DefaultFile = 1,
|
||||||
// Changed after loading
|
/**
|
||||||
Changed = 3,
|
* From a system/installation specific default value.
|
||||||
|
*/
|
||||||
|
DefaultSystem = 2,
|
||||||
|
/**
|
||||||
|
* Loaded from file or string
|
||||||
|
*/
|
||||||
|
Loaded = 3,
|
||||||
|
/**
|
||||||
|
* Changed after loading
|
||||||
|
*/
|
||||||
|
Changed = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Entry {
|
interface Entry {
|
||||||
@ -169,12 +179,12 @@ export function pathsub(
|
|||||||
|
|
||||||
const r = lookup(inner, depth + 1);
|
const r = lookup(inner, depth + 1);
|
||||||
if (r !== undefined) {
|
if (r !== undefined) {
|
||||||
s = s.substr(0, start) + r + s.substr(p + 1);
|
s = s.substring(0, start) + r + s.substring(p + 1);
|
||||||
l = start + r.length;
|
l = start + r.length;
|
||||||
continue;
|
continue;
|
||||||
} else if (defaultValue !== undefined) {
|
} else if (defaultValue !== undefined) {
|
||||||
const resolvedDefault = pathsub(defaultValue, lookup, depth + 1);
|
const resolvedDefault = pathsub(defaultValue, lookup, depth + 1);
|
||||||
s = s.substr(0, start) + resolvedDefault + s.substr(p + 1);
|
s = s.substring(0, start) + resolvedDefault + s.substring(p + 1);
|
||||||
l = start + resolvedDefault.length;
|
l = start + resolvedDefault.length;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -186,7 +196,7 @@ export function pathsub(
|
|||||||
if (m && m[0]) {
|
if (m && m[0]) {
|
||||||
const r = lookup(m[0], depth + 1);
|
const r = lookup(m[0], depth + 1);
|
||||||
if (r !== undefined) {
|
if (r !== undefined) {
|
||||||
s = s.substr(0, l) + r + s.substr(l + 1 + m[0].length);
|
s = s.substring(0, l) + r + s.substring(l + 1 + m[0].length);
|
||||||
l = l + r.length;
|
l = l + r.length;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -492,7 +502,9 @@ export class Configuration {
|
|||||||
value: val,
|
value: val,
|
||||||
sourceFile: opts.filename ?? "<unknown>",
|
sourceFile: opts.filename ?? "<unknown>",
|
||||||
sourceLine: lineNo,
|
sourceLine: lineNo,
|
||||||
origin: isDefaultSource ? EntryOrigin.Default : EntryOrigin.Loaded,
|
origin: isDefaultSource
|
||||||
|
? EntryOrigin.DefaultFile
|
||||||
|
: EntryOrigin.Loaded,
|
||||||
};
|
};
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -535,6 +547,23 @@ export class Configuration {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get upper-cased section names.
|
* Get upper-cased section names.
|
||||||
*/
|
*/
|
||||||
@ -595,7 +624,7 @@ export class Configuration {
|
|||||||
lookupVariable(x: string, depth: number = 0): string | undefined {
|
lookupVariable(x: string, depth: number = 0): string | undefined {
|
||||||
// We loop up options in PATHS in upper case, as option names
|
// We loop up options in PATHS in upper case, as option names
|
||||||
// are case insensitive
|
// are case insensitive
|
||||||
const val = this.findEntry("PATHS", x)?.value;
|
let val = this.findEntry("PATHS", x)?.value;
|
||||||
if (val !== undefined) {
|
if (val !== undefined) {
|
||||||
return pathsub(val, (v, d) => this.lookupVariable(v, d), depth);
|
return pathsub(val, (v, d) => this.lookupVariable(v, d), depth);
|
||||||
}
|
}
|
||||||
@ -623,21 +652,54 @@ export class Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private loadDefaults(): void {
|
private loadDefaults(): void {
|
||||||
let bc = process.env["TALER_BASE_CONFIG"];
|
let baseConfigDir = process.env["TALER_BASE_CONFIG"];
|
||||||
if (!bc) {
|
if (!baseConfigDir) {
|
||||||
/* Try to locate the configuration based on the location
|
/* Try to locate the configuration based on the location
|
||||||
* of the taler-config binary. */
|
* of the taler-config binary. */
|
||||||
const path = which("taler-config");
|
const path = which("taler-config");
|
||||||
if (path) {
|
if (path) {
|
||||||
bc = nodejs_fs.realpathSync(
|
baseConfigDir = nodejs_fs.realpathSync(
|
||||||
nodejs_path.dirname(path) + "/../share/taler/config.d",
|
nodejs_path.dirname(path) + "/../share/taler/config.d",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!bc) {
|
if (!baseConfigDir) {
|
||||||
bc = "/usr/share/taler/config.d";
|
baseConfigDir = "/usr/share/taler/config.d";
|
||||||
}
|
}
|
||||||
this.loadDefaultsFromDir(bc);
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
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/`);
|
||||||
|
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/`);
|
||||||
|
|
||||||
|
this.loadDefaultsFromDir(baseConfigDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
getDefaultConfigFilename(): string | undefined {
|
getDefaultConfigFilename(): string | undefined {
|
||||||
@ -698,7 +760,11 @@ export class Configuration {
|
|||||||
let headerWritten = false;
|
let headerWritten = false;
|
||||||
for (const optionName of Object.keys(sec.entries)) {
|
for (const optionName of Object.keys(sec.entries)) {
|
||||||
const entry = this.sectionMap[sectionName].entries[optionName];
|
const entry = this.sectionMap[sectionName].entries[optionName];
|
||||||
if (opts.excludeDefaults && entry.origin === EntryOrigin.Default) {
|
if (
|
||||||
|
opts.excludeDefaults &&
|
||||||
|
(entry.origin === EntryOrigin.DefaultSystem ||
|
||||||
|
entry.origin === EntryOrigin.DefaultFile)
|
||||||
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!headerWritten) {
|
if (!headerWritten) {
|
||||||
@ -711,7 +777,16 @@ export class Configuration {
|
|||||||
}
|
}
|
||||||
if (entry !== undefined) {
|
if (entry !== undefined) {
|
||||||
if (opts.diagnostics) {
|
if (opts.diagnostics) {
|
||||||
s += `# ${entry.sourceFile}:${entry.sourceLine}\n`;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
s += `${optionName} = ${entry.value}\n`;
|
s += `${optionName} = ${entry.value}\n`;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user