diff --git a/src/tools/env-variables-converter/env-variables-converter.vue b/src/tools/env-variables-converter/env-variables-converter.vue new file mode 100644 index 00000000..14762b2b --- /dev/null +++ b/src/tools/env-variables-converter/env-variables-converter.vue @@ -0,0 +1,62 @@ + + + diff --git a/src/tools/env-variables-converter/index.ts b/src/tools/env-variables-converter/index.ts new file mode 100644 index 00000000..33dd8b44 --- /dev/null +++ b/src/tools/env-variables-converter/index.ts @@ -0,0 +1,13 @@ +import { Terminal } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Env Variables Format Converter', + path: '/env-variables-converter', + description: 'Convert between common env vars format and yaml', + keywords: ['env', 'environment', 'variables', 'yaml', 'converter'], + component: () => import('./env-variables-converter.vue'), + icon: Terminal, + createdAt: new Date('2024-08-15'), + category: 'Converters', +}); diff --git a/src/tools/env-variables-converter/lib/deflate.js b/src/tools/env-variables-converter/lib/deflate.js new file mode 100644 index 00000000..c9a366dd --- /dev/null +++ b/src/tools/env-variables-converter/lib/deflate.js @@ -0,0 +1,37 @@ +// https://github.com/jusufazer/yaml2properties/blob/master/src/scripts/parser.js +function deflate(json, prefix) { + let result = []; + const keys = Object.keys(json); + keys.forEach((key) => { + let _prefix; + + let _currPrefix = key; + if (isNumeric(key)) { + _currPrefix = `[${_currPrefix}]`; + } + + if (typeof json[key] === 'object') { + if (!Array.isArray(json[key])) { + _currPrefix = _currPrefix.concat('.'); + } + _prefix = prefix ? prefix.concat(_currPrefix) : _currPrefix; + result = result.concat(deflate(json[key], _prefix)); + } + else { + _prefix = prefix ? prefix.concat(_currPrefix) : _currPrefix; + result.push(_prefix.concat('=').concat(json[key])); + } + }); + + return result; +} + +function isNumeric(str) { + if (typeof str != 'string') { + return false; + } // we only process strings! + return !Number.isNaN(Number(str)) // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)... + && !Number.isNaN(Number.parseFloat(str)); // ...and ensure strings of whitespace fail +}; + +export default deflate; diff --git a/src/tools/env-variables-converter/lib/deflate.test.js b/src/tools/env-variables-converter/lib/deflate.test.js new file mode 100644 index 00000000..864e04ee --- /dev/null +++ b/src/tools/env-variables-converter/lib/deflate.test.js @@ -0,0 +1,37 @@ +import { describe, expect } from 'vitest'; +import deflate from './deflate'; + +describe('deflates json', () => { + expect(deflate({ 'foo-bar': { baz: [{ biz: ['value1', 'value2'] }] } }, '')) + .toStrictEqual(['foo-bar.baz[0].biz[0]=value1', 'foo-bar.baz[0].biz[1]=value2']); +}); + +describe('deflates large json', () => { + expect(deflate({ + cloud: { + gateway: { + routes: [ + { + id: 'after_route', + uri: 'https://example.org', + predicates: [ + { + name: 'Cookie', + args: { + name: 'mycookie', + regexp: 'mycookievalue', + }, + }, + ], + }, + ], + }, + }, + }, '')) + .toStrictEqual( + ['cloud.gateway.routes[0].id=after_route', + 'cloud.gateway.routes[0].uri=https://example.org', + 'cloud.gateway.routes[0].predicates[0].name=Cookie', + 'cloud.gateway.routes[0].predicates[0].args.name=mycookie', + 'cloud.gateway.routes[0].predicates[0].args.regexp=mycookievalue']); +}); diff --git a/src/tools/env-variables-converter/lib/formats.ts b/src/tools/env-variables-converter/lib/formats.ts new file mode 100644 index 00000000..62c896fb --- /dev/null +++ b/src/tools/env-variables-converter/lib/formats.ts @@ -0,0 +1,51 @@ +export enum Formats { + YAML = 'Spring Boot YAML', + PROPERTIES = 'Spring Boot Properties', + SIMPLE = 'Simple Environment Variables', + TERMINAL = 'Terminal Environment Variables', + KUBERNETES = 'Kubernetes Environment Variables', +} + +export enum ModelType { + ENV, + FLAT, +} + +export interface Defaults { + modelType: ModelType + outputType: string + inputValue: string +} + +export interface Model { + inputModelType?: ModelType + value?: string +} + +export const DefaultsMap = new Map([ + [Formats.YAML, { + modelType: ModelType.FLAT, + outputType: Formats.SIMPLE, + inputValue: 'foo-bar:\n baz:\n - value1\n - value2\n enabled: true\nabcDef: value3', + }], + [Formats.PROPERTIES, { + modelType: ModelType.FLAT, + outputType: Formats.SIMPLE, + inputValue: 'foo-bar.baz[0]=value1\nfoo-bar.baz[1]=value2\nfoo-bar.enabled=true\nabcDef=value3', + }], + [Formats.SIMPLE, { + modelType: ModelType.ENV, + outputType: Formats.KUBERNETES, + inputValue: 'FOOBAR_BAZ_0_=value1\nFOOBAR_BAZ_1_=value2\nFOOBAR_ENABLED=true\nABCDEF=value3', + }], + [Formats.TERMINAL, { + modelType: ModelType.ENV, + outputType: Formats.SIMPLE, + inputValue: 'FOOBAR_BAZ_0_=value1 FOOBAR_BAZ_1_=value2 FOOBAR_ENABLED=true ABCDEF=value3', + }], + [Formats.KUBERNETES, { + modelType: ModelType.ENV, + outputType: Formats.SIMPLE, + inputValue: '- name: FOOBAR_BAZ_0_\n value: value1\n- name: FOOBAR_BAZ_1_\n value: value2\n- name: FOOBAR_ENABLED\n value: true\n- name: ABCDEF\n value: value3', + }], +]); diff --git a/src/tools/env-variables-converter/lib/inputHandler.test.js b/src/tools/env-variables-converter/lib/inputHandler.test.js new file mode 100644 index 00000000..42825774 --- /dev/null +++ b/src/tools/env-variables-converter/lib/inputHandler.test.js @@ -0,0 +1,60 @@ +import { describe, expect } from 'vitest'; +import { Formats } from '../components/home/main/Main'; +import inputHandler from './inputHandler'; + +describe('simplifies input', () => { + const input = ` - name: SPRING_CACHE_TYPE + value: redis + - name: SPRING_REDIS_SSL + value: true`; + expect(inputHandler(Formats.KUBERNETES, input)) + .toStrictEqual({ SPRING_CACHE_TYPE: 'redis', SPRING_REDIS_SSL: 'true' }); +}); + +// https://stackoverflow.com/a/21699210/1098564 +describe('simplifies input quotations', () => { + const input = ` + - name: SPRING_CACHE_TYPE + value: redis + - name: >- + SPRING_REDIS_SSL + value: don't + - name: >- + SPRING_REDIS_SSL1 + value: "don't" + - name: >- + SPRING_REDIS_SSL2 + value: >- + don't + - name: >- + SPRING_REDIS_SSL3 + value: 'redis'`; + + expect(inputHandler(Formats.KUBERNETES, input)) + .toStrictEqual({ SPRING_CACHE_TYPE: 'redis', SPRING_REDIS_SSL: 'don\'t', SPRING_REDIS_SSL1: 'don\'t', SPRING_REDIS_SSL2: 'don\'t', SPRING_REDIS_SSL3: 'redis' }); +}); + +// // https://stackoverflow.com/a/21699210/1098564 +// test('block style chomp strip', () => { +// const input = ` +// - name: SOME_KEY +// value: >- +// very "long" +// 'string' with + +// paragraph gap, \n and +// spaces. +// ` +// expect(inputHandler(Formats.KUBERNETES, input)) +// .toStrictEqual({"SOME_KEY": "very \"long\" 'string' with\nparagraph gap, \\n and spaces."}); +// }); + +describe('simplifies input properties quotes', () => { + const input = `foo-bar.baz[0]=value1 +foo-bar.baz[1]=value2 +foo-bar.enabled=true +abcDef=value3 +`; + expect(inputHandler(Formats.PROPERTIES, input)) + .toStrictEqual({ 'abcDef': 'value3', 'foo-bar': { baz: ['value1', 'value2'], enabled: 'true' } }); +}); diff --git a/src/tools/env-variables-converter/lib/inputHandler.ts b/src/tools/env-variables-converter/lib/inputHandler.ts new file mode 100644 index 00000000..413032f4 --- /dev/null +++ b/src/tools/env-variables-converter/lib/inputHandler.ts @@ -0,0 +1,84 @@ +import yaml from 'yaml'; +import _ from 'lodash'; +import { Formats } from './formats'; + +function setAcc(acc: any, ...args: any[]) { + const [path, value] = args; + _.set(acc, path, value); +} + +function inputHandler(type: Formats, text: string) { + let data; + if (type === Formats.YAML) { + // https://github.com/jusufazer/yaml2properties/blob/master/src/scripts/processor.js + data = yaml.parse(text); + if (typeof data !== 'object') { + throw new TypeError('I could be wrong, but YAML doesn\'t seem valid.'); + } + } + else if (type === Formats.PROPERTIES) { + // const flattened = deflated.join("\r\n") + data = text + .split('\n') // divides lines + .filter(Boolean) // removes empty lines + .reduce((acc: any, line: any) => { + if (line.includes('=')) { + // https://stackoverflow.com/a/4607799/1098564 + setAcc(acc, ...line.split(/=(.+)/)); + } + else if (line.includes(': ')) { + setAcc(acc, ...line.split(/: (.+)/)); + } + else { + throw new Error('Doesn\'t look like a valid .properties file'); + } + return acc; + }, {}); + } + else if (type === Formats.SIMPLE) { + data = text + .split('\n') // divides lines + .filter(Boolean) // removes empty lines + .reduce((acc: any, line: any) => { + setAcc(acc, ...line.split('=')); + return acc; + }, {}); + } + else if (type === Formats.TERMINAL) { + data = text + .split(' ') // divides lines + .filter(Boolean) // removes empty lines + .reduce((acc: any, line: any) => { + setAcc(acc, ...line.split('=')); + return acc; + }, {}); + } + else if (type === Formats.KUBERNETES) { + data = text + .replace(/>-/gm, '') + .replace(/(\r\n|\n|\r)/gm, '') + .trim() + .split('- name: ') // divides lines + .filter(Boolean) // removes empty lines + .reduce((acc: any, line: any) => { + const pair = line.split(' value: '); + if (pair[1]) { + pair[1] = pair[1] + // trim single quotes at beginning and end + .replace(/^['](.+(?=[']$))[']$/, '$1') // https://stackoverflow.com/a/19156197/1098564 + // trim out double quotes (TODO: this needs improvement and is definitely bug prone...but works for most simple values I come across) + .replace(/"([^"]+(?="))"/g, '$1'); + } + setAcc(acc, ...pair); + return acc; + }, {}); + } + else { + throw new Error('Unsupported input type'); + } + // https://stackoverflow.com/a/33510710/1098564 + data = JSON.parse(JSON.stringify(data).replace(/"\s+|\s+"/g, '"')); + return data; +} + +export default inputHandler; diff --git a/src/tools/env-variables-converter/lib/outputFormatter.js b/src/tools/env-variables-converter/lib/outputFormatter.js new file mode 100644 index 00000000..0dc8245b --- /dev/null +++ b/src/tools/env-variables-converter/lib/outputFormatter.js @@ -0,0 +1,113 @@ +import YAML from 'yaml'; +import _ from 'lodash'; +import { Formats } from './formats'; + +function outputFormatter(outputType, properties) { + if (outputType === Formats.SIMPLE) { + return simpleFormatter(properties); + } + else if (outputType === Formats.TERMINAL) { + return terminalFormatter(properties); + } + else if (outputType === Formats.KUBERNETES) { + return kubernetesFormatter(properties); + } + else if (outputType === Formats.PROPERTIES) { + return propertiesFormatter(properties); + } + else if (outputType === Formats.YAML) { + return yamlFormatter(properties); + } + else { + throw new Error(`outputType ${outputType}not supported`); + } +} + +// see https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-relaxed-binding-from-environment-variables +function getName(property) { + return property + .split('=')[0] + .toUpperCase() + .replaceAll('-', '') + .replaceAll('].', '_') // don't do double underscore after list element + .replaceAll('.', '_') + .replaceAll('[', '_') + .replaceAll(']', '_'); +} + +function getValue(property) { + return (property.split('=')[1]) + .replace(/^["'](.+(?=["']$))["']$/, '$1'); +} + +function simpleFormatter(properties) { + let result = ''; + properties.forEach((property) => { + result = result + .concat(getName(property)) + .concat('=') + .concat(getValue(property)) + .concat('\n'); + }); + return result; +} + +function terminalFormatter(properties) { + let result = ''; + properties.forEach((property) => { + result = result + .concat(getName(property)) + .concat('=') + .concat(getValue(property)) + .concat(' '); + }); + return result; +} + +function kubernetesFormatter(properties) { + let result = ''; + properties.forEach((property) => { + result = result + .concat('- name: ') + .concat(getName(property)) + // .concat('\n value: \'') + .concat('\n value: ') + .concat(`'${getValue(property)}'`) + // .concat('\'\n') + .concat('\n'); + }); + return result; +} + +function propertiesFormatter(properties) { + let result = ''; + properties.forEach((property) => { + result = result + .concat(property.split('=')[0]) + .concat('=') + .concat(property.split('=')[1]) + .concat('\n'); + }); + return result; +} + +function yamlFormatter(properties) { + const result = properties + .filter(Boolean) // removes empty lines + .reduce((acc, line) => { + const pair = line.split('='); + if (pair[1]) { + if (pair[1] === 'true') { + pair[1] = true; + } + else if (pair[1] === 'false') { + pair[1] = false; + } + } + _.set(acc, ...pair); + return acc; + }, {}); + return YAML.stringify(result); +} + +export default outputFormatter; diff --git a/src/tools/env-variables-converter/lib/outputFormatter.test.js b/src/tools/env-variables-converter/lib/outputFormatter.test.js new file mode 100644 index 00000000..4a4e51a9 --- /dev/null +++ b/src/tools/env-variables-converter/lib/outputFormatter.test.js @@ -0,0 +1,40 @@ +import { describe, expect } from 'vitest'; +import { Formats } from '../formats'; +import outputFormatter from './outputFormatter'; + +describe('yaml format', () => { + const input = `foo-bar.baz[0]=value1 +foo-bar.baz[1]=value2 +foo-bar.enabled=true +abcDef=value3 +`.split('\n'); + + expect(outputFormatter(Formats.YAML, input)) + .toStrictEqual( +`foo-bar: + baz: + - value1 + - value2 + enabled: true +abcDef: value3 +`); +}); + +describe('k8s format', () => { + const input = `foo-bar.baz[0]=value1 +foo-bar.baz[1]=value2 +foo-bar.enabled=true +abcDef=value3`.split('\n'); + + expect(outputFormatter(Formats.KUBERNETES, input)) + .toStrictEqual( +`- name: FOOBAR_BAZ_0_ + value: 'value1' +- name: FOOBAR_BAZ_1_ + value: 'value2' +- name: FOOBAR_ENABLED + value: 'true' +- name: ABCDEF + value: 'value3' +`); +});