Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (re)release process-templates flag override input #60

Merged
merged 11 commits into from
Aug 28, 2024
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ inputs:
settings:
description: The settings to extract.
required: false
process-templates:
description: "Enable/disable processing of Go templates in Atmos stacks manifests."
required: false
default: "true"
outputs:
value:
description: "The value of the setting when a single setting is returned."
Expand Down
8 changes: 8 additions & 0 deletions atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ logs:
verbose: false
colors: true

templates:
settings:
enabled: true
sprig:
enabled: true
gomplate:
enabled: true

# Custom CLI commands
commands:
- name: tf
Expand Down
21 changes: 11 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28877,9 +28877,9 @@ exports.NEVER = parseUtil_1.INVALID;
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.runAtmosDescribeComponent = void 0;
const node_child_process_1 = __nccwpck_require__(7718);
const runAtmosDescribeComponent = async (component, stack, cwd) => {
const runAtmosDescribeComponent = async (component, stack, processTemplates, cwd) => {
const options = cwd ? { cwd } : {};
const command = `atmos describe component ${component} -s ${stack} --format=json`;
const command = `atmos describe component ${component} -s ${stack} --format=json --process-templates=${String(processTemplates)}`;
const atmos = (0, node_child_process_1.execSync)(command, options);
return atmos.toString();
};
Expand Down Expand Up @@ -28944,8 +28944,8 @@ exports.SettingInput = zod_1.z.object({
outputPath: zod_1.z.string().trim().min(1)
});
exports.SettingsInput = zod_1.z.array(exports.SettingInput).min(1);
const getSetting = async (component, stack, settingsPath) => {
const cmdOutput = await (0, atmos_1.runAtmosDescribeComponent)(component, stack);
const getSetting = async (component, stack, settingsPath, processTemplates) => {
const cmdOutput = await (0, atmos_1.runAtmosDescribeComponent)(component, stack, processTemplates);
const json = JSON.parse(cmdOutput);
return (0, exports.getNestedValue)(json, settingsPath);
};
Expand Down Expand Up @@ -28987,8 +28987,9 @@ const core = __importStar(__nccwpck_require__(2186));
const _useCase_1 = __nccwpck_require__(9264);
(async () => {
try {
const singleResult = await (0, _useCase_1.processSingleSetting)();
const multipleResult = await (0, _useCase_1.processMultipleSettings)();
const processTemplates = core.getBooleanInput("process-templates");
const singleResult = await (0, _useCase_1.processSingleSetting)(processTemplates);
const multipleResult = await (0, _useCase_1.processMultipleSettings)(processTemplates);
if (singleResult || multipleResult) {
core.info("result returned successfully");
}
Expand Down Expand Up @@ -29065,7 +29066,7 @@ exports.processMultipleSettings = void 0;
const core = __importStar(__nccwpck_require__(2186));
const _lib_1 = __nccwpck_require__(6791);
const YAML = __importStar(__nccwpck_require__(4083));
const processMultipleSettings = async () => {
const processMultipleSettings = async (processTemplates) => {
const settingsInput = core.getInput("settings");
if (settingsInput) {
const yaml = YAML.parse(settingsInput);
Expand All @@ -29075,7 +29076,7 @@ const processMultipleSettings = async () => {
const output = await settings.reduce(async (accPromise, item) => {
const acc = await accPromise;
const { outputPath, ...rest } = item;
const result = await (0, _lib_1.getSetting)(item.component, item.stack, item.settingsPath);
const result = await (0, _lib_1.getSetting)(item.component, item.stack, item.settingsPath, processTemplates);
return { ...acc, [outputPath]: result };
}, Promise.resolve({}));
core.setOutput("settings", JSON.stringify(output));
Expand Down Expand Up @@ -29121,7 +29122,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.processSingleSetting = void 0;
const core = __importStar(__nccwpck_require__(2186));
const _lib_1 = __nccwpck_require__(6791);
const processSingleSetting = async () => {
const processSingleSetting = async (processTemplates) => {
const component = core.getInput("component");
const stack = core.getInput("stack");
const settingsPath = core.getInput("settings-path");
Expand All @@ -29132,7 +29133,7 @@ const processSingleSetting = async () => {
};
const parseResult = _lib_1.SingleSettingInput.safeParse(singleSetting);
if (parseResult.success) {
const value = await (0, _lib_1.getSetting)(parseResult.data.component, parseResult.data.stack, parseResult.data["settings-path"]);
const value = await (0, _lib_1.getSetting)(parseResult.data.component, parseResult.data.stack, parseResult.data["settings-path"], processTemplates);
core.setOutput("value", value);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/lib/atmos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { runAtmosDescribeComponent } from "./atmos";

describe("runAtmosDescribeComponent", () => {
it("should return a string", async () => {
const result = JSON.parse(await runAtmosDescribeComponent("foo", "core-ue1-dev"));
const result = JSON.parse(await runAtmosDescribeComponent("foo", "core-ue1-dev", true));
expect(result.atmos_component).toEqual("foo");
expect(result.atmos_stack).toEqual("core-ue1-dev");
});
Expand Down
5 changes: 2 additions & 3 deletions src/lib/atmos.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { execSync } from "node:child_process";

export const runAtmosDescribeComponent = async (component: string, stack: string, cwd?: string) => {
export const runAtmosDescribeComponent = async (component: string, stack: string, processTemplates: boolean, cwd?: string) => {
const options = cwd ? { cwd } : {};

const command = `atmos describe component ${component} -s ${stack} --format=json`;
const command = `atmos describe component ${component} -s ${stack} --format=json --process-templates=${String(processTemplates)}`;
const atmos = execSync(command, options);
return atmos.toString();
};
4 changes: 2 additions & 2 deletions src/lib/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe("getSingleSetting", () => {
const settingValue = await getSetting(
"foo",
"core-ue1-dev",
"atmos_cli_config.components.terraform.base_path"
);
"atmos_cli_config.components.terraform.base_path",
true);
expect(settingValue).toEqual("components/terraform");
});
});
5 changes: 3 additions & 2 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export type SettingsInput = z.infer<typeof SettingsInput>;
export const getSetting = async (
component: string,
stack: string,
settingsPath: string
settingsPath: string,
processTemplates: boolean
) => {
const cmdOutput = await runAtmosDescribeComponent(component, stack);
const cmdOutput = await runAtmosDescribeComponent(component, stack, processTemplates);
const json = JSON.parse(cmdOutput);

return getNestedValue(json, settingsPath);
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { processMultipleSettings, processSingleSetting } from "@useCase";

(async () => {
try {
const singleResult = await processSingleSetting();
const multipleResult = await processMultipleSettings();
const processTemplates = core.getBooleanInput("process-templates");
const singleResult = await processSingleSetting(processTemplates);
const multipleResult = await processMultipleSettings(processTemplates);

if (singleResult || multipleResult) {
core.info("result returned successfully");
Expand Down
2 changes: 1 addition & 1 deletion src/useCase/process-multiple-settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("multipleSettings", () => {
});

it("should return an object", async () => {
const result = await processMultipleSettings();
const result = await processMultipleSettings(true);
expect(outputs["settings"]).toEqual(
'{"prop1":"components/terraform","secretArn":"arn:aws:secretsmanager:us-east-1:000000000000:secret:MySecret-PlMes3"}'
);
Expand Down
6 changes: 3 additions & 3 deletions src/useCase/process-multiple-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from "@actions/core";
import { getSetting, SettingsInput } from "@lib";
import * as YAML from "yaml";

export const processMultipleSettings = async () => {
export const processMultipleSettings = async (processTemplates: boolean) => {
const settingsInput = core.getInput("settings");

if (settingsInput) {
Expand All @@ -18,8 +18,8 @@ export const processMultipleSettings = async () => {
const result = await getSetting(
item.component,
item.stack,
item.settingsPath
);
item.settingsPath,
processTemplates);
return { ...acc, [outputPath]: result };
}, Promise.resolve({}));

Expand Down
80 changes: 78 additions & 2 deletions src/useCase/process-single-setting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("singleSetting", () => {
const mockValues: any = {
component: "foo",
stack: "core-ue1-dev",
"settings-path": "atmos_cli_config.components.terraform.base_path"
"settings-path": "atmos_cli_config.components.terraform.base_path",
};

jest
Expand All @@ -35,7 +35,83 @@ describe("singleSetting", () => {
});

it("should return a value", async () => {
const result = await processSingleSetting();
const result = await processSingleSetting(true);
expect(outputs["value"]).toEqual("components/terraform");
});
});

describe("singleSettingWithTemplatesEnabled", () => {
let outputs: any = {};

beforeEach(() => {
outputs = {};
const mockValues: any = {
component: "hello",
stack: "core-ue1-dev",
"settings-path": "settings.level1.example",
};

jest
.spyOn(core, "getInput")
.mockImplementation(
(name: string, options?: core.InputOptions | undefined) => {
return mockValues[name];
}
);

jest
.spyOn(core, "setOutput")
.mockImplementation((name: string, value: any) => {
outputs[name] = value;
});
});

afterEach(() => {
jest.resetAllMocks();
});

it("should return a templated value", async () => {
const result = await processSingleSetting(true);
expect(outputs["value"]).toEqual(
"core-ue1-dev"
);
});
});

describe("singleSettingWithTemplatesDisabled", () => {
let outputs: any = {};

beforeEach(() => {
outputs = {};
const mockValues: any = {
component: "hello",
stack: "core-ue1-dev",
"settings-path": "settings.level1.example"
};

jest
.spyOn(core, "getInput")
.mockImplementation(
(name: string, options?: core.InputOptions | undefined) => {
return mockValues[name];
}
);

jest
.spyOn(core, "setOutput")
.mockImplementation((name: string, value: any) => {
outputs[name] = value;
});
});

afterEach(() => {
jest.resetAllMocks();
});

it("should return a template placeholder", async () => {
const result = await processSingleSetting(false);
expect(outputs["value"]).toEqual(
"{{ (printf \"%s-%s-%s\" .vars.tenant .vars.environment .vars.stage) }}"
);
});
});
6 changes: 3 additions & 3 deletions src/useCase/process-single-setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from "@actions/core";
import { getSetting, SingleSettingInput } from "@lib";
import * as YAML from "yaml";

export const processSingleSetting = async () => {
export const processSingleSetting = async (processTemplates: boolean) => {
const component = core.getInput("component");
const stack = core.getInput("stack");
const settingsPath = core.getInput("settings-path");
Expand All @@ -19,8 +19,8 @@ export const processSingleSetting = async () => {
const value = await getSetting(
parseResult.data.component,
parseResult.data.stack,
parseResult.data["settings-path"]
);
parseResult.data["settings-path"],
processTemplates);
core.setOutput("value", value);
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions stacks/orgs/demo/dev/templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
components:
terraform:
hello:
settings:
level1:
example: '{{ (printf "%s-%s-%s" .vars.tenant .vars.environment .vars.stage) }}'
vars:
namespace: demo
tenant: core
environment: ue1
stage: dev
hello: world
Loading