Try it yourself with our free Toml Formatter tool — runs entirely in your browser, no signup needed.

How to Parse TOML for DevOps

How to Parse TOML for DevOps

As DevOps practitioners, we often work with configuration files in various formats, including TOML (Tom's Obvious, Minimal Language). TOML is a lightweight, easy-to-read format that is widely used in the DevOps community for configuration files, such as those used by tools like GitLab CI/CD and CircleCI. In this article, we will explore how to parse TOML files in a DevOps context, providing practical examples and best practices to help you get the most out of this format.

Quick Example

Here is a minimal example of how to parse a TOML file using JavaScript and the toml package:

// Install the toml package using npm or yarn
// npm install toml
// yarn add toml

import * as toml from 'toml';

const tomlString = `
  title = "My Project"
  [database]
    host = "localhost"
    port = 5432
`;

const parsedToml = toml.parse(tomlString);

console.log(parsedToml);
// Output:
// {
//   title: 'My Project',
//   database: {
//     host: 'localhost',
//     port: 5432
//   }
// }

This example demonstrates how to parse a simple TOML string into a JavaScript object.

Real-World Scenarios

Scenario 1: Parsing a GitLab CI/CD Configuration File

In this scenario, we want to parse a GitLab CI/CD configuration file (.gitlab-ci.yml) to extract the job definitions.

import * as fs from 'fs';
import * as toml from 'toml';

const gitlabCiYml = fs.readFileSync('.gitlab-ci.yml', 'utf8');
const parsedCiYml = toml.parse(gitlabCiYml);

console.log(parsedCiYml.jobs);
// Output:
// {
//   build: {
//     stage: 'build',
//     script: 'npm run build'
//   },
//   deploy: {
//     stage: 'deploy',
//     script: 'npm run deploy'
//   }
// }

Scenario 2: Extracting Environment Variables from a TOML File

In this scenario, we want to extract environment variables from a TOML file and set them as environment variables in our Node.js application.

import * as toml from 'toml';

const tomlString = `
  [env]
    DB_HOST = "localhost"
    DB_PORT = 5432
`;

const parsedToml = toml.parse(tomlString);

Object.keys(parsedToml.env).forEach((key) => {
  process.env[key] = parsedToml.env[key];
});

Scenario 3: Validating a TOML Configuration File

In this scenario, we want to validate a TOML configuration file against a schema to ensure it conforms to our expected format.

import * as toml from 'toml';
import * as Ajv from 'ajv';

const schema = {
  type: 'object',
  properties: {
    title: { type: 'string' },
    database: {
      type: 'object',
      properties: {
        host: { type: 'string' },
        port: { type: 'integer' }
      },
      required: ['host', 'port']
    }
  },
  required: ['title', 'database']
};

const ajv = new Ajv();
const validate = ajv.compile(schema);

const tomlString = `
  title = "My Project"
  [database]
    host = "localhost"
    port = 5432
`;

const parsedToml = toml.parse(tomlString);

if (!validate(parsedToml)) {
  console.error('Invalid TOML configuration');
} else {
  console.log('Valid TOML configuration');
}

Best Practices

  1. Use a TOML parser library: Instead of writing your own TOML parser, use a well-maintained library like toml to ensure correct parsing and avoid security vulnerabilities.
  2. Validate your TOML files: Use a schema validation library like ajv to ensure your TOML files conform to your expected format.
  3. Use environment variables: Instead of hardcoding values in your TOML files, use environment variables to keep sensitive information secure.
  4. Keep your TOML files simple: Avoid complex data structures and keep your TOML files simple and easy to read.
  5. Test your TOML parsing: Write tests to ensure your TOML parsing code works correctly and handles different edge cases.

Common Mistakes

Mistake 1: Not handling errors properly

Wrong code:

try {
  const parsedToml = toml.parse(tomlString);
  // ...
} catch (error) {
  console.error('Error parsing TOML');
}

Corrected code:

try {
  const parsedToml = toml.parse(tomlString);
  // ...
} catch (error) {
  console.error(`Error parsing TOML: ${error.message}`);
  process.exit(1);
}

Mistake 2: Not validating TOML files

Wrong code:

const parsedToml = toml.parse(tomlString);
// ...

Corrected code:

const schema = {
  // ...
};

const ajv = new Ajv();
const validate = ajv.compile(schema);

const parsedToml = toml.parse(tomlString);

if (!validate(parsedToml)) {
  console.error('Invalid TOML configuration');
} else {
  // ...
}

Mistake 3: Hardcoding values in TOML files

Wrong code:

title = "My Project"
[database]
  host = "localhost"
  port = 5432

Corrected code:

title = "My Project"
[database]
  host = "${DB_HOST}"
  port = "${DB_PORT}"

FAQ

Q: What is TOML?

A: TOML is a lightweight, easy-to-read format for configuration files.

Q: How do I parse a TOML file in Node.js?

A: You can use a library like toml to parse a TOML file in Node.js.

Q: How do I validate a TOML file?

A: You can use a schema validation library like ajv to validate a TOML file.

Q: Can I use TOML files for environment variables?

A: Yes, you can use TOML files to store environment variables.

Q: How do I handle errors when parsing TOML files?

A: You should handle errors properly by catching and logging the error, and exiting the process if necessary.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp