Skip to content

Commit

Permalink
Merge pull request #6 from deploymy/improvements
Browse files Browse the repository at this point in the history
feat(dockerComposeContent) switched to content
  • Loading branch information
Lasim authored Oct 1, 2024
2 parents 759e0f9 + aae31e6 commit 043f0cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ npm install

```typescript
import { translate } from 'docker-to-iac';
import { writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';

const translatedConfig = translate('path/to/docker-compose.yml', 'aws-cloudformation');
// Read Docker Compose file content as plain text
const dockerComposeContent = readFileSync('path/to/docker-compose.yml', 'utf8');

const translatedConfig = translate(dockerComposeContent, 'CFN');
console.log(translatedConfig);

// Write the translated config to a file
Expand All @@ -32,13 +35,13 @@ You can retrieve metadata about the available parsers as well:
```typescript
import { getParserInfo } from 'docker-to-iac';

const awsInfo = getParserInfo('aws-cloudformation');
const awsInfo = getParserInfo('CFN');
console.log(awsInfo);
```

### Supported Platforms

- `aws-cloudformation`
- `CFN`: AWS CloudFormation

## Development

Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readFileSync } from 'fs';
import * as yaml from 'js-yaml';
import cloudFormationParserInstance from './parsers/aws-cloudformation';
import { BaseParser, ParserInfo } from './parsers/base-parser';
Expand All @@ -10,10 +9,10 @@ const parsers: BaseParser[] = [
// e.g., terraformParserInstance
];

function translate(dockerComposeFilePath: string, targetPlatform: string): any {
// Updated translate function to take plain text as input
function translate(dockerComposeContent: string, targetPlatform: string): any {
try {
const fileContents = readFileSync(dockerComposeFilePath, 'utf8');
const dockerCompose = yaml.load(fileContents) as any;
const dockerCompose = yaml.load(dockerComposeContent) as any;

const parser = parsers.find(parser => parser.getInfo().abbreviation.toLowerCase() === targetPlatform.toLowerCase());
if (!parser) {
Expand All @@ -23,7 +22,7 @@ function translate(dockerComposeFilePath: string, targetPlatform: string): any {
const translatedConfig = parser.parse(dockerCompose);
return translatedConfig;
} catch (e) {
console.error(`Error translating docker-compose file: ${e}`);
console.error(`Error translating docker-compose content: ${e}`);
return null;
}
}
Expand Down
8 changes: 6 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { translate, getParserInfo, listAllParsers } from '../src/index';
import { writeFileSync } from 'fs';
import { writeFileSync, readFileSync } from 'fs';

const awsInfo = getParserInfo('CFN');
console.log('AWS CloudFormation Info:');
console.log(awsInfo);

// Read the Docker Compose file as plain text
const dockerComposeContent = readFileSync('test/sample-docker-compose.yml', 'utf8');

// Testing AWS CloudFormation
const awsConfig = translate('test/sample-docker-compose.yml', 'CFN');
const awsConfig = translate(dockerComposeContent, 'CFN');
console.log('AWS CloudFormation:');
console.log(awsConfig);
writeFileSync('test/output/output-aws.json', JSON.stringify(awsConfig, null, 2));

// List all available parsers
const parsers = listAllParsers();

console.log('Available Parsers:');
console.log(parsers);

0 comments on commit 043f0cb

Please sign in to comment.