mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
1 parent
14bfc21b0a
commit
00584bd549
4 files changed
+116
-29
No files matched your search
@@ -7579,6 +7579,7 @@ tools:
|
||||
label-docker-compose-file: 'Docker Compose file:'
|
||||
placeholder-paste-your-docker-compose-yaml-here: Paste your docker-compose YAML here...
|
||||
title-extract-env: Extract .env
|
||||
title-updated-docker-compose: 'Updated Docker Compose:'
|
||||
emv-tlv-decoder:
|
||||
title: EMV TLV decoder
|
||||
description: >-
|
||||
|
||||
@@ -15,12 +15,26 @@ services:
|
||||
- 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');
|
||||
expect(result).to.deep.eq({
|
||||
dotenv: `# web
|
||||
PORT=3000
|
||||
DEBUG=true
|
||||
|
||||
# db
|
||||
POSTGRES_USER=admin
|
||||
POSTGRES_PASSWORD=secret
|
||||
`,
|
||||
updatedCompose: `services:
|
||||
web:
|
||||
environment:
|
||||
- PORT=\${PORT}
|
||||
- DEBUG=\${DEBUG}
|
||||
db:
|
||||
environment:
|
||||
- POSTGRES_USER=\${POSTGRES_USER}
|
||||
- POSTGRES_PASSWORD=\${POSTGRES_PASSWORD}
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it('handles object format', () => {
|
||||
@@ -32,9 +46,40 @@ services:
|
||||
API_KEY: abc123
|
||||
`;
|
||||
const result = extractEnvFromCompose(yaml);
|
||||
expect(result).toContain('# api');
|
||||
expect(result).toContain('NODE_ENV=production');
|
||||
expect(result).toContain('API_KEY=abc123');
|
||||
expect(result).to.deep.eq({
|
||||
dotenv: `# api
|
||||
NODE_ENV=production
|
||||
API_KEY=abc123
|
||||
`,
|
||||
updatedCompose: `services:
|
||||
api:
|
||||
environment:
|
||||
- NODE_ENV=\${NODE_ENV}
|
||||
- API_KEY=\${API_KEY}
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it('handles with already existing environment variables', () => {
|
||||
const yaml = `
|
||||
services:
|
||||
api:
|
||||
environment:
|
||||
NODE_ENV: \${NODE_ENV}
|
||||
API_KEY: abc123
|
||||
`;
|
||||
const result = extractEnvFromCompose(yaml);
|
||||
expect(result).to.deep.eq({
|
||||
dotenv: `# api
|
||||
API_KEY=abc123
|
||||
`,
|
||||
updatedCompose: `services:
|
||||
api:
|
||||
environment:
|
||||
- NODE_ENV=\${NODE_ENV}
|
||||
- API_KEY=\${API_KEY}
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it('handles mixed formats and null values', () => {
|
||||
@@ -51,13 +96,28 @@ services:
|
||||
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=');
|
||||
expect(result).to.deep.eq({
|
||||
dotenv: `# mixed
|
||||
FOO=bar
|
||||
EMPTY=
|
||||
FOO3=null
|
||||
|
||||
# obj
|
||||
BAR=baz
|
||||
FOO2=
|
||||
`,
|
||||
updatedCompose: `services:
|
||||
mixed:
|
||||
environment:
|
||||
- FOO=\${FOO}
|
||||
- EMPTY=\${EMPTY}
|
||||
- FOO3=\${FOO3}
|
||||
obj:
|
||||
environment:
|
||||
- BAR=\${BAR}
|
||||
- FOO2=\${FOO2}
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns empty string if no env vars', () => {
|
||||
@@ -67,11 +127,20 @@ services:
|
||||
image: nginx
|
||||
`;
|
||||
const result = extractEnvFromCompose(yaml);
|
||||
expect(result).toBe('');
|
||||
expect(result).to.deep.eq({
|
||||
dotenv: '',
|
||||
updatedCompose: `services:
|
||||
noenv:
|
||||
image: nginx
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it('handles empty input gracefully', () => {
|
||||
const result = extractEnvFromCompose('');
|
||||
expect(result).toBe('');
|
||||
expect(result).to.deep.eq({
|
||||
dotenv: '',
|
||||
updatedCompose: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,9 @@
|
||||
import yaml from 'yaml';
|
||||
|
||||
export function extractEnvFromCompose(yamlInput: string) {
|
||||
if (!yamlInput?.trim()) {
|
||||
return { dotenv: '', updatedCompose: '' };
|
||||
}
|
||||
const doc = yaml.parse(yamlInput);
|
||||
const envLines: string[] = [];
|
||||
|
||||
@@ -11,25 +14,42 @@ export function extractEnvFromCompose(yamlInput: string) {
|
||||
}
|
||||
|
||||
const serviceVars: string[] = [];
|
||||
const serviceReplacementVars: string[] = [];
|
||||
|
||||
if (Array.isArray(env)) {
|
||||
for (const entry of env) {
|
||||
const [key, value = ''] = entry.split('=');
|
||||
if (value?.trim().match(/^\$\{.*?\}$/)) {
|
||||
serviceReplacementVars.push(entry);
|
||||
continue;
|
||||
}
|
||||
serviceVars.push(`${key.trim()}=${value.trim()}`);
|
||||
serviceReplacementVars.push(`${key.trim()}=\$\{${key.trim()}\}`);
|
||||
}
|
||||
}
|
||||
else if (typeof env === 'object') {
|
||||
} else if (typeof env === 'object') {
|
||||
for (const [key, value] of Object.entries(env)) {
|
||||
if (
|
||||
value
|
||||
?.toString()
|
||||
.trim()
|
||||
.match(/^\$\{.*?\}$/)
|
||||
) {
|
||||
serviceReplacementVars.push(`${key.trim()}=${(value ?? '').toString().trim()}`);
|
||||
continue;
|
||||
}
|
||||
serviceVars.push(`${key.trim()}=${(value ?? '').toString().trim()}`);
|
||||
serviceReplacementVars.push(`${key.trim()}=\$\{${key.trim()}\}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (serviceVars.length) {
|
||||
(service as { environment: any }).environment = serviceReplacementVars;
|
||||
|
||||
envLines.push(`# ${serviceName}`);
|
||||
envLines.push(...serviceVars);
|
||||
envLines.push(''); // Add spacing between services
|
||||
}
|
||||
}
|
||||
|
||||
return envLines.join('\n');
|
||||
return { dotenv: envLines.join('\n'), updatedCompose: yaml.stringify(doc) };
|
||||
}
|
||||
@@ -17,8 +17,7 @@ const yamlInput = ref(`services:
|
||||
const envOutput = computed(() => {
|
||||
try {
|
||||
return extractEnvFromCompose(yamlInput.value);
|
||||
}
|
||||
catch (e: any) {
|
||||
} catch (e: any) {
|
||||
return e.toString();
|
||||
}
|
||||
});
|
||||
@@ -36,12 +35,10 @@ const envOutput = computed(() => {
|
||||
/>
|
||||
|
||||
<n-card :title="t('tools.docker-compose-to-env-file.texts.title-extract-env')">
|
||||
<textarea-copyable
|
||||
v-model:value="envOutput"
|
||||
rows="3"
|
||||
download-file-name=".env"
|
||||
multiline
|
||||
/>
|
||||
<textarea-copyable v-model:value="envOutput.dotenv" rows="3" download-file-name=".env" multiline />
|
||||
</n-card>
|
||||
<n-card :title="t('tools.docker-compose-to-env-file.texts.title-updated-docker-compose')">
|
||||
<textarea-copyable v-model:value="envOutput.updatedCompose" rows="3" download-file-name="compose.yml" multiline />
|
||||
</n-card>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user