Skip to content

Commit

Permalink
Can save the updated root config
Browse files Browse the repository at this point in the history
  • Loading branch information
macmv committed Sep 28, 2023
1 parent 0f434ce commit 69c144a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/commands/add-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Adds an endpoint to ~/.fauna-shell.
secret,
url,
});
// config.saveRootConfig();
config.saveRootConfig();

this.log(`Saved endpoint ${endpointName} to ${getRootConfigPath()}`);
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/lib/config/root-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Config, InvalidConfigError } from ".";
import fs from "fs";
const ini = require("ini");

// Represents `~/.fauna-shell`
export class RootConfig {
Expand Down Expand Up @@ -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()])
),
};
}
}

/**
Expand Down Expand Up @@ -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 } : {}),
};
}
}

0 comments on commit 69c144a

Please sign in to comment.