mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
feat(new tool): Docker Compose to .env file
This commit is contained in:
1 parent
4a148b5cf2
commit
bc7166324a
4 files changed
+168
No files matched your search
@@ -0,0 +1,77 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { extractEnvFromCompose } from './docker-compose-to-env-file.service';
|
||||||
|
|
||||||
|
describe('extractEnvFromCompose', () => {
|
||||||
|
it('handles array format', () => {
|
||||||
|
const yaml = `
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
environment:
|
||||||
|
- PORT=3000
|
||||||
|
- DEBUG=true
|
||||||
|
db:
|
||||||
|
environment:
|
||||||
|
- POSTGRES_USER=admin
|
||||||
|
- POSTGRES_PASSWORD=secret
|
||||||
|
`;
|
||||||
|
const result = extractEnvFromCompose(yaml);
|
||||||
|
expect(result).toContain('# web');
|
||||||
|
expect(result).toContain('PORT=3000');
|
||||||
|
expect(result).toContain('DEBUG=true');
|
||||||
|
expect(result).toContain('# db');
|
||||||
|
expect(result).toContain('POSTGRES_USER=admin');
|
||||||
|
expect(result).toContain('POSTGRES_PASSWORD=secret');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles object format', () => {
|
||||||
|
const yaml = `
|
||||||
|
services:
|
||||||
|
api:
|
||||||
|
environment:
|
||||||
|
NODE_ENV: production
|
||||||
|
API_KEY: abc123
|
||||||
|
`;
|
||||||
|
const result = extractEnvFromCompose(yaml);
|
||||||
|
expect(result).toContain('# api');
|
||||||
|
expect(result).toContain('NODE_ENV=production');
|
||||||
|
expect(result).toContain('API_KEY=abc123');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles mixed formats and null values', () => {
|
||||||
|
const yaml = `
|
||||||
|
services:
|
||||||
|
mixed:
|
||||||
|
environment:
|
||||||
|
- FOO=bar
|
||||||
|
- EMPTY=
|
||||||
|
- FOO3=null
|
||||||
|
obj:
|
||||||
|
environment:
|
||||||
|
BAR: baz
|
||||||
|
FOO2: null
|
||||||
|
`;
|
||||||
|
const result = extractEnvFromCompose(yaml);
|
||||||
|
expect(result).toContain('# mixed');
|
||||||
|
expect(result).toContain('FOO=bar');
|
||||||
|
expect(result).toContain('EMPTY=');
|
||||||
|
expect(result).toContain('# obj');
|
||||||
|
expect(result).toContain('BAR=baz');
|
||||||
|
expect(result).toContain('FOO2=');
|
||||||
|
expect(result).toContain('FOO3=');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty string if no env vars', () => {
|
||||||
|
const yaml = `
|
||||||
|
services:
|
||||||
|
noenv:
|
||||||
|
image: nginx
|
||||||
|
`;
|
||||||
|
const result = extractEnvFromCompose(yaml);
|
||||||
|
expect(result).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles empty input gracefully', () => {
|
||||||
|
const result = extractEnvFromCompose('');
|
||||||
|
expect(result).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import yaml from 'yaml';
|
||||||
|
|
||||||
|
export function extractEnvFromCompose(yamlInput: string) {
|
||||||
|
const doc = yaml.parse(yamlInput);
|
||||||
|
const envLines: string[] = [];
|
||||||
|
|
||||||
|
for (const [serviceName, service] of Object.entries(doc?.services || {})) {
|
||||||
|
const env = (service as { environment: any }).environment;
|
||||||
|
if (env === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const serviceVars: string[] = [];
|
||||||
|
|
||||||
|
if (Array.isArray(env)) {
|
||||||
|
for (const entry of env) {
|
||||||
|
const [key, value = ''] = entry.split('=');
|
||||||
|
serviceVars.push(`${key.trim()}=${value.trim()}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (typeof env === 'object') {
|
||||||
|
for (const [key, value] of Object.entries(env)) {
|
||||||
|
serviceVars.push(`${key.trim()}=${(value ?? '').toString().trim()}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serviceVars.length) {
|
||||||
|
envLines.push(`# ${serviceName}`);
|
||||||
|
envLines.push(...serviceVars);
|
||||||
|
envLines.push(''); // Add spacing between services
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return envLines.join('\n');
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { extractEnvFromCompose } from './docker-compose-to-env-file.service';
|
||||||
|
|
||||||
|
const yamlInput = ref(`services:
|
||||||
|
web:
|
||||||
|
environment:
|
||||||
|
PORT: 3000
|
||||||
|
DEBUG: true
|
||||||
|
db:
|
||||||
|
environment:
|
||||||
|
- POSTGRES_USER=admin
|
||||||
|
- POSTGRES_PASSWORD=secret
|
||||||
|
`);
|
||||||
|
const envOutput = computed(() => {
|
||||||
|
try {
|
||||||
|
return extractEnvFromCompose(yamlInput.value);
|
||||||
|
}
|
||||||
|
catch (e: any) {
|
||||||
|
return e.toString();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="yamlInput"
|
||||||
|
label="Docker Compose file:"
|
||||||
|
multiline
|
||||||
|
placeholder="Paste your docker-compose YAML here..."
|
||||||
|
rows="10"
|
||||||
|
mb-2
|
||||||
|
/>
|
||||||
|
|
||||||
|
<n-card title="Extract .env">
|
||||||
|
<textarea-copyable
|
||||||
|
v-model:value="envOutput"
|
||||||
|
rows="3"
|
||||||
|
multiline
|
||||||
|
/>
|
||||||
|
</n-card>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { BrandDocker } from '@vicons/tabler';
|
||||||
|
import { defineTool } from '../tool';
|
||||||
|
|
||||||
|
export const tool = defineTool({
|
||||||
|
name: 'Docker Compose to .env file',
|
||||||
|
path: '/docker-compose-to-env-file',
|
||||||
|
description: 'Extract .env file from an existing Docker Compose file',
|
||||||
|
keywords: ['docker', 'compose', 'env', 'environment'],
|
||||||
|
component: () => import('./docker-compose-to-env-file.vue'),
|
||||||
|
icon: BrandDocker,
|
||||||
|
createdAt: new Date('2025-11-11'),
|
||||||
|
category: 'Docker',
|
||||||
|
});
|
||||||
Reference in new issue
Block a user