mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-07 23:05:11 -04:00
Add Spring Boot properties to YAML converter
This commit is contained in:
parent
670f735501
commit
e376a57b70
6 changed files with 168 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
import { tool as base64FileConverter } from './base64-file-converter';
|
||||
import { tool as base64StringConverter } from './base64-string-converter';
|
||||
import { tool as basicAuthGenerator } from './basic-auth-generator';
|
||||
import { tool as propertiesToYaml } from './properties-to-yaml';
|
||||
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
|
||||
import { tool as numeronymGenerator } from './numeronym-generator';
|
||||
import { tool as macAddressGenerator } from './mac-address-generator';
|
||||
|
@ -100,6 +101,7 @@ export const toolsByCategory: ToolCategory[] = [
|
|||
listConverter,
|
||||
tomlToJson,
|
||||
tomlToYaml,
|
||||
propertiesToYaml,
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
12
src/tools/properties-to-yaml/index.ts
Normal file
12
src/tools/properties-to-yaml/index.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { AlignJustified } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'Properties to YAML',
|
||||
path: '/properties-to-yaml',
|
||||
description: '',
|
||||
keywords: ['properties', 'yaml', 'convert', 'spring', 'spring-boot', 'parse'],
|
||||
component: () => import('./properties-to-yaml.vue'),
|
||||
icon: AlignJustified,
|
||||
createdAt: new Date('2023-12-24'),
|
||||
});
|
65
src/tools/properties-to-yaml/properties-to-yaml.e2e.spec.ts
Normal file
65
src/tools/properties-to-yaml/properties-to-yaml.e2e.spec.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import { expect, test } from '@playwright/test';
|
||||
|
||||
test.describe('Tool - Properties to YAML', () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
await page.goto('/properties-to-yaml');
|
||||
});
|
||||
|
||||
test('Has correct title', async ({ page }) => {
|
||||
await expect(page).toHaveTitle('Properties to YAML - IT Tools');
|
||||
});
|
||||
|
||||
test('', async ({ page }) => {
|
||||
await page.getByTestId('input').fill(`
|
||||
spring.mvc.async.request-timeout=-1
|
||||
spring.output.ansi.enabled=NEVER
|
||||
spring.config.import=optional:file:.env[.properties]
|
||||
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.url=\${DATABASE_URI}
|
||||
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
|
||||
spring.jpa.open-in-view=false
|
||||
management.endpoints.web.base-path=/internal
|
||||
management.endpoints.web.exposure.include[0]=health
|
||||
management.endpoints.web.exposure.include[1]=info
|
||||
management.endpoint.info.enabled=true
|
||||
management.endpoint.health=
|
||||
`.trim());
|
||||
|
||||
const generatedJson = await page.getByTestId('area-content').innerText();
|
||||
|
||||
expect(generatedJson.trim()).toEqual(`
|
||||
spring:
|
||||
mvc:
|
||||
async:
|
||||
request-timeout: "-1"
|
||||
output:
|
||||
ansi:
|
||||
enabled: NEVER
|
||||
config:
|
||||
import: optional:file:.env[.properties]
|
||||
datasource:
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
url: \${DATABASE_URI}
|
||||
jpa:
|
||||
hibernate:
|
||||
naming:
|
||||
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||
database-platform: org.hibernate.dialect.MySQLDialect
|
||||
open-in-view: "false"
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
base-path: /internal
|
||||
exposure:
|
||||
include:
|
||||
- health
|
||||
- info
|
||||
endpoint:
|
||||
info:
|
||||
enabled: "true"
|
||||
health: ""
|
||||
`.trim(),
|
||||
);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,19 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { isValidProperties } from './properties-to-yaml.service';
|
||||
|
||||
describe('isValidProperties', () => {
|
||||
it('should return true for valid properties', () => {
|
||||
const properties = 'key1=value1\nkey2=value2';
|
||||
expect(isValidProperties(properties)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for properties with duplicate keys', () => {
|
||||
const properties = 'key1=value1\nkey1=value2';
|
||||
expect(isValidProperties(properties)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for properties with incorrect format', () => {
|
||||
const properties = 'key1\nkey2=value2';
|
||||
expect(isValidProperties(properties)).toBe(false);
|
||||
});
|
||||
});
|
45
src/tools/properties-to-yaml/properties-to-yaml.service.ts
Normal file
45
src/tools/properties-to-yaml/properties-to-yaml.service.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
export { isValidProperties, parseProperties };
|
||||
|
||||
function isValidProperties(properties: string): boolean {
|
||||
const lines = properties.split('\n');
|
||||
const keys: Set<string> = new Set();
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.trim().startsWith('#') || line.trim() === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!line.includes('=')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const [key, _value] = line.split('=');
|
||||
|
||||
if (!key) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (keys.has(key.trim())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
keys.add(key.trim());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Thanks to https://github.com/sdoxsee/env-gen
|
||||
function parseProperties(properties: string): Record<string, string> {
|
||||
const result = properties
|
||||
.split('\n')
|
||||
.filter(Boolean) // removes empty lines
|
||||
.reduce((acc, line) => {
|
||||
const pair = line.split('=');
|
||||
_.set(acc, pair[0], pair[1]);
|
||||
return acc;
|
||||
}, {});
|
||||
return result;
|
||||
}
|
25
src/tools/properties-to-yaml/properties-to-yaml.vue
Normal file
25
src/tools/properties-to-yaml/properties-to-yaml.vue
Normal file
|
@ -0,0 +1,25 @@
|
|||
<script setup lang="ts">
|
||||
import { stringify as stringifyToYaml } from 'yaml';
|
||||
import { withDefaultOnError } from '../../utils/defaults';
|
||||
import { isValidProperties, parseProperties } from './properties-to-yaml.service';
|
||||
import type { UseValidationRule } from '@/composable/validation';
|
||||
|
||||
const transformer = (value: string) => value.trim() === '' ? '' : withDefaultOnError(() => stringifyToYaml(parseProperties(value)), '');
|
||||
const rules: UseValidationRule<string>[] = [
|
||||
{
|
||||
validator: isValidProperties,
|
||||
message: 'Invalid .properties given.',
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<format-transformer
|
||||
input-label="Your Properties"
|
||||
input-placeholder="Paste your Properties here..."
|
||||
output-label="YAML from your Properties"
|
||||
output-language="yaml"
|
||||
:input-validation-rules="rules"
|
||||
:transformer="transformer"
|
||||
/>
|
||||
</template>
|
Loading…
Add table
Add a link
Reference in a new issue