@contrail/cli 3.8.0-alpha.8de44ff → 3.8.0-alpha.ca28651

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,7 +17,7 @@ $ npm install -g @contrail/cli
17
17
  $ contrail COMMAND
18
18
  running command...
19
19
  $ contrail (--version)
20
- @contrail/cli/3.8.0-alpha.8de44ff linux-x64 node-v24.22.3
20
+ @contrail/cli/3.8.0-alpha.ca28651 linux-x64 node-v24.22.3
21
21
  $ contrail --help [COMMAND]
22
22
  USAGE
23
23
  $ contrail COMMAND
@@ -172,6 +172,7 @@ async function authenticateWithApiKey(config, apiKey, orgSlug) {
172
172
  (0, sdk_1.setConfig)(sdk_1.Auth.config);
173
173
  await (0, sdk_1.getCurrentUser)();
174
174
  await (0, config_1.storeConfig)({ orgSlug, apiGateway });
175
+ await (0, config_1.clearStoredApiKey)();
175
176
  return Object.assign({}, sdk_1.Auth.config);
176
177
  }
177
178
  async function authenticateWithUsernamePassword(params) {
@@ -23,6 +23,7 @@ jest.mock('../helpers/config/config', () => ({
23
23
  ...jest.requireActual('../helpers/config/config'),
24
24
  getConfig: jest.fn(),
25
25
  storeConfig: jest.fn(),
26
+ clearStoredApiKey: jest.fn(),
26
27
  }));
27
28
  jest.mock('../logger', () => ({
28
29
  __esModule: true,
@@ -146,6 +147,10 @@ describe('authUser with an API key', () => {
146
147
  expect(context).toMatchObject({ apiUserToken: 'api-key-from-env', orgSlug: 'adam-zone-1' });
147
148
  expect(config_1.storeConfig).toHaveBeenCalledWith({ orgSlug: 'adam-zone-1', apiGateway: DEV_GATEWAY });
148
149
  });
150
+ it('should clear an API key that an earlier version saved', async () => {
151
+ await (0, login_1.authUser)();
152
+ expect(config_1.clearStoredApiKey).toHaveBeenCalled();
153
+ });
149
154
  });
150
155
  describe('authUser with stored tokens', () => {
151
156
  const originalFetch = global.fetch;
@@ -18,3 +18,8 @@ export declare abstract class BaseCommand extends Command {
18
18
  }): Promise<unknown>;
19
19
  private confirmEnv;
20
20
  }
21
+ export declare abstract class ConfigCommand extends BaseCommand {
22
+ skipConfirmEnv: boolean;
23
+ shouldSelectConfigAtStart: boolean;
24
+ protected getConfigNameFromFlag(): string | undefined;
25
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseCommand = void 0;
3
+ exports.ConfigCommand = exports.BaseCommand = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const core_1 = require("@oclif/core");
6
6
  const common_1 = require("./common");
@@ -32,11 +32,11 @@ class BaseCommand extends core_1.Command {
32
32
  });
33
33
  this.flags = flags;
34
34
  this.args = args;
35
- (0, config_1.selectConfigForRun)(flags.config);
35
+ const config = await (0, config_1.readVibeIQConfig)();
36
+ (0, config_1.selectConfigForRun)({ requestedName: flags.config, nameInUse: config?.configInUse });
36
37
  if (this.shouldSelectConfigAtStart) {
37
38
  await (0, config_1.fetchConfigNameForRun)();
38
39
  }
39
- const config = await (0, config_1.readVibeIQConfig)();
40
40
  if (!this.skipValidateEnv) {
41
41
  await (0, config_1.validateEnvironments)();
42
42
  }
@@ -94,3 +94,12 @@ class BaseCommand extends core_1.Command {
94
94
  }
95
95
  }
96
96
  exports.BaseCommand = BaseCommand;
97
+ class ConfigCommand extends BaseCommand {
98
+ skipConfirmEnv = true;
99
+ shouldSelectConfigAtStart = false;
100
+ getConfigNameFromFlag() {
101
+ const isConfigFlagPassed = this.argv.some((arg) => arg === '--config' || arg.startsWith('--config='));
102
+ return isConfigFlagPassed ? this.flags.config : undefined;
103
+ }
104
+ }
105
+ exports.ConfigCommand = ConfigCommand;
@@ -3,8 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const fs_1 = require("fs");
5
5
  const os_1 = require("os");
6
+ const util_1 = require("util");
6
7
  const path_1 = require("path");
7
8
  const basecommand_1 = require("./basecommand");
9
+ const list_1 = tslib_1.__importDefault(require("./commands/config/list"));
10
+ const describe_1 = tslib_1.__importDefault(require("./commands/config/describe"));
8
11
  const set_1 = tslib_1.__importDefault(require("./commands/config/set"));
9
12
  const config_1 = require("./helpers/config/config");
10
13
  jest.mock('node:os', () => {
@@ -66,4 +69,29 @@ describe('config selection on a command', () => {
66
69
  orgSlug: 'acme',
67
70
  });
68
71
  });
72
+ it('should fail without creating a config when CONTRAIL_CLI_CONFIG names one that does not exist', async () => {
73
+ process.env.CONTRAIL_CLI_CONFIG = 'misspelled';
74
+ await expect(set_1.default.run(['--orgSlug', 'acme'], PACKAGE_ROOT)).rejects.toThrow('No config named "misspelled"');
75
+ expect((0, fs_1.existsSync)((0, path_1.join)(configDir, 'misspelled.json'))).toBe(false);
76
+ });
77
+ it('should list configs when CONTRAIL_CLI_CONFIG names one that does not exist', async () => {
78
+ process.env.CONTRAIL_CLI_CONFIG = 'misspelled';
79
+ const table = jest.spyOn(console, 'table').mockImplementation(() => undefined);
80
+ await list_1.default.run([], PACKAGE_ROOT);
81
+ expect(table).toHaveBeenCalledWith(expect.arrayContaining([{ Name: 'pointer' }]), ['Name']);
82
+ table.mockRestore();
83
+ });
84
+ it('should describe the config named by --config', async () => {
85
+ const log = jest.spyOn(describe_1.default.prototype, 'log').mockImplementation(() => undefined);
86
+ await describe_1.default.run(['--config', 'from-flag'], PACKAGE_ROOT);
87
+ expect(log).toHaveBeenCalledWith((0, util_1.inspect)({ orgSlug: 'from-flag' }));
88
+ log.mockRestore();
89
+ });
90
+ it('should create the config when --config and CONTRAIL_CLI_CONFIG name the same new one', async () => {
91
+ process.env.CONTRAIL_CLI_CONFIG = 'new-config';
92
+ await set_1.default.run(['--orgSlug', 'acme', '--config', 'new-config'], PACKAGE_ROOT);
93
+ expect(JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(configDir, 'new-config.json')).toString())).toEqual({
94
+ orgSlug: 'acme',
95
+ });
96
+ });
69
97
  });
@@ -1,8 +1,6 @@
1
- import { BaseCommand } from '../../basecommand';
2
- export default class CreateConfig extends BaseCommand {
1
+ import { ConfigCommand } from '../../basecommand';
2
+ export default class CreateConfig extends ConfigCommand {
3
3
  static description: string;
4
- skipConfirmEnv: boolean;
5
- shouldSelectConfigAtStart: boolean;
6
4
  static examples: string[];
7
5
  static flags: {
8
6
  name: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
@@ -5,13 +5,11 @@ const config_1 = require("../../helpers/config/config");
5
5
  const basecommand_1 = require("../../basecommand");
6
6
  const core_1 = require("@oclif/core");
7
7
  const logger_1 = tslib_1.__importDefault(require("../../logger"));
8
- class CreateConfig extends basecommand_1.BaseCommand {
8
+ class CreateConfig extends basecommand_1.ConfigCommand {
9
9
  static description = 'Create a new configuration file';
10
- skipConfirmEnv = true;
11
- shouldSelectConfigAtStart = false;
12
10
  static examples = ['$ contrail config create --name demo-org'];
13
11
  static flags = {
14
- ...basecommand_1.BaseCommand.baseFlags,
12
+ ...basecommand_1.ConfigCommand.baseFlags,
15
13
  name: core_1.Flags.string({ description: 'The config name', required: true }),
16
14
  };
17
15
  async run() {
@@ -1,8 +1,6 @@
1
- import { BaseCommand } from '../../basecommand';
2
- export default class DescribeConfig extends BaseCommand {
1
+ import { ConfigCommand } from '../../basecommand';
2
+ export default class DescribeConfig extends ConfigCommand {
3
3
  static description: string;
4
- skipConfirmEnv: boolean;
5
- shouldSelectConfigAtStart: boolean;
6
4
  static examples: string[];
7
5
  static args: {
8
6
  name: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
@@ -8,19 +8,17 @@ const basecommand_1 = require("../../basecommand");
8
8
  const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
9
9
  const core_1 = require("@oclif/core");
10
10
  const logger_1 = tslib_1.__importDefault(require("../../logger"));
11
- class DescribeConfig extends basecommand_1.BaseCommand {
11
+ class DescribeConfig extends basecommand_1.ConfigCommand {
12
12
  static description = 'Display the contents of a configuration file';
13
- skipConfirmEnv = true;
14
- shouldSelectConfigAtStart = false;
15
13
  static examples = ['$ contrail config describe demo-org'];
16
14
  static args = {
17
15
  name: core_1.Args.string({ description: 'The config name', required: false }),
18
16
  };
19
17
  static flags = {
20
- ...basecommand_1.BaseCommand.baseFlags,
18
+ ...basecommand_1.ConfigCommand.baseFlags,
21
19
  };
22
20
  async run() {
23
- let configName = this.args.name ?? this.flags.config;
21
+ let configName = this.args.name ?? this.getConfigNameFromFlag();
24
22
  const arrConfigFiles = await (0, config_1.getConfigFiles)();
25
23
  if (configName && !arrConfigFiles.includes(configName)) {
26
24
  logger_1.default.error(`No config found with name ${configName}`);
@@ -1,8 +1,6 @@
1
- import { BaseCommand } from '../../basecommand';
2
- export default class ListConfig extends BaseCommand {
1
+ import { ConfigCommand } from '../../basecommand';
2
+ export default class ListConfig extends ConfigCommand {
3
3
  static description: string;
4
- skipConfirmEnv: boolean;
5
- shouldSelectConfigAtStart: boolean;
6
4
  static examples: string[];
7
5
  static flags: {
8
6
  config: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
@@ -4,21 +4,26 @@ const tslib_1 = require("tslib");
4
4
  const config_1 = require("../../helpers/config/config");
5
5
  const basecommand_1 = require("../../basecommand");
6
6
  const chalk_1 = tslib_1.__importDefault(require("chalk"));
7
- class ListConfig extends basecommand_1.BaseCommand {
7
+ const logger_1 = tslib_1.__importDefault(require("../../logger"));
8
+ class ListConfig extends basecommand_1.ConfigCommand {
8
9
  static description = 'List the configurations';
9
- skipConfirmEnv = true;
10
- shouldSelectConfigAtStart = false;
11
10
  static examples = [
12
11
  `$ contrail config list
13
12
  `,
14
13
  ];
15
14
  static flags = {
16
- ...basecommand_1.BaseCommand.baseFlags,
15
+ ...basecommand_1.ConfigCommand.baseFlags,
17
16
  };
18
17
  async run() {
19
18
  // Get all available config files
20
19
  const arrConfigFiles = await (0, config_1.getConfigFiles)();
21
- const activeConfig = (await (0, config_1.fetchConfigNameForRun)()) || '';
20
+ const activeConfig = await (0, config_1.fetchConfigNameForRun)().catch((error) => {
21
+ if (!(error instanceof config_1.ConfigSelectionError)) {
22
+ throw error;
23
+ }
24
+ logger_1.default.warn(error.message);
25
+ return '';
26
+ });
22
27
  // Create a simpler array of objects with just the names
23
28
  const tableData = arrConfigFiles.map((config, index) => {
24
29
  return {
@@ -1,7 +1,5 @@
1
- import { BaseCommand } from '../../basecommand';
2
- export default class SetConfig extends BaseCommand {
3
- skipConfirmEnv: boolean;
4
- shouldSelectConfigAtStart: boolean;
1
+ import { ConfigCommand } from '../../basecommand';
2
+ export default class SetConfig extends ConfigCommand {
5
3
  static description: string;
6
4
  static examples: string[];
7
5
  static flags: {
@@ -5,16 +5,14 @@ const core_1 = require("@oclif/core");
5
5
  const config_1 = require("../../helpers/config/config");
6
6
  const basecommand_1 = require("../../basecommand");
7
7
  const logger_1 = tslib_1.__importDefault(require("../../logger"));
8
- class SetConfig extends basecommand_1.BaseCommand {
9
- skipConfirmEnv = true;
10
- shouldSelectConfigAtStart = false;
8
+ class SetConfig extends basecommand_1.ConfigCommand {
11
9
  static description = 'Set a variable in the configuration';
12
10
  static examples = [
13
11
  '$ contrail config set --orgSlug acme',
14
12
  '$ contrail config set --orgSlug acme --config acme-dev',
15
13
  ];
16
14
  static flags = {
17
- ...basecommand_1.BaseCommand.baseFlags,
15
+ ...basecommand_1.ConfigCommand.baseFlags,
18
16
  orgSlug: core_1.Flags.string({ description: 'The org slug', required: false }),
19
17
  environment: core_1.Flags.string({
20
18
  description: 'The Environment',
@@ -29,7 +27,7 @@ class SetConfig extends basecommand_1.BaseCommand {
29
27
  static args = {};
30
28
  async run() {
31
29
  delete this.flags.alwaysConfirmEnv;
32
- const configName = this.flags.config;
30
+ const configName = this.getConfigNameFromFlag();
33
31
  delete this.flags.config;
34
32
  if (configName && !(await (0, config_1.getConfigFiles)()).includes(configName)) {
35
33
  await (0, config_1.createConfig)(configName);
@@ -1,8 +1,6 @@
1
- import { BaseCommand } from '../../basecommand';
2
- export default class UseConfig extends BaseCommand {
1
+ import { ConfigCommand } from '../../basecommand';
2
+ export default class UseConfig extends ConfigCommand {
3
3
  static description: string;
4
- skipConfirmEnv: boolean;
5
- shouldSelectConfigAtStart: boolean;
6
4
  static examples: string[];
7
5
  static args: {
8
6
  name: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
@@ -6,16 +6,14 @@ const basecommand_1 = require("../../basecommand");
6
6
  const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
7
7
  const core_1 = require("@oclif/core");
8
8
  const logger_1 = tslib_1.__importDefault(require("../../logger"));
9
- class UseConfig extends basecommand_1.BaseCommand {
9
+ class UseConfig extends basecommand_1.ConfigCommand {
10
10
  static description = 'Use a specific configuration';
11
- skipConfirmEnv = true;
12
- shouldSelectConfigAtStart = false;
13
11
  static examples = ['$ contrail config use demo-org'];
14
12
  static args = {
15
13
  name: core_1.Args.string({ description: 'The config name', required: false }),
16
14
  };
17
15
  static flags = {
18
- ...basecommand_1.BaseCommand.baseFlags,
16
+ ...basecommand_1.ConfigCommand.baseFlags,
19
17
  };
20
18
  async run() {
21
19
  let configName = this.args.name;
@@ -1,3 +1,7 @@
1
+ export declare class ConfigSelectionError extends Error {
2
+ readonly code: 'CONFIG_NOT_SELECTED' | 'CONFIG_NOT_FOUND';
3
+ constructor(code: 'CONFIG_NOT_SELECTED' | 'CONFIG_NOT_FOUND', message: string);
4
+ }
1
5
  export declare const ENVIRONMENTS: {
2
6
  dev: string;
3
7
  prod: string;
@@ -9,9 +13,13 @@ export declare function readVibeIQConfig(): Promise<any>;
9
13
  export declare function writeVibeIQConfig(config: any): Promise<void>;
10
14
  export declare function storeVibeIQConfig(incomingConfig: any): Promise<void>;
11
15
  export declare function createConfig(fileName: any): Promise<void>;
12
- export declare function selectConfigForRun(configName: string | undefined): void;
16
+ export declare function selectConfigForRun({ requestedName, nameInUse, }: {
17
+ requestedName: string | undefined;
18
+ nameInUse: string | undefined;
19
+ }): void;
13
20
  export declare function fetchConfigNameForRun(): Promise<string>;
14
21
  export declare function getConfig(): Promise<any>;
22
+ export declare function clearStoredApiKey(): Promise<void>;
15
23
  export declare function storeConfig(incomingConfig: any): Promise<void>;
16
24
  export declare function getConfigFiles(): Promise<any>;
17
25
  export declare function getConfigContent(fileName: string): Promise<any>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkTelemetry = exports.validateEnvironments = exports.getConfigContent = exports.getConfigFiles = exports.storeConfig = exports.getConfig = exports.fetchConfigNameForRun = exports.selectConfigForRun = exports.createConfig = exports.storeVibeIQConfig = exports.writeVibeIQConfig = exports.readVibeIQConfig = exports.initializeVibeIQConfig = exports.findEnvironmentName = exports.ENVIRONMENTS = void 0;
3
+ exports.checkTelemetry = exports.validateEnvironments = exports.getConfigContent = exports.getConfigFiles = exports.storeConfig = exports.clearStoredApiKey = exports.getConfig = exports.fetchConfigNameForRun = exports.selectConfigForRun = exports.createConfig = exports.storeVibeIQConfig = exports.writeVibeIQConfig = exports.readVibeIQConfig = exports.initializeVibeIQConfig = exports.findEnvironmentName = exports.ENVIRONMENTS = exports.ConfigSelectionError = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const node_fs_1 = require("node:fs");
6
6
  const node_path_1 = require("node:path");
@@ -13,7 +13,17 @@ const userHomeDir = (0, node_os_1.homedir)();
13
13
  const configDirectoryLocation = (0, node_path_1.join)(userHomeDir, '.vibeiq', 'configs');
14
14
  const configFileLocation = (0, node_path_1.join)(configDirectoryLocation, configFileName);
15
15
  const defaultConfigFileLocation = (0, node_path_1.join)(configDirectoryLocation, defaultConfigFileName);
16
+ class ConfigSelectionError extends Error {
17
+ code;
18
+ constructor(code, message) {
19
+ super(message);
20
+ this.code = code;
21
+ }
22
+ }
23
+ exports.ConfigSelectionError = ConfigSelectionError;
24
+ let hasSelectedConfig = false;
16
25
  let requestedConfigName;
26
+ let configNameInUse;
17
27
  let configNameForRun;
18
28
  exports.ENVIRONMENTS = {
19
29
  dev: 'https://api.dev.vibeiq.com/dev/api',
@@ -102,27 +112,31 @@ async function writeConfig(config, configName) {
102
112
  throw error;
103
113
  }
104
114
  }
105
- function selectConfigForRun(configName) {
106
- requestedConfigName = configName;
115
+ function selectConfigForRun({ requestedName, nameInUse, }) {
116
+ hasSelectedConfig = true;
117
+ requestedConfigName = requestedName;
118
+ configNameInUse = nameInUse;
107
119
  configNameForRun = undefined;
108
120
  }
109
121
  exports.selectConfigForRun = selectConfigForRun;
110
122
  function fetchConfigNameForRun() {
123
+ if (!hasSelectedConfig) {
124
+ return Promise.reject(new Error('No config selected for this run. Call selectConfigForRun before reading a config.'));
125
+ }
111
126
  configNameForRun ??= fetchConfigNameFromFlagOrPointer();
112
127
  return configNameForRun;
113
128
  }
114
129
  exports.fetchConfigNameForRun = fetchConfigNameForRun;
115
130
  async function fetchConfigNameFromFlagOrPointer() {
116
131
  if (!requestedConfigName) {
117
- const configInUse = (await readVibeIQConfig())?.configInUse;
118
- if (!configInUse) {
119
- throw new Error('No config selected. Pass --config, set CONTRAIL_CLI_CONFIG, or run `contrail config use`.');
132
+ if (!configNameInUse) {
133
+ throw new ConfigSelectionError('CONFIG_NOT_SELECTED', 'No config selected. Pass --config, set CONTRAIL_CLI_CONFIG, or run `contrail config use`.');
120
134
  }
121
- return configInUse;
135
+ return configNameInUse;
122
136
  }
123
- const configNames = await getConfigFiles();
124
- if (!configNames?.includes(requestedConfigName)) {
125
- throw new Error(`No config named "${requestedConfigName}". Pass --config or set CONTRAIL_CLI_CONFIG to one of: ${configNames?.join(', ')}`);
137
+ const configNames = (await getConfigFiles()) ?? [];
138
+ if (!configNames.includes(requestedConfigName)) {
139
+ throw new ConfigSelectionError('CONFIG_NOT_FOUND', `No config named "${requestedConfigName}". Create it with \`contrail config create --name ${requestedConfigName}\`, or pass --config or set CONTRAIL_CLI_CONFIG to one of: ${configNames.join(', ')}`);
126
140
  }
127
141
  return requestedConfigName;
128
142
  }
@@ -130,6 +144,18 @@ async function getConfig() {
130
144
  return getConfigContent(await fetchConfigNameForRun());
131
145
  }
132
146
  exports.getConfig = getConfig;
147
+ async function clearStoredApiKey() {
148
+ const configName = await fetchConfigNameForRun();
149
+ const config = await getConfigContent(configName);
150
+ if (config?.signInMethod !== 'apiKey') {
151
+ return;
152
+ }
153
+ delete config.apiUserToken;
154
+ delete config.apiRefreshToken;
155
+ delete config.signInMethod;
156
+ await writeConfig(config, configName);
157
+ }
158
+ exports.clearStoredApiKey = clearStoredApiKey;
133
159
  async function storeConfig(incomingConfig) {
134
160
  const configName = await fetchConfigNameForRun();
135
161
  const config = {
@@ -36,15 +36,18 @@ describe('config selection', () => {
36
36
  (0, fs_1.rmSync)(mockHome, { recursive: true, force: true });
37
37
  });
38
38
  it('should use the config chosen with config use when none is named', async () => {
39
- config.selectConfigForRun(undefined);
39
+ config.selectConfigForRun({ requestedName: undefined, nameInUse: 'dev' });
40
40
  expect(await config.getConfig()).toEqual(DEV);
41
41
  });
42
+ it('should fail when a config is read before one is selected for the run', async () => {
43
+ await expect(config.getConfig()).rejects.toThrow('No config selected for this run.');
44
+ });
42
45
  it('should use the named config over the one chosen with config use', async () => {
43
- config.selectConfigForRun('prod');
46
+ config.selectConfigForRun({ requestedName: 'prod', nameInUse: 'dev' });
44
47
  expect(await config.getConfig()).toEqual(PROD);
45
48
  });
46
49
  it('should keep reading and saving the config it started with after another terminal switches', async () => {
47
- config.selectConfigForRun(undefined);
50
+ config.selectConfigForRun({ requestedName: undefined, nameInUse: 'dev' });
48
51
  await config.getConfig();
49
52
  switchPointerTo('prod');
50
53
  await config.storeConfig({ apiUserToken: 'refreshed-dev-token' });
@@ -52,16 +55,15 @@ describe('config selection', () => {
52
55
  expect(readJson('prod')).toEqual(PROD);
53
56
  });
54
57
  it('should fail when no config is named and none is chosen with config use', async () => {
55
- writeJson('config', {});
56
- config.selectConfigForRun(undefined);
58
+ config.selectConfigForRun({ requestedName: undefined, nameInUse: undefined });
57
59
  await expect(config.getConfig()).rejects.toThrow('No config selected.');
58
60
  });
59
61
  it('should fail and list the configs when the named config does not exist', async () => {
60
- config.selectConfigForRun('staging');
61
- await expect(config.getConfig()).rejects.toThrow('No config named "staging". Pass --config or set CONTRAIL_CLI_CONFIG to one of: dev, prod');
62
+ config.selectConfigForRun({ requestedName: 'staging', nameInUse: 'dev' });
63
+ await expect(config.getConfig()).rejects.toThrow('No config named "staging". Create it with `contrail config create --name staging`, or pass --config or set CONTRAIL_CLI_CONFIG to one of: dev, prod');
62
64
  });
63
65
  it('should leave the previous config intact when a save dies partway', async () => {
64
- config.selectConfigForRun('dev');
66
+ config.selectConfigForRun({ requestedName: 'dev', nameInUse: 'dev' });
65
67
  const realWriteFile = fs_1.promises.writeFile;
66
68
  jest.spyOn(fs_1.promises, 'writeFile').mockImplementationOnce(async (path) => {
67
69
  await realWriteFile(path, '{"apiGatew');
@@ -71,8 +73,31 @@ describe('config selection', () => {
71
73
  expect(readJson('dev')).toEqual(DEV);
72
74
  expect((0, fs_1.readdirSync)(configDir).filter((file) => file.endsWith('.tmp'))).toEqual([]);
73
75
  });
76
+ it('should remove a stored API key and keep the org and environment', async () => {
77
+ writeJson('dev', {
78
+ ...DEV,
79
+ apiUserToken: 'old-api-key',
80
+ apiRefreshToken: null,
81
+ signInMethod: 'apiKey',
82
+ });
83
+ config.selectConfigForRun({ requestedName: 'dev', nameInUse: 'dev' });
84
+ await config.clearStoredApiKey();
85
+ expect(readJson('dev')).toEqual(DEV);
86
+ });
87
+ it('should keep the tokens of a config that was not signed in with an API key', async () => {
88
+ const signedInWithSso = {
89
+ ...DEV,
90
+ apiUserToken: 'access',
91
+ apiRefreshToken: 'refresh',
92
+ signInMethod: 'sso',
93
+ };
94
+ writeJson('dev', signedInWithSso);
95
+ config.selectConfigForRun({ requestedName: 'dev', nameInUse: 'dev' });
96
+ await config.clearStoredApiKey();
97
+ expect(readJson('dev')).toEqual(signedInWithSso);
98
+ });
74
99
  it('should keep a saved config readable only by its owner', async () => {
75
- config.selectConfigForRun('dev');
100
+ config.selectConfigForRun({ requestedName: 'dev', nameInUse: 'dev' });
76
101
  await config.storeConfig({ apiUserToken: 'new-token' });
77
102
  expect((0, fs_1.statSync)((0, path_1.join)(configDir, 'dev.json')).mode & 0o777).toBe(0o600);
78
103
  });
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.8.0-alpha.8de44ff",
2
+ "version": "3.8.0-alpha.ca28651",
3
3
  "commands": {
4
4
  "login": {
5
5
  "id": "login",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/cli",
3
- "version": "3.8.0-alpha.8de44ff",
3
+ "version": "3.8.0-alpha.ca28651",
4
4
  "description": "VibeIQ's Contrail Platform CLI.",
5
5
  "bin": {
6
6
  "contrail-cli": "./bin/run",