From 69c144a7575786cdc369abbd07b8ef347a13393a Mon Sep 17 00:00:00 2001 From: Neil Macneale V Date: Thu, 28 Sep 2023 14:05:06 -0700 Subject: [PATCH] Can save the updated root config --- src/commands/add-endpoint.ts | 2 +- src/lib/config/index.ts | 7 +++++++ src/lib/config/root-config.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/commands/add-endpoint.ts b/src/commands/add-endpoint.ts index 588e57e3..4ae87d38 100644 --- a/src/commands/add-endpoint.ts +++ b/src/commands/add-endpoint.ts @@ -125,7 +125,7 @@ Adds an endpoint to ~/.fauna-shell. secret, url, }); - // config.saveRootConfig(); + config.saveRootConfig(); this.log(`Saved endpoint ${endpointName} to ${getRootConfigPath()}`); } diff --git a/src/lib/config/index.ts b/src/lib/config/index.ts index 76056802..8d0c515d 100644 --- a/src/lib/config/index.ts +++ b/src/lib/config/index.ts @@ -282,6 +282,13 @@ export class ShellConfig { this.projectConfig?.save(this.projectConfigFile()!); } + /** + * Saves the root config. + */ + saveRootConfig() { + this.rootConfig.save(getRootConfigPath()); + } + projectConfigFile(): string | undefined { return this.projectPath === undefined ? undefined diff --git a/src/lib/config/root-config.ts b/src/lib/config/root-config.ts index 90ef0aaf..eeb31d3f 100644 --- a/src/lib/config/root-config.ts +++ b/src/lib/config/root-config.ts @@ -1,4 +1,6 @@ import { Config, InvalidConfigError } from "."; +import fs from "fs"; +const ini = require("ini"); // Represents `~/.fauna-shell` export class RootConfig { @@ -26,6 +28,24 @@ export class RootConfig { ); } } + + save(path: string) { + const config = this.toIni(); + + const encoded = ini.encode(config); + fs.writeFileSync(path, encoded); + } + + toIni() { + return { + ...(this.defaultEndpoint !== undefined + ? { default: this.defaultEndpoint } + : {}), + endpoint: Object.fromEntries( + Object.entries(this.endpoints).map(([k, v]) => [k, v.toIni()]) + ), + }; + } } /** @@ -134,4 +154,16 @@ export class Endpoint { return opts.url; } }; + + toIni() { + return { + secret: this.secret, + ...(this.url != "https://db.fauna.com" ? { url: this.url } : {}), + + ...(this.graphqlHost != "graphql.fauna.com" + ? { graphqlHost: this.graphqlHost } + : {}), + ...(this.graphqlPort != 443 ? { graphqlPort: this.graphqlPort } : {}), + }; + } }