-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
423 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { writeFile } from "fs/promises"; | ||
import { join } from "path"; | ||
import { BuiltinPlatform } from "@winglang/compiler"; | ||
import { describe, expect, test, vitest, beforeEach, afterEach, vi } from "vitest"; | ||
import { secrets } from "../commands/secrets"; | ||
import { generateTmpDir } from "../util"; | ||
|
||
vitest.mock("inquirer"); | ||
|
||
describe("secrets", () => { | ||
let log: any; | ||
|
||
beforeEach(() => { | ||
log = console.log; | ||
console.log = vi.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
console.log = log; | ||
}); | ||
|
||
test("no secrets found", async () => { | ||
const workdir = await generateTmpDir(); | ||
process.chdir(workdir); | ||
|
||
const wingCode = ` | ||
bring cloud; | ||
`; | ||
|
||
await writeFile(join(workdir, "main.w"), wingCode); | ||
|
||
await secrets("main.w", { | ||
platform: [BuiltinPlatform.SIM], | ||
targetDir: workdir, | ||
}); | ||
|
||
expect(console.log).toHaveBeenCalledWith("0 secret(s) found\n"); | ||
}); | ||
|
||
test("secrets found", async () => { | ||
const workdir = await generateTmpDir(); | ||
process.chdir(workdir); | ||
|
||
const wingCode = ` | ||
bring cloud; | ||
let s1 = new cloud.Secret(name: "my-secret") as "s1"; | ||
let s2 = new cloud.Secret(name: "other-secret") as "s2"; | ||
`; | ||
|
||
await writeFile(join(workdir, "main.w"), wingCode); | ||
|
||
await secrets("main.w", { | ||
platform: [BuiltinPlatform.SIM], | ||
targetDir: workdir, | ||
list: true, | ||
}); | ||
|
||
expect(console.log).toHaveBeenCalledWith("2 secret(s) found\n"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { PlatformManager, SECRETS_FILE_NAME } from "@winglang/sdk/lib/platform"; | ||
import inquirer from "inquirer"; | ||
import { CompileOptions, compile } from "./compile"; | ||
|
||
export interface SecretsOptions extends CompileOptions { | ||
readonly list?: boolean; | ||
} | ||
|
||
export async function secrets(entrypoint?: string, options?: SecretsOptions): Promise<void> { | ||
// Compile the program to generate secrets file | ||
const outdir = await compile(entrypoint, options); | ||
const secretsFile = path.join(outdir, SECRETS_FILE_NAME); | ||
|
||
let secretNames = fs.existsSync(secretsFile) | ||
? JSON.parse(fs.readFileSync(secretsFile, "utf-8")) | ||
: []; | ||
|
||
process.env.WING_SOURCE_DIR = path.resolve(path.dirname(entrypoint ?? "main.w")); | ||
|
||
let secretValues: Record<string, string> = {}; | ||
console.log(`${secretNames.length} secret(s) found\n`); | ||
|
||
if (options?.list) { | ||
console.log("- " + secretNames.join("\n- ")); | ||
return; | ||
} | ||
|
||
if (secretNames.length === 0) { | ||
return; | ||
} | ||
|
||
for (const secret of secretNames) { | ||
const response = await inquirer.prompt([ | ||
{ | ||
type: "password", | ||
name: "value", | ||
message: `Enter the secret value for ${secret}:`, | ||
}, | ||
]); | ||
secretValues[secret] = response.value; | ||
} | ||
|
||
const plaformManager = new PlatformManager({ platformPaths: options?.platform }); | ||
await plaformManager.storeSecrets(secretValues); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.