Skip to content

subnero1/tomler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”§ Tomler

License: MIT Rust

A simple, fast, and reliable CLI utility for in-place editing of TOML files with automatic type inference and nested key support.

✨ Features

  • πŸ”„ In-place editing: Modify TOML files while preserving comments and formatting
  • 🎯 Dot notation: Access nested keys with server.database.port syntax
  • 🧠 Smart type inference: Automatically detects booleans, integers, floats, arrays, and strings
  • πŸ“ Format preservation: Uses toml_edit to maintain original file structure
  • ⚑ Fast and lightweight: Single binary with minimal dependencies
  • πŸ”§ Shell-friendly: Perfect for automation and scripting

πŸš€ Quick Start

# Create or edit config.toml (default file)
echo '[database]' > config.toml
echo 'host = "localhost"' >> config.toml
echo '[app]' >> config.toml
echo '[server]' >> config.toml

# Get a value
tomler get database.host

# Set a string value
tomler set app.name "My Application"

# Set a number
tomler set server.port 8080

# Set a boolean
tomler set debug true

# Set an array
tomler set allowed_hosts "localhost,127.0.0.1,example.com"

# Work with nested keys
tomler set database.host.url "tcp://localhost:6379"

πŸ“¦ Installation

From Source

# Clone the repository
git clone https://github.com/subnero1/tomler.git
cd tomler

# Build release binary
cargo build --release

# Install to ~/.cargo/bin (make sure it's in your PATH)
cargo install --path .

Prerequisites

πŸ“š Usage

Edit TOML files in-place with simple type inference and nested keys

Usage: tomler [OPTIONS] <COMMAND>

Commands:
  get   Get a value by key (dot notation)
  set   Set a value by key (dot notation)
  help  Print this message or the help of the given subcommand(s)

Options:
  -f, --file <FILE>  TOML file path (default: config.toml) [default: config.toml]
  -h, --help         Print help

Commands

get <key>

Retrieve a value from the TOML file using dot notation.

tomler get server.port
tomler -f app.toml get database.url

# Default prints strings with quotes
tomler get app.name         # => "My Application"

# Raw string mode prints strings without quotes (useful in shells)
tomler get --raw app.name   # => My Application
tomler get -r app.name      # short flag

set <key> <value>

Set a value in the TOML file with automatic type inference.

tomler set server.port 3000
tomler -f app.toml set debug false

🧠 Type Inference

Tomler automatically infers the correct TOML type based on the input:

Input TOML Type Example
true, false Boolean debug = true
42, -10 Integer port = 8080
3.14, 0.5 Float timeout = 30.5
a,b,c Array hosts = ["a", "b", "c"]
1,2,3 Integer Array ports = [80, 443, 8080]
"hello", anything else String name = "hello"

Array Syntax

Simple comma-separated values are converted to arrays:

# Creates: ports = [80, 443, 8080]
tomler set ports "80,443,8080"

# Creates: tags = ["web", "api", "production"]
tomler set tags "web,api,production"

# Quoted strings are treated as single values:
# Creates: description = "Hello, World!"
tomler set description "\"Hello, World!\""

πŸ“ File Handling

  • Default file: config.toml (in current directory)
  • Custom file: Use -f or --file option
  • File creation: Creates file if it doesn't exist (for set operations)
  • Format preservation: Maintains comments, spacing, and key order

🎯 Examples

Basic Configuration Management

# Initialize a new config
tomler set app.name "MyApp"
tomler set app.version "1.0.0"
tomler set app.debug false

# Configure server
tomler set server.host "0.0.0.0"
tomler set server.port 8080
tomler set server.workers 4

# Set up database
tomler set database.url "postgresql://localhost/myapp"
tomler set database.pool_size 10
tomler set database.timeout 30.0

Result in config.toml:

[app]
name = "MyApp"
version = "1.0.0"
debug = false

[server]
host = "0.0.0.0"
port = 8080
workers = 4

[database]
url = "postgresql://localhost/myapp"
pool_size = 10
timeout = 30.0

Working with Arrays

# Set allowed origins for CORS
tomler set cors.allowed_origins "http://localhost:3000,https://example.com"

# Set multiple environment variables
tomler set env.required "DATABASE_URL,SECRET_KEY,API_KEY"

# Set numeric arrays
tomler set monitoring.alert_thresholds "50,75,90,95"

Shell Scripting Integration

#!/bin/bash

# Read current port
current_port=$(tomler get server.port)
echo "Current port: $current_port"

# Read a string value without quotes using raw mode
app_name=$(tomler get --raw app.name)
echo "App name: $app_name"

# Update configuration based on environment
if [[ "$ENV" == "production" ]]; then
    tomler set app.debug false
    tomler set server.workers 8
    tomler set database.pool_size 20
else
    tomler set app.debug true
    tomler set server.workers 2
    tomler set database.pool_size 5
fi

# Set deployment timestamp
tomler set deployment.timestamp "$(date -u +%Y-%m-%dT%H:%M:%SZ)"

Docker Integration

FROM rust:1.70 AS builder
COPY . .
RUN cargo build --release

FROM debian:bookworm-slim
COPY --from=builder /target/release/tomler /usr/local/bin/
RUN tomler set app.environment "production"
RUN echo "Environment: $(tomler get --raw app.environment)"

⚠️ Error Handling

Tomler provides clear error messages:

# File not found
$ tomler -f missing.toml get key
Error: failed to read missing.toml: No such file or directory

# Invalid TOML syntax
$ echo "invalid toml [" > bad.toml
$ tomler -f bad.toml get key
Error: failed to parse toml file bad.toml: expected `]`, found eof

# Key not found
$ tomler get nonexistent.key
Key not found: nonexistent.key

Exit codes:

  • 0: Success
  • 1: General error (file I/O, parsing, etc.)
  • 2: Key not found (get command only)

🧡 Raw String Mode

When retrieving values with get, Tomler prints TOML tokens by default. That means strings include their quotes (e.g. "example"). If you're piping output to other commands or using shell variables, you may prefer unquoted strings.

  • Use --raw (or -r) to print strings without enclosing quotes.
  • Non-string values (numbers, booleans, arrays) are unaffected and print the same with or without --raw.

Examples:

# Quoted by default
tomler get app.name         # => "MyApp"

# Unquoted with --raw
tomler get --raw app.name   # => MyApp

# Non-string values are unchanged
tomler get --raw server.port   # => 8080
tomler get --raw debug         # => true

πŸ› οΈ Development

Building from Source

git clone https://github.com/subnero1/tomler.git
cd tomler

# Development build
cargo build

# Release build
cargo build --release

# Run tests
cargo test

# Check formatting and linting
cargo fmt --check
cargo clippy -- -D warnings

Project Structure

tomler/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.rs          # CLI interface and argument parsing
β”‚   └── lib.rs           # Core TOML manipulation logic
β”œβ”€β”€ tests/
β”‚   └── integration.rs   # Integration tests
β”œβ”€β”€ Cargo.toml           # Project configuration
└── README.md           # This file

Testing

# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test test_name

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Guidelines

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please ensure:

  • Tests pass (cargo test)
  • Code is formatted (cargo fmt)
  • No clippy warnings (cargo clippy)

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Built with toml_edit for format-preserving TOML manipulation
  • CLI powered by clap
  • Error handling via anyhow

πŸ“ž Support


Made with ❀️ by Chinmay Pendharkar

About

A simple lightweight toml get/set tool

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages