Skip to content

Commit 0816b40

Browse files
committedApr 10, 2024
feat(cli): support loading auth-token from env var
Support loading the auth token and Mermaid Chart base URL from environment variables using `MERMAID_CHART_AUTH_TOKEN` and `MERMAID_CHART_BASE_URL`. The order of priority is: 1. CLI --options 2. Environment variables 3. Config file 4. Default values
1 parent 1806bbe commit 0816b40

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed
 

‎packages/cli/src/commander.test.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeAll, beforeEach, describe, expect, vi, it, type Mock } from 'vitest';
1+
import { afterEach, beforeAll, beforeEach, describe, expect, vi, it, type Mock } from 'vitest';
22
import { createCommanderCommand } from './commander.js';
33
import { copyFile, mkdir, readFile, rm } from 'node:fs/promises';
44
import type { Command, CommanderError, OutputConfiguration } from '@commander-js/extra-typings';
@@ -106,6 +106,10 @@ describe('--version', () => {
106106
});
107107

108108
describe('whoami', () => {
109+
afterEach(() => {
110+
vi.unstubAllEnvs();
111+
});
112+
109113
it('should error if --config file does not exist', async () => {
110114
const { program } = mockedProgram();
111115

@@ -136,11 +140,13 @@ describe('whoami', () => {
136140
).rejects.toThrowError('Invalid access token.');
137141
});
138142

139-
it('--auth-token should override config file', async () => {
143+
it('--auth-token should override config file and environment variables', async () => {
140144
const { program } = mockedProgram();
141145

142146
const myCliAuthToken = 'my-cli-auth-token';
143147

148+
vi.stubEnv('MERMAID_CHART_AUTH_TOKEN', 'my-env-auth-token');
149+
144150
await program.parseAsync(
145151
['--config', CONFIG_AUTHED, '--auth-token', myCliAuthToken, 'whoami'],
146152
{ from: 'user' },
@@ -157,6 +163,21 @@ describe('whoami', () => {
157163

158164
expect(consoleLogSpy).toBeCalledWith(mockedMCUser.emailAddress);
159165
});
166+
167+
it('should support loading auth from environment variables', async () => {
168+
const { program } = mockedProgram();
169+
170+
const authToken = 'my-api-key-from-env-var';
171+
vi.stubEnv('MERMAID_CHART_AUTH_TOKEN', authToken);
172+
vi.stubEnv('MERMAID_CHART_BASE_URL', 'https://test.mermaidchart.invalid');
173+
174+
const consoleLogSpy = vi.spyOn(global.console, 'log');
175+
await program.parseAsync(['--config', CONFIG_AUTHED, 'whoami'], { from: 'user' });
176+
177+
// environment variables should override config file
178+
expect(vi.mocked(MermaidChart.prototype.setAccessToken)).toHaveBeenCalledWith(authToken);
179+
expect(consoleLogSpy).toBeCalledWith(mockedMCUser.emailAddress);
180+
});
160181
});
161182

162183
describe('login', () => {

‎packages/cli/src/commander.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,15 @@ export function createCommanderCommand() {
296296
defaultConfigPath(),
297297
)
298298
.addOption(
299-
new Option(
300-
'--base-url <base_url>',
301-
'The base URL of the Mermaid Chart instance to use.',
302-
).default('https://mermaidchart.com'),
299+
new Option('--base-url <base_url>', 'The base URL of the Mermaid Chart instance to use.')
300+
.default('https://mermaidchart.com')
301+
.env('MERMAID_CHART_BASE_URL'),
302+
)
303+
.addOption(
304+
new Option('--auth-token <auth_token>', 'The Mermaid Chart API token to use.').env(
305+
'MERMAID_CHART_AUTH_TOKEN',
306+
),
303307
)
304-
.addOption(new Option('--auth-token <auth_token>', 'The Mermaid Chart API token to use.'))
305308
.hook('preSubcommand', async (command, actionCommand) => {
306309
const configPath = command.getOptionValue('config');
307310
/**

0 commit comments

Comments
 (0)
Failed to load comments.