Skip to content

Commit

Permalink
chore: fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
ohoareau committed Sep 1, 2024
1 parent e3899df commit 3090bf4
Show file tree
Hide file tree
Showing 40 changed files with 596 additions and 261 deletions.
2 changes: 1 addition & 1 deletion src/layer-actions/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'apply', 'plan.tfplan']);
}
};
2 changes: 1 addition & 1 deletion src/layer-actions/clean-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['rm', '-rf', '.terraform/plugins', '.terraform/providers']);
}
};
2 changes: 1 addition & 1 deletion src/layer-actions/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import {layer_run} from '../types';
export default async (run: layer_run) => {
await run(['terraform', 'plan', '-destroy', '-out', 'destroy.tfplan']);
await run(['terraform', 'apply', 'destroy.tfplan']);
}
};
2 changes: 1 addition & 1 deletion src/layer-actions/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'get']);
}
};
34 changes: 17 additions & 17 deletions src/layer-actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export { default as get } from './get';
export { default as init } from './init';
export { default as init_full } from './init-full';
export { default as init_upgrade } from './init-upgrade';
export { default as init_full_upgrade } from './init-full-upgrade';
export { default as providers_lock_delete } from './providers-lock-delete';
export { default as clean_dirs } from './clean-dirs';
export { default as providers_lock } from './providers-lock';
export { default as update } from './update';
export { default as plan } from './plan';
export { default as refresh } from './refresh';
export { default as apply } from './apply';
export { default as output_json } from './output-json';
export { default as output } from './output';
export { default as sync } from './sync';
export { default as sync_full } from './sync-full';
export { default as destroy } from './destroy';
export {default as get} from './get';
export {default as init} from './init';
export {default as init_full} from './init-full';
export {default as init_upgrade} from './init-upgrade';
export {default as init_full_upgrade} from './init-full-upgrade';
export {default as providers_lock_delete} from './providers-lock-delete';
export {default as clean_dirs} from './clean-dirs';
export {default as providers_lock} from './providers-lock';
export {default as update} from './update';
export {default as plan} from './plan';
export {default as refresh} from './refresh';
export {default as apply} from './apply';
export {default as output_json} from './output-json';
export {default as output} from './output';
export {default as sync} from './sync';
export {default as sync_full} from './sync-full';
export {default as destroy} from './destroy';
2 changes: 1 addition & 1 deletion src/layer-actions/init-full-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'init', '-upgrade=true']);
}
};
2 changes: 1 addition & 1 deletion src/layer-actions/init-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'init']);
}
};
24 changes: 19 additions & 5 deletions src/layer-actions/init-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ import {layer_run, loggable, logger_factory, raw_logger} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'init', '-upgrade=true'], createLogger);
}
};

const createLogger:logger_factory = (rawLogger: raw_logger) => {
const createLogger: logger_factory = (rawLogger: raw_logger) => {
let messagesBuffer: (loggable | string)[] = [];
return ({ group, type, data, error }: { group :string; type: string, data: unknown; error?: boolean }) => {
return ({
group,
type,
data,
error,
}: {
group: string;
type: string;
data: unknown;
error?: boolean;
}) => {
switch (type) {
case 'starting':
rawLogger({group, type: 'message', data: 'Re-initializing terraform workspace...'});
rawLogger({
group,
type: 'message',
data: 'Re-initializing terraform workspace...',
});
break;
case 'message':
messagesBuffer.push({group, type, data, error});
Expand All @@ -22,4 +36,4 @@ const createLogger:logger_factory = (rawLogger: raw_logger) => {
break;
}
};
}
};
22 changes: 18 additions & 4 deletions src/layer-actions/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ import {layer_run, loggable, raw_logger} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'init'], createLogger);
}
};
const createLogger = (rawLogger: raw_logger) => {
let messagesBuffer: (loggable | string)[] = [];
return ({ group, type, data, error }: { group :string; type: string, data: unknown; error?: boolean }) => {
return ({
group,
type,
data,
error,
}: {
group: string;
type: string;
data: unknown;
error?: boolean;
}) => {
switch (type) {
case 'starting':
rawLogger({group, type: 'message', data: 'Re-initializing terraform workspace...'});
rawLogger({
group,
type: 'message',
data: 'Re-initializing terraform workspace...',
});
break;
case 'message':
messagesBuffer.push({group, type, data, error});
Expand All @@ -21,4 +35,4 @@ const createLogger = (rawLogger: raw_logger) => {
break;
}
};
}
};
27 changes: 23 additions & 4 deletions src/layer-actions/output-json.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import {layer_run, loggable, logger_factory, raw_logger} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'output', '-json', '-no-color'], createLogger, true);
}
await run(
['terraform', 'output', '-json', '-no-color'],
createLogger,
true,
);
};

const createLogger: logger_factory = (rawLogger: raw_logger) => {
const messagesBuffer: (loggable | string)[] = [];
return ({ group, type, data, error }: { group :string; type: string, data: unknown; error?: boolean }) => {
return ({
group,
type,
data,
error,
}: {
group: string;
type: string;
data: unknown;
error?: boolean;
}) => {
switch (type) {
case 'message':
if (error) {
Expand All @@ -16,7 +30,12 @@ const createLogger: logger_factory = (rawLogger: raw_logger) => {
}
break;
case 'completed':
console.log(JSON.stringify({id: group, variables: JSON.parse(messagesBuffer.join("\n"))}));
console.log(
JSON.stringify({
id: group,
variables: JSON.parse(messagesBuffer.join('\n')),
}),
);
break;
default:
}
Expand Down
2 changes: 1 addition & 1 deletion src/layer-actions/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'output'], undefined, true);
}
};
2 changes: 1 addition & 1 deletion src/layer-actions/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'plan', '-out', 'plan.tfplan']);
}
};
2 changes: 1 addition & 1 deletion src/layer-actions/providers-lock-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['rm', '-f', '.terraform.lock.hcl']);
}
};
12 changes: 10 additions & 2 deletions src/layer-actions/providers-lock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'providers', 'lock', '-platform=darwin_amd64', '-platform=linux_amd64', '-platform=windows_amd64', '-platform=darwin_arm64']);
}
await run([
'terraform',
'providers',
'lock',
'-platform=darwin_amd64',
'-platform=linux_amd64',
'-platform=windows_amd64',
'-platform=darwin_arm64',
]);
};
2 changes: 1 addition & 1 deletion src/layer-actions/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'refresh']);
}
};
2 changes: 1 addition & 1 deletion src/layer-actions/sync-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import {layer_run} from '../types';
export default async (run: layer_run) => {
await run(['terraform', 'plan', '-out', 'plan.tfplan']);
await run(['terraform', 'apply', 'plan.tfplan']);
}
};
54 changes: 43 additions & 11 deletions src/layer-actions/sync.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
import {layer_run, loggable} from '../types';
import rawLogger from "../utils/rawLogger";
import rawLogger from '../utils/rawLogger';

export default async (run: layer_run) => {
let needApply: boolean | undefined;
const createLogger = () => {
let messagesBuffer: (loggable | string)[] = [];
return ({ group, type, data, error }: { group :string; type: string, data: unknown; error?: boolean }) => {
return ({
group,
type,
data,
error,
}: {
group: string;
type: string;
data: unknown;
error?: boolean;
}) => {
switch (type) {
case 'starting':
rawLogger({group, type: 'message', data: 'Planning changes...'});
rawLogger({
group,
type: 'message',
data: 'Planning changes...',
});
break;
case 'message':
if (undefined === needApply) {
if (/ 0 to add, 0 to change, 0 to destroy/.test(data as string)) {
if (
/ 0 to add, 0 to change, 0 to destroy/.test(
data as string,
)
) {
needApply = false;
} else if (/To perform exactly these actions, run the following command to apply/.test(data as string)) {
} else if (
/To perform exactly these actions, run the following command to apply/.test(
data as string,
)
) {
needApply = true;
messagesBuffer.forEach(m => rawLogger(m as loggable));
messagesBuffer.forEach(m =>
rawLogger(m as loggable),
);
messagesBuffer = [];
} else if (/No changes. Infrastructure is up-to-date./.test(data as string)) {
} else if (
/No changes. Infrastructure is up-to-date./.test(
data as string,
)
) {
needApply = false;
}
}
Expand All @@ -35,15 +63,19 @@ export default async (run: layer_run) => {
break;
case 'completed':
if (!needApply) {
rawLogger({group, type: 'message', data: 'No changes detected, skipping.'})
rawLogger({
group,
type: 'message',
data: 'No changes detected, skipping.',
});
} else {
messagesBuffer.forEach(m => rawLogger(m as loggable));
messagesBuffer = [];
}
break;
}
};
}
};
await run(['terraform', 'plan', '-out', 'plan.tfplan'], createLogger);
needApply && await run(['terraform', 'apply', 'plan.tfplan']);
}
needApply && (await run(['terraform', 'apply', 'plan.tfplan']));
};
2 changes: 1 addition & 1 deletion src/layer-actions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import {layer_run} from '../types';

export default async (run: layer_run) => {
await run(['terraform', 'get', '-update']);
}
};
40 changes: 34 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,45 @@ export type layer_config = {
};

export type loggable = {
group :string;
type: string,
group: string;
type: string;
data: unknown;
error?: boolean;
};

export type raw_logger = ({group, type, data, error}: { group: string; type: string; data: unknown; error?: boolean }) => void;
export type logger = ({group, type, data, error}: { group: string; type: string; data: unknown; error?: boolean }) => void;
export type raw_logger = ({
group,
type,
data,
error,
}: {
group: string;
type: string;
data: unknown;
error?: boolean;
}) => void;
export type logger = ({
group,
type,
data,
error,
}: {
group: string;
type: string;
data: unknown;
error?: boolean;
}) => void;
export type logger_factory = (rawLogger: raw_logger) => logger;

export type layer_run = (args: string[], loggerFactory?: logger_factory, silent?: boolean) => Promise<void>;
export type layer_run = (
args: string[],
loggerFactory?: logger_factory,
silent?: boolean,
) => Promise<void>;
export type layer_action = (run: layer_run, layer: layer) => Promise<void>;

export type fetch_layer = (root: string, env: string, name: string) => Promise<[ Record<string, string>, string ]>;
export type fetch_layer = (
root: string,
env: string,
name: string,
) => Promise<[Record<string, string>, string]>;
Loading

0 comments on commit 3090bf4

Please sign in to comment.