LocalWorkspace list stacks and current stack

This commit is contained in:
evanboyle 2020-09-19 14:07:02 -07:00 committed by Evan Boyle
parent 0420d38354
commit 6a29404db2
3 changed files with 43 additions and 15 deletions

View file

@ -111,6 +111,28 @@ describe("LocalWorkspace", () => {
await ws.removeStack(stackName);
}));
it(`can list stacks and currently selected stack`, asyncTest(async () => {
const projectSettings = new ProjectSettings();
projectSettings.name = `node_list_test${getTestSuffix()}`;
projectSettings.runtime.name = "nodejs";
const ws = new LocalWorkspace({ projectSettings });
await ws.ready;
const stackNamer = () => `int_test${getTestSuffix()}`;
const stackNames: string[] = [];
for (let i = 0; i < 2; i++) {
const stackName = stackNamer();
stackNames[i] = stackName;
const _ = await Stack.Create(stackName, ws);
const stackSummary = await ws.stack();
assert.equal(stackSummary?.current, true);
const stacks = await ws.listStacks();
assert.equal(stacks.length, i + 1);
}
for (const name of stackNames) {
await ws.removeStack(name);
}
}));
});
const getTestSuffix = () => {

View file

@ -15,6 +15,7 @@
import * as fs from "fs";
import * as os from "os";
import * as upath from "upath";
import { CommandResult, runPulumiCmd } from "./cmd";
import { ConfigMap, ConfigValue } from "./config";
import { ProjectSettings } from "./projectSettings";
@ -217,18 +218,23 @@ export class LocalWorkspace implements Workspace {
getPulumiHome(): string | undefined {
return this.pulumiHome;
}
whoAmI(): Promise<string> {
// TODO
return Promise.resolve(<any>{});
async whoAmI(): Promise<string> {
const result = await this.runPulumiCmd(["whoami"]);
return Promise.resolve(result.stdout.trim());
}
stack(): Promise<string> {
// TODO
return Promise.resolve(<any>{});
async stack(): Promise<StackSummary | undefined> {
const stacks = await this.listStacks();
for (const stack of stacks) {
if (stack.current) {
return Promise.resolve(stack);
}
}
return Promise.resolve(undefined);
}
listStacks(): Promise<StackSummary[]> {
// TODO
return Promise.resolve(<any>{});
async listStacks(): Promise<StackSummary[]> {
const result = await this.runPulumiCmd(["stack", "ls", "--json"]);
const stacks: StackSummary[] = JSON.parse(result.stdout);
return Promise.resolve(stacks);
}
getProgram(): (() => void) | undefined {
return this.program;
@ -246,9 +252,8 @@ export class LocalWorkspace implements Workspace {
}
private async runPulumiCmd(
args: string[],
onOutput?: (data: string) => void,
): Promise<CommandResult> {
return runPulumiCmd(args, this.workDir, {});
return runPulumiCmd(args, this.workDir, this.getEnvVars());
}
}

View file

@ -37,20 +37,21 @@ export interface Workspace {
getWorkDir(): string;
getPulumiHome(): string | undefined;
whoAmI(): Promise<string>;
stack(): Promise<string>;
stack(): Promise<StackSummary | undefined>;
createStack(stackName: string): Promise<void>;
selectStack(stackName: string): Promise<void>;
removeStack(stackName: string): Promise<void>;
listStacks(): Promise<StackSummary[]>;
getProgram(): (() => void) | undefined;
setProgram(program: () => void): void;
// TODO import/export
}
export type StackSummary = {
name: string,
current: boolean,
lastUpdate: string,
lastUpdate?: string,
updateInProgress: boolean,
resourceCount?: number,
url: string,
url?: string,
};