feat(new tool): Env Variables Converter

Fix #1205
This commit is contained in:
ShareVB committed 2025-08-21 22:33:44 +02:00
1 parent 6cb4958013
commit 083b5e1834
9 files changed
+497

No files matched your search

@@ -0,0 +1,62 @@
<script setup>
import { ref } from 'vue';
import { NFormItem, NSelect } from 'naive-ui';
import deflate from './lib/deflate';
import inputHandler from './lib/inputHandler';
import outputFormatter from './lib/outputFormatter';
import { DefaultsMap, Formats, ModelType } from './lib/formats';
import { useQueryParamOrStorage } from '@/composable/queryParams';
const defaultInputType = Formats.YAML;
const inputText = ref(DefaultsMap.get(defaultInputType).inputValue);
const inputType = useQueryParamOrStorage({ name: 'in', storageName: 'env-var-conv:i', defaultValue: defaultInputType });
const outputType = useQueryParamOrStorage({ name: 'out', storageName: 'env-var-conv:o', defaultValue: DefaultsMap.get(defaultInputType).outputType });
const alertText = ref('');
const inputOptions = Object.keys(Formats).map(key => ({ label: Formats[key], value: Formats[key] }));
const outputOptions = ref([]);
function updateOutputOptions() {
const regular = [Formats.SIMPLE, Formats.TERMINAL, Formats.KUBERNETES].filter(opt => opt !== inputType.value);
if (DefaultsMap.get(inputType.value)?.modelType === ModelType.FLAT) {
regular.push(inputType.value === Formats.YAML ? Formats.PROPERTIES : Formats.YAML);
}
outputOptions.value = regular.map(value => ({ label: value, value }));
}
const outputText = computed(() => {
alertText.value = '';
try {
return outputFormatter(outputType.value, deflate(inputHandler(inputType.value, inputText.value)));
}
catch (error) {
alertText.value = error.message;
return '';
}
});
function onChangeInputTypeHandler(value) {
outputType.value = DefaultsMap.get(value).outputType;
updateOutputOptions();
}
updateOutputOptions();
</script>
<template>
<NForm>
<NFormItem label="Input Type:">
<NSelect v-model:value="inputType" :options="inputOptions" @update:value="onChangeInputTypeHandler" />
</NFormItem>
<c-input-text v-model:value="inputText" label="Input:" rows="10" multiline />
<NFormItem label="Output Type:">
<NSelect v-model:value="outputType" :options="outputOptions" />
</NFormItem>
<c-alert v-if="alertText" type="error">
{{ alertText }}
</c-alert>
<c-card title="Output">
<textarea-copyable v-model:value="outputText" />
</c-card>
</NForm>
</template>
@@ -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',
});
@@ -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;
@@ -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']);
});
@@ -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<string, Defaults>([
[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',
}],
]);
@@ -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' } });
});
@@ -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;
@@ -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;
@@ -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'
`);
});