refactor(config): load JSON configs and emit JSON-only PCA output
This commit is contained in:
52
src/index.js
52
src/index.js
@@ -10,31 +10,37 @@ export function getUserConfigDir() {
|
||||
return process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), ".config");
|
||||
}
|
||||
|
||||
export async function loadConfig(type) {
|
||||
const configBaseDir = getUserConfigDir();
|
||||
const configType = typeof type === "string" ? type.toLowerCase() : "";
|
||||
let configFileName;
|
||||
|
||||
switch (configType) {
|
||||
case "public":
|
||||
case "p":
|
||||
configFileName = "public-config.json";
|
||||
break;
|
||||
case "confidential":
|
||||
case "c":
|
||||
configFileName = "confidential-config.json";
|
||||
break;
|
||||
default:
|
||||
configFileName = null;
|
||||
}
|
||||
|
||||
if (!configFileName) {
|
||||
async function loadConfig(configFileName) {
|
||||
if (typeof configFileName !== "string" || configFileName.trim() === "") {
|
||||
throw new Error(
|
||||
`Invalid config type: ${type}. Expected "public"|"p" or "confidential"|"c".`,
|
||||
'Invalid config file name. Expected a non-empty string like "public-config.json" or "confidential-config.json".',
|
||||
);
|
||||
}
|
||||
|
||||
const configPath = path.join(configBaseDir, "sk-az-tools", configFileName);
|
||||
const configJson = await readFile(configPath, "utf8");
|
||||
return JSON.parse(configJson);
|
||||
const config = {
|
||||
tenantId: process.env.AZURE_TENANT_ID,
|
||||
clientId: process.env.AZURE_CLIENT_ID,
|
||||
};
|
||||
|
||||
const configPath = path.join(getUserConfigDir(), "sk-az-tools", configFileName);
|
||||
return readFile(configPath, "utf8")
|
||||
.then((configJson) => JSON.parse(configJson))
|
||||
.catch((err) => {
|
||||
if (err?.code === "ENOENT") {
|
||||
return {};
|
||||
}
|
||||
throw err;
|
||||
})
|
||||
.then((json) => ({
|
||||
tenantId: json.tenantId || config.tenantId,
|
||||
clientId: json.clientId || config.clientId,
|
||||
}));
|
||||
}
|
||||
|
||||
export function loadPublicConfig() {
|
||||
return loadConfig("public-config.json");
|
||||
}
|
||||
|
||||
export function loadConfidentialConfig() {
|
||||
return loadConfig("confidential-config.json");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user