Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit ceffc45

Browse files
committed
refactor to have config code in config.ts
1 parent 63bb38f commit ceffc45

22 files changed

+190
-161
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ dist
33
package-lock.json
44
.DS_Store
55
.bedrock
6+
.idea
67

78
# Logs
89
logs

src/commands/command.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import commander from "commander";
22
import { enableVerboseLogging, logger } from "../logger";
3-
import { loadConfiguration } from "./init";
43

54
/**
65
* General interface to encapsulate a sub-command.
@@ -107,10 +106,15 @@ export const executeCommand = (cmd: ICommand, argv: string[]): void => {
107106
if (targetCommandName && targetCommand) {
108107
// If command is a sub-command, it needs to load configuration before
109108
// sub-command can be executed.
110-
loadConfiguration();
111109
executeCommand(targetCommand, decrementArgv(argv));
112110
} else {
113-
argv.length <= 2 ? cmd.command.outputHelp() : cmd.command.parse(argv);
111+
// Top level try/catch. If an error occurs, log it and exit with code 1
112+
try {
113+
argv.length <= 2 ? cmd.command.outputHelp() : cmd.command.parse(argv);
114+
} catch (err) {
115+
logger.error(err);
116+
process.exit(1);
117+
}
114118
}
115119
};
116120

src/commands/deployment/dashboard.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as path from "path";
2+
import { Config, loadConfiguration } from "../../config";
23
import { exec } from "../../lib/shell";
34
import {
45
disableVerboseLogging,
56
enableVerboseLogging,
67
logger
78
} from "../../logger";
8-
import { validatePrereqs } from "../infra/vaildate";
9-
import { config, loadConfiguration } from "./../init";
9+
import { validatePrereqs } from "../infra/validate";
1010
import { launchDashboard } from "./dashboard";
1111

1212
beforeAll(() => {
@@ -31,7 +31,7 @@ describe("Validate dashboard container pull", () => {
3131
const dockerId = await exec("docker", [
3232
"images",
3333
"-q",
34-
config.introspection!.dashboard!.image!
34+
Config().introspection!.dashboard!.image!
3535
]);
3636
expect(dockerId).toBeDefined();
3737
expect(dashboardContainerId).not.toBe("");

src/commands/deployment/dashboard.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import commander from "commander";
22
import open = require("open");
3-
import { env } from "shelljs";
3+
import { Config } from "../../config";
44
import { exec } from "../../lib/shell";
55
import { logger } from "../../logger";
6-
import { validatePrereqs } from "../infra/vaildate";
7-
import { config } from "../init";
6+
import { validatePrereqs } from "../infra/validate";
87

98
/**
109
* Adds the onboard command to the commander command object
@@ -17,6 +16,7 @@ export const dashboardCommandDecorator = (command: commander.Command): void => {
1716
.description("Launch the service introspection dashboard")
1817
.option("-p, --port <port>", "Port to launch the dashboard on", 4040)
1918
.action(async opts => {
19+
const config = Config();
2020
if (
2121
!config.introspection ||
2222
!config.azure_devops ||
@@ -44,6 +44,7 @@ export const launchDashboard = async (port: number): Promise<string> => {
4444
if (!(await validatePrereqs(["docker"], false))) {
4545
return "";
4646
}
47+
const config = Config();
4748
const dockerRepository = config.introspection!.dashboard!.image!;
4849
logger.info("Pulling dashboard docker image");
4950
await exec("docker", ["pull", dockerRepository]);
@@ -65,6 +66,7 @@ export const launchDashboard = async (port: number): Promise<string> => {
6566
};
6667

6768
const getEnvVars = (): string[] => {
69+
const config = Config();
6870
const envVars = [];
6971
envVars.push("-e");
7072
envVars.push("REACT_APP_PIPELINE_ORG=" + config.azure_devops!.org!);

src/commands/deployment/get.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import commander from "commander";
33
import Deployment from "spektate/lib/Deployment";
44
import AzureDevOpsPipeline from "spektate/lib/pipeline/AzureDevOpsPipeline";
55
import IPipeline from "spektate/lib/pipeline/Pipeline";
6+
import { Config } from "../../config";
67
import { logger } from "../../logger";
7-
import { config } from "../init";
88
export let hldPipeline: IPipeline;
99
export let clusterPipeline: IPipeline;
1010
export let srcPipeline: IPipeline;
@@ -123,6 +123,7 @@ export const getDeployments = (
123123
service?: string,
124124
deploymentId?: string
125125
): Promise<Deployment[]> => {
126+
const config = Config();
126127
return Deployment.getDeploymentsBasedOnFilters(
127128
config.introspection!.azure!.account_name!,
128129
config.introspection!.azure!.key!,
@@ -151,6 +152,8 @@ export const getDeployments = (
151152
* Initializes the pipelines assuming that the configuration has been loaded
152153
*/
153154
const initialize = () => {
155+
const config = Config();
156+
154157
if (
155158
!config.introspection ||
156159
!config.azure_devops ||

src/commands/deployment/validate.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from "path";
2-
import { config, loadConfiguration } from "./../init";
2+
import { Config, loadConfiguration } from "../../config";
33
import { isValidConfig, isValidStorageAccount } from "./validate";
44

55
import { StorageManagementClient } from "@azure/arm-storage";
@@ -55,7 +55,7 @@ describe("Validate deployment configuration", () => {
5555

5656
describe("Validate missing deployment configuration", () => {
5757
test("no deployment configuration", async () => {
58-
config.introspection = undefined;
58+
Config().introspection = undefined;
5959
const isValid = isValidConfig();
6060

6161
expect(isValid).toBe(false);
@@ -64,7 +64,7 @@ describe("Validate missing deployment configuration", () => {
6464

6565
describe("Validate missing deployment.storage configuration", () => {
6666
test("missing deployment.storage", async () => {
67-
config.introspection!.azure = undefined;
67+
Config().introspection!.azure = undefined;
6868
const isValid = isValidConfig();
6969

7070
expect(isValid).toBe(false);
@@ -73,7 +73,7 @@ describe("Validate missing deployment.storage configuration", () => {
7373

7474
describe("Validate missing deployment.storage configuration", () => {
7575
test("missing deployment.storage.account_name configuration", async () => {
76-
config.introspection!.azure!.account_name = undefined;
76+
Config().introspection!.azure!.account_name = undefined;
7777
const isValid = isValidConfig();
7878

7979
expect(isValid).toBe(false);
@@ -82,7 +82,7 @@ describe("Validate missing deployment.storage configuration", () => {
8282

8383
describe("Validate missing deployment.storage configuration", () => {
8484
test("missing deployment.storage.table_name configuration", async () => {
85-
config.introspection!.azure!.table_name = undefined;
85+
Config().introspection!.azure!.table_name = undefined;
8686
const isValid = isValidConfig();
8787

8888
expect(isValid).toBe(false);
@@ -91,7 +91,7 @@ describe("Validate missing deployment.storage configuration", () => {
9191

9292
describe("Validate missing deployment.storage configuration", () => {
9393
test("missing deployment.storage.partition_key configuration", async () => {
94-
config.introspection!.azure!.partition_key = undefined;
94+
Config().introspection!.azure!.partition_key = undefined;
9595
const isValid = isValidConfig();
9696

9797
expect(isValid).toBe(false);
@@ -100,7 +100,7 @@ describe("Validate missing deployment.storage configuration", () => {
100100

101101
describe("Validate missing deployment.storage configuration", () => {
102102
test("missing deployment.storage.key configuration", async () => {
103-
config.introspection!.azure!.key = undefined;
103+
Config().introspection!.azure!.key = undefined;
104104
const isValid = isValidConfig();
105105

106106
expect(isValid).toBe(false);
@@ -109,7 +109,7 @@ describe("Validate missing deployment.storage configuration", () => {
109109

110110
describe("Validate missing deployment.pipeline configuration", () => {
111111
test("missing deployment.pipeline configuration", async () => {
112-
config.azure_devops = undefined;
112+
Config().azure_devops = undefined;
113113
const isValid = isValidConfig();
114114

115115
expect(isValid).toBe(false);
@@ -118,7 +118,7 @@ describe("Validate missing deployment.pipeline configuration", () => {
118118

119119
describe("Validate missing deployment.pipeline configuration", () => {
120120
test("missing deployment.pipeline.org configuration", async () => {
121-
config.azure_devops!.org = undefined;
121+
Config().azure_devops!.org = undefined;
122122
const isValid = isValidConfig();
123123

124124
expect(isValid).toBe(false);
@@ -127,7 +127,7 @@ describe("Validate missing deployment.pipeline configuration", () => {
127127

128128
describe("Validate missing deployment.pipeline configuration", () => {
129129
test("missing deployment.pipeline.project configuration", async () => {
130-
config.azure_devops!.project = undefined;
130+
Config().azure_devops!.project = undefined;
131131
const isValid = isValidConfig();
132132

133133
expect(isValid).toBe(false);
@@ -136,30 +136,30 @@ describe("Validate missing deployment.pipeline configuration", () => {
136136

137137
describe("Validate storage account", () => {
138138
test("non-existing storage account", async () => {
139-
config.introspection!.azure!.account_name = "non-existing-account-name";
139+
Config().introspection!.azure!.account_name = "non-existing-account-name";
140140
const isValid = await isValidStorageAccount();
141141

142142
expect(isValid).toBe(false);
143143
});
144144

145145
test("existing storage account no keys", async () => {
146-
config.introspection!.azure!.account_name = "epi-test-no-keys";
146+
Config().introspection!.azure!.account_name = "epi-test-no-keys";
147147
const isValid = await isValidStorageAccount();
148148

149149
expect(isValid).toBe(false);
150150
});
151151

152152
test("existing storage account with valid key", async () => {
153-
config.introspection!.azure!.account_name = "epi-test";
154-
config.introspection!.azure!.key = "mock access key2";
153+
Config().introspection!.azure!.account_name = "epi-test";
154+
Config().introspection!.azure!.key = "mock access key2";
155155
const isValid = await isValidStorageAccount();
156156

157157
expect(isValid).toBe(true);
158158
});
159159

160160
test("existing storage account with invalid key", async () => {
161-
config.introspection!.azure!.account_name = "epi-test";
162-
config.introspection!.azure!.key = "mock access key3";
161+
Config().introspection!.azure!.account_name = "epi-test";
162+
Config().introspection!.azure!.key = "mock access key3";
163163
const isValid = await isValidStorageAccount();
164164

165165
expect(isValid).toBe(false);

src/commands/deployment/validate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import commander from "commander";
2+
import { Config } from "../../config";
23
import { storageAccountExists } from "../../lib/azure/storage";
34
import { logger } from "../../logger";
4-
import { config } from "../init";
55

66
/**
77
* Adds the validate command to the commander command object
@@ -23,7 +23,7 @@ export const validateCommandDecorator = (command: commander.Command): void => {
2323
*/
2424
export const isValidConfig = (): boolean => {
2525
const missingConfig = [];
26-
26+
const config = Config();
2727
if (!config.introspection) {
2828
missingConfig.push("introspection");
2929
} else {
@@ -69,6 +69,7 @@ export const isValidConfig = (): boolean => {
6969
* Check is the configured storage account is valid
7070
*/
7171
export const isValidStorageAccount = async (): Promise<boolean> => {
72+
const config = Config();
7273
const isValid = await storageAccountExists(
7374
config.introspection!.azure!.resource_group!,
7475
config.introspection!.azure!.account_name!,

src/commands/hld/pipeline.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import commander from "commander";
2-
31
import { IBuildApi } from "azure-devops-node-api/BuildApi";
4-
import { logger } from "../../logger";
5-
6-
import { config, loadConfiguration } from "../init";
7-
2+
import {
3+
BuildDefinition,
4+
BuildDefinitionVariable
5+
} from "azure-devops-node-api/interfaces/BuildInterfaces";
6+
import commander from "commander";
7+
import { Config } from "../../config";
88
import {
99
createPipelineForDefinition,
1010
definitionForAzureRepoPipeline,
1111
getBuildApiClient,
1212
IAzureRepoPipelineConfig,
1313
queueBuild
1414
} from "../../lib/pipelines/pipelines";
15-
16-
import {
17-
BuildDefinition,
18-
BuildDefinitionVariable
19-
} from "azure-devops-node-api/interfaces/BuildInterfaces";
15+
import { logger } from "../../logger";
2016

2117
export const installHldToManifestPipelineDecorator = (
2218
command: commander.Command
@@ -28,7 +24,7 @@ export const installHldToManifestPipelineDecorator = (
2824
"Install the manifest generation pipeline to your Azure DevOps instance"
2925
)
3026
.action(async () => {
31-
loadConfiguration();
27+
const config = Config();
3228

3329
if (!config) {
3430
logger.error("Config failed to load");

src/commands/infra/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Command } from "../command";
22
import { createCommandDecorator } from "./create";
33
import { scaffoldCommandDecorator } from "./scaffold";
4-
import { validateCommandDecorator } from "./vaildate";
4+
import { validateCommandDecorator } from "./validate";
55

66
export const infraCommand = Command(
77
"infra",

src/commands/infra/validate.test.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import child_process from "child_process";
22
import * as path from "path";
3-
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
4-
import { config, loadConfiguration } from "../init";
3+
import { promisify } from "util";
4+
import { Config, loadConfiguration } from "../../config";
5+
import {
6+
disableVerboseLogging,
7+
enableVerboseLogging,
8+
logger
9+
} from "../../logger";
510
import {
611
validateAzure,
712
validateEnvVariables,
813
validatePrereqs
9-
} from "./vaildate";
14+
} from "./validate";
1015

1116
beforeAll(() => {
1217
enableVerboseLogging();
@@ -37,16 +42,18 @@ describe("Validating executable prerequisites in spk-config", () => {
3742
loadConfiguration(filename);
3843
const fakeBinaries: string[] = ["ydawgie"];
3944
await validatePrereqs(fakeBinaries, true);
40-
expect(config.infra!).toBeDefined();
41-
expect(config.infra!.checks!).toBeDefined();
42-
expect(config.infra!.checks!.ydawgie!).toBe(false);
45+
expect(Config().infra!).toBeDefined();
46+
expect(Config().infra!.checks!).toBeDefined();
47+
expect(Config().infra!.checks!.ydawgie!).toBe(false);
4348
});
4449
});
4550

4651
describe("Validating Azure authentication", () => {
4752
test("Validate that a logged out user produces a force fail", async () => {
4853
// Produce an error that requires user to login
49-
child_process.exec("az logout");
54+
await promisify(child_process.exec)("az logout").catch(err => {
55+
logger.warn(err);
56+
});
5057
const value = await validateAzure(false);
5158
expect(value).toBe(false);
5259
});
@@ -61,9 +68,9 @@ describe("Validating Azure login in spk-config", () => {
6168
process.env.test_key = "my_storage_key";
6269
loadConfiguration(filename);
6370
await validateAzure(true);
64-
expect(config.infra!).toBeDefined();
65-
expect(config.infra!.checks!).toBeDefined();
66-
expect(config.infra!.checks!.az_login_check!).toBe(false);
71+
expect(Config().infra!).toBeDefined();
72+
expect(Config().infra!.checks!).toBeDefined();
73+
expect(Config().infra!.checks!.az_login_check!).toBe(false);
6774
});
6875
});
6976

@@ -87,8 +94,8 @@ describe("Validating environment variables in spk-config", () => {
8794
process.env.test_key = "my_storage_key";
8895
loadConfiguration(filename);
8996
await validateEnvVariables(variables, true);
90-
expect(config.infra!).toBeDefined();
91-
expect(config.infra!.checks!).toBeDefined();
92-
expect(config.infra!.checks!.env_var_check!).toBe(false);
97+
expect(Config().infra!).toBeDefined();
98+
expect(Config().infra!.checks!).toBeDefined();
99+
expect(Config().infra!.checks!.env_var_check!).toBe(false);
93100
});
94101
});

0 commit comments

Comments
 (0)