Skip to content

Commit

Permalink
implement config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
echo-bravo-yahoo committed Nov 22, 2024
1 parent 1ff1144 commit 0a1fc6e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 767 deletions.
17 changes: 16 additions & 1 deletion src/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import keyCommand from "./commands/key.mjs";
import loginCommand from "./commands/login.mjs";
import schemaCommand from "./commands/schema/schema.mjs";
import shellCommand from "./commands/shell.mjs";

import { authNZMiddleware } from "./lib/auth/authNZ.mjs";
import { checkForUpdates, fixPaths, logArgv } from "./lib/middleware.mjs";
import { configParser } from "./lib/config/config.mjs";

/** @typedef {import('awilix').AwilixContainer<import('./config/setup-container.mjs').modifiedInjectables>} cliContainer */

Expand Down Expand Up @@ -92,6 +94,8 @@ function buildYargs(argvInput) {

return yargsInstance
.scriptName("fauna")
.env("FAUNA")
.config("config", configParser)
.middleware([checkForUpdates, logArgv], true)
.middleware([fixPaths, authNZMiddleware], false)
.command("eval", "evaluate a query", evalCommand)
Expand All @@ -103,9 +107,20 @@ function buildYargs(argvInput) {
.demandCommand()
.strict(true)
.options({
config: {
type: "string",
description: "a config file to use",
},
profile: {
alias: "p",
type: "string",
description:
"the profile in your config file to fetch CLI settings from",
default: "default",
},
user: {
alias: "u",
type: "string",
description: "a user profile",
default: "default",
},
Expand Down Expand Up @@ -137,7 +152,7 @@ function buildYargs(argvInput) {
"components to emit diagnostic logs for; this takes precedence over the 'verbosity' flag",
type: "array",
default: [],
choices: ["fetch", "error", "argv"],
choices: ["fetch", "error", "config", "argv"],
},
// Whether authNZ middleware should run. Better way of doing this?
authRequired: {
Expand Down
33 changes: 33 additions & 0 deletions src/lib/config/config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from "node:fs";

import yaml from "yaml";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import { container } from "../../cli.mjs";

/** @type {yaml.Document.Parsed<Record<string, any>>} */
export function getConfig(path) {
const fileBody = fs.readFileSync(path, { encoding: "utf8" });
return yaml.parseDocument(fileBody);
}

let parsedProfile;
export function configParser(path) {
const logger = container.resolve("logger");

if (parsedProfile) return parsedProfile;

logger.debug(`Reading config from ${path}...`, "config");
const config = getConfig(path);
const argv = yargs(hideBin(process.argv)).options({
profile: {
default: "default",
},
}).argv;
logger.debug(`Using profile ${argv.profile}...`, "config");
parsedProfile = config.toJSON()[argv.profile];
logger.debug(`Applying config: ${JSON.stringify(parsedProfile, null, 4)}`);

return parsedProfile;
}
Loading

0 comments on commit 0a1fc6e

Please sign in to comment.