Skip to content

Commit

Permalink
Sort out the config logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaline committed Feb 22, 2024
1 parent d1e584a commit ad189ec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/cli/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Command } from "@commander-js/extra-typings";

import { Config } from "lib/config";
import { print } from "lib/logging";
import sindri from "lib";

export const configListCommand = new Command()
.name("list")
.description("Show the current config.")
.action(async () => {
const config = new Config();
print(config.config);
// Reload the config because the log level was `silent` when the config was initially loaded.
sindri._config!.reload();
print(sindri._config!.config);
});

export const configCommand = new Command()
Expand Down
6 changes: 3 additions & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export const program = new Command()
return exit(1);
}
if (debug) {
sindri.logger.level = "trace";
sindri.logLevel = "trace";
} else if (quiet) {
sindri.logger.level = "silent";
sindri.logLevel = "silent";
} else {
sindri.logger.level = "info";
sindri.logLevel = "info";
}
});

Expand Down
13 changes: 10 additions & 3 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export class SindriClient {
readonly _client: ApiClient;
/** @hidden */
readonly _clientConfig: OpenAPIConfig;
/** @hidden */
readonly _config: Config | undefined;

readonly logger: Logger;

Expand Down Expand Up @@ -126,6 +128,9 @@ export class SindriClient {
this._client = new ApiClient();
this._clientConfig = this._client.request.config;
this.logger = createLogger();
if (!process.env.BROWSER_BUILD) {
this._config = new Config(this.logger);
}
this._clientConfig.logger = this.logger;
this.authorize(authOptions);
}
Expand Down Expand Up @@ -233,15 +238,17 @@ export class SindriClient {
this._clientConfig.BASE = authOptions.baseUrl || "https://sindri.app";
this._clientConfig.TOKEN = authOptions.apiKey;
} else {
const config = new Config();
this._config!.reload();
this._clientConfig.BASE =
authOptions.baseUrl ||
process.env.SINDRI_BASE_URL ||
config.auth?.baseUrl ||
this._config!.auth?.baseUrl ||
this._clientConfig.BASE ||
"https://sindri.app";
this._clientConfig.TOKEN =
authOptions.apiKey || process.env.SINDRI_API_KEY || config.auth?.apiKey;
authOptions.apiKey ||
process.env.SINDRI_API_KEY ||
this._config!.auth?.apiKey;
}
return !!(this._clientConfig.BASE && this._clientConfig.TOKEN);
}
Expand Down
38 changes: 21 additions & 17 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import envPaths from "env-paths";
import _ from "lodash";
import { z } from "zod";

import { logger } from "lib/logging";
import { type Logger } from "lib/logging";

const getConfigPath = (): string => {
const paths = envPaths("sindri", {
Expand Down Expand Up @@ -33,41 +33,38 @@ type ConfigSchema = z.infer<typeof ConfigSchema>;

const defaultConfig: ConfigSchema = ConfigSchema.parse({});

export const loadConfig = (): ConfigSchema => {
export const loadConfig = (logger?: Logger): ConfigSchema => {
const configPath = getConfigPath();
if (fs.existsSync(configPath)) {
logger.debug(`Loading config from "${configPath}".`);
logger?.debug(`Loading config from "${configPath}".`);
try {
const configFileContents: string = fs.readFileSync(configPath, {
encoding: "utf-8",
});
const loadedConfig = ConfigSchema.parse(JSON.parse(configFileContents));
logger.debug("Config loaded successfully.");
logger?.debug("Config loaded successfully.");
return loadedConfig;
} catch (error) {
logger.warn(
logger?.warn(
`The config schema in "${configPath}" is invalid and will not be used.\n` +
`To remove it and start fresh, run:\n rm ${configPath}`,
);
logger.debug(error);
logger?.debug(error);
}
}
logger.debug(
logger?.debug(
`Config file "${configPath}" does not exist, initializing default config.`,
);
return _.cloneDeep(defaultConfig);
};

export class Config {
protected _config!: ConfigSchema;
protected static instance: Config;
protected readonly logger: Logger | undefined;

constructor() {
if (!Config.instance) {
this._config = loadConfig();
Config.instance = this;
}
return Config.instance;
constructor(logger?: Logger) {
this.logger = logger;
this.reload();
}

get auth(): ConfigSchema["auth"] {
Expand All @@ -78,10 +75,14 @@ export class Config {
return _.cloneDeep(this._config);
}

reload() {
this._config = loadConfig(this.logger);
}

update(configData: Partial<ConfigSchema>) {
// Merge and validate the configs.
logger.debug("Merging in config update:");
logger.debug(configData);
this.logger?.debug("Merging in config update:");
this.logger?.debug(configData);
const newConfig: ConfigSchema = _.cloneDeep(this._config);
_.merge(newConfig, configData);
this._config = ConfigSchema.parse(newConfig);
Expand All @@ -94,7 +95,10 @@ export class Config {
}

// Write out the new config.
logger.debug(`Writing merged config to "${configPath}":`, this._config);
this.logger?.debug(
`Writing merged config to "${configPath}":`,
this._config,
);
fs.writeFileSync(configPath, JSON.stringify(this._config, null, 2), {
encoding: "utf-8",
});
Expand Down

0 comments on commit ad189ec

Please sign in to comment.