Skip to content

Commit ea0b13a

Browse files
committed
Move license-related code to a separate module
1 parent 019df5f commit ea0b13a

File tree

4 files changed

+46
-40
lines changed

4 files changed

+46
-40
lines changed

src/plugins/setup.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { commands, ProgressLocation, window } from "vscode";
2-
import type { LogOutputChannel } from "vscode";
32

43
import { createPlugin } from "../plugins.ts";
54
import {
65
checkIsAuthenticated,
7-
checkIsLicenseValid,
86
requestAuthentication,
97
saveAuthToken,
108
} from "../utils/authenticate.ts";
11-
import { execLocalStack } from "../utils/cli.ts";
129
import { configureAwsProfiles } from "../utils/configure-aws.ts";
1310
import { runInstallProcess } from "../utils/install.ts";
11+
import {
12+
activateLicense,
13+
checkIsLicenseValid,
14+
activateLicenseUntilValid,
15+
} from "../utils/license.ts";
1416
import { minDelay } from "../utils/promises.ts";
1517

1618
export default createPlugin(
@@ -172,9 +174,7 @@ export default createPlugin(
172174
// then there will be no license info to be reported by `localstack license info`.
173175
// Also, an expired license could be cached.
174176
// Activating the license pre-emptively to know its state during the setup process.
175-
await execLocalStack(["license", "activate"], {
176-
outputChannel,
177-
});
177+
await activateLicense(outputChannel);
178178
const licenseIsValid = await minDelay(
179179
checkIsLicenseValid(outputChannel),
180180
);
@@ -190,7 +190,7 @@ export default createPlugin(
190190

191191
commands.executeCommand("localstack.openLicensePage");
192192

193-
await checkLicenseUntilValid(outputChannel);
193+
await activateLicenseUntilValid(outputChannel);
194194
}
195195
//TODO add telemetry
196196

@@ -252,19 +252,3 @@ export default createPlugin(
252252
}
253253
},
254254
);
255-
256-
async function checkLicenseUntilValid(
257-
outputChannel: LogOutputChannel,
258-
): Promise<void> {
259-
while (true) {
260-
const licenseIsValid = await checkIsLicenseValid(outputChannel);
261-
if (licenseIsValid) {
262-
break;
263-
}
264-
await execLocalStack(["license", "activate"], {
265-
outputChannel,
266-
});
267-
// Wait before trying again
268-
await new Promise((resolve) => setTimeout(resolve, 1000));
269-
}
270-
}

src/utils/authenticate.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
import { env, Uri, window } from "vscode";
1111

1212
import { assertIsError } from "./assert.ts";
13-
import { execLocalStack } from "./cli.ts";
1413

1514
/**
1615
* Registers a {@link UriHandler} that waits for an authentication token from the browser,
@@ -113,21 +112,6 @@ export async function saveAuthToken(
113112
}
114113
}
115114

116-
const LICENSE_VALIDITY_MARKER = "license validity: valid";
117-
118-
export async function checkIsLicenseValid(outputChannel: LogOutputChannel) {
119-
try {
120-
const licenseInfoResponse = await execLocalStack(["license", "info"], {
121-
outputChannel,
122-
});
123-
return licenseInfoResponse.stdout.includes(LICENSE_VALIDITY_MARKER);
124-
} catch (error) {
125-
outputChannel.error(error instanceof Error ? error : String(error));
126-
127-
return false;
128-
}
129-
}
130-
131115
/**
132116
* Checks if the user is authenticated by validating the stored auth token.
133117
*

src/utils/license.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { LogOutputChannel } from "vscode";
2+
3+
import { execLocalStack } from "./cli.ts";
4+
5+
const LICENSE_VALIDITY_MARKER = "license validity: valid";
6+
7+
export async function checkIsLicenseValid(outputChannel: LogOutputChannel) {
8+
try {
9+
const licenseInfoResponse = await execLocalStack(["license", "info"], {
10+
outputChannel,
11+
});
12+
return licenseInfoResponse.stdout.includes(LICENSE_VALIDITY_MARKER);
13+
} catch (error) {
14+
outputChannel.error(error instanceof Error ? error : String(error));
15+
16+
return false;
17+
}
18+
}
19+
20+
export async function activateLicense(outputChannel: LogOutputChannel) {
21+
await execLocalStack(["license", "activate"], {
22+
outputChannel,
23+
});
24+
}
25+
26+
export async function activateLicenseUntilValid(
27+
outputChannel: LogOutputChannel,
28+
): Promise<void> {
29+
while (true) {
30+
const licenseIsValid = await checkIsLicenseValid(outputChannel);
31+
if (licenseIsValid) {
32+
break;
33+
}
34+
await activateLicense(outputChannel);
35+
// Wait before trying again
36+
await new Promise((resolve) => setTimeout(resolve, 1000));
37+
}
38+
}

src/utils/manage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { v7 as uuidv7 } from "uuid";
22
import type { ExtensionContext, LogOutputChannel, MessageItem } from "vscode";
33
import { commands, env, Uri, window } from "vscode";
44

5-
import { checkIsLicenseValid } from "./authenticate.ts";
65
import { spawnLocalStack } from "./cli.ts";
76
import { exec } from "./exec.ts";
7+
import { checkIsLicenseValid } from "./license.ts";
88
import type { Telemetry } from "./telemetry.ts";
99

1010
export type LocalstackStatus = "running" | "starting" | "stopping" | "stopped";

0 commit comments

Comments
 (0)