๐Ÿ… A command-line tool to get and set values in toml files while preserving comments and formatting

Overview

tomato

Test the tomato Crates.io

Get, set, and delete values in TOML files while preserving comments and formatting.

That's it. That's the feature set. I wrote tomato to satisfy my own primary use case, which is to read values of various types from a TOML preferences file, process those values in bash tooling, and infrequently update those values from other bash scripts.

To install:

# using homebrew:
brew tap ceejbot/tap
brew install tomato

# if you have rust installed and prefer to build from source:
cargo install tomato-toml

# once installed:
tomato --help

Or snag a pre-built executable from the releases.

An alternative tool would be dasel, if you don't need to preserve comments and formatting when you modify a value. dasel also supports a large variety of file formats.

If you need to convert among JSON, YAML, and TOML, check out jyt.

Usage

The short version:

  • Get a key: tomato get <dotted.key> <file>
  • Set a key: tomato set <dotted.key> <value> <file>
  • Delete a key: tomato rm <dotted.key> <file> (with lots of aliases for rm)

The set and rm subcommands modify the input file in place. Thanks to the magic of toml_edit, they do so without disturbing whitespace and comments.

By default tomato emits data a form suitable for immediate use in bash scripts. Strings are unquoted, for instance. The bash format generates output suitable for eval inside bash. Use this for arrays and associative arrays. If you need to consume more complex output, you might select json format and pipe the results to jq. And of course if you need TOML, use toml.

The longer version:

๐Ÿ… tomato 0.2.0
A command-line tool to get and set values in toml files while preserving comments and formatting.

Keys are written using `.` to separate path segments. You can use `array[idx]` syntax to index into
arrays if you want to. For example, to get the name of the current crate you're working on, you'd
run `tomato get Cargo.toml package.name`.

By default tomato emits data in a form suitable for immediate use in bash scripts if they are
primitive values: strings are unquoted, for instance. If you want to use more complex data types,
consider one of the other output formats.

To read from stdin instead of a file, omit the file from the arguments. Operating on stdin changes
the behavior of set and rm somewhat, under the assumption that you are using this tool in a shell
script. If you read from stdin, normal output (the old value) is suppressed. Instead the modified
file is written to stdout in json if you requested json, toml otherwise. The 'bash' format option is
ignored.

USAGE:
	tomato [OPTIONS] <SUBCOMMAND>

OPTIONS:
	-b, --backup
			Back up the file to <filepath>.bak if we write a new version. This option is ignored
			when we're operating on stdin

	-f, --format <FORMAT>
			How to format the output: json, toml, bash, or raw
			[default: raw]

	-h, --help
			Print help information

	-V, --version
			Print version information

SUBCOMMANDS:
	get     Get the value of a key from the given file
	set     Set a key to the given value, returning the previous value if one existed
	rm      Delete a key from the given file, returning the previous value if one existed
	completions
			Generate completions for the named shell
	help    Print this message or the help of the given subcommand(s)

get and rm both print empty string to stdout if the target key is not found. set exits with a non-zero status with a message printed to stderr if the target key is not found.

Examples

Here are some examples run against the Cargo manifest for this project:

$ tomato get package.name Cargo.toml
tomato
$ tomato --format json get package.name Cargo.toml
"tomato"
$ cat Cargo.toml | tomato get package.name
tomato

# set examples
$ tomato set package.name broccoli Cargo.toml
tomato
$ tomato set package.keywords[1] yaml Cargo.toml
toml

# Keys that don't exist
$ tomato get dependencies.toml_edit[0] Cargo.toml

$ tomato set dependencies.toml_edit[0] "first!" Cargo.toml
Error: unable to index into non-array at dependencies.toml_edit.0

# rm has a number of aliases to prevent user frustration
$ tomato --format json del package.categories[0] Cargo.toml
"command-line-utilities"

Look at the examples/ directory for some sample bash scripts with more varied examples, including examples of using lists and associative arrays in bash.

CONTRIBUTING

Heck, yeah! Please keep the major use case in mind: you need to read toml and do stuff with it in bash. I'm happy to accept anything that improves that use case or makes the Rust parts better.

LICENSE

Blue Oak Model License; text in LICENSE.md.

You might also like...
PlandUML and Drawio diagrams in doc comments as PNG or SVG images.
PlandUML and Drawio diagrams in doc comments as PNG or SVG images.

rsdoc This crate provides a procedural macro that transform PlandUML and Drawio diagrams in doc comments as PNG or SVG images. The diagrams in doc com

RnR is a command-line tool to securely rename multiple files and directories that supports regular expressions
RnR is a command-line tool to securely rename multiple files and directories that supports regular expressions

RnR is a command-line tool to securely rename multiple files and directories that supports regular expressions. Features Batch rename files and direct

rsv is a command line tool to deal with small and big CSV, TXT, EXCEL files (especially 10G)

csv, excel toolkit written in Rust rsv is a command line tool to deal with small and big CSV, TXT, EXCEL files (especially 10G). rsv has following fe

Experimental implementation of the Privacy Preserving Measurement (PPM) specification.

janus Janus is an experimental implementation of the Privacy Preserving Measurement (PPM) specification. It is currently in active development. Runnin

Command line tool to extract various data from Blender .blend files

blendtool Command line tool to extract various data from Blender .blend files. Currently supports dumping Eevee irradiance volumes to .dds, new featur

 apkeep - A command-line tool for downloading APK files from various sources
apkeep - A command-line tool for downloading APK files from various sources

apkeep - A command-line tool for downloading APK files from various sources Installation Precompiled binaries for apkeep on various platforms can be d

Anglosaxon is a command line tool to parse XML files using SAX
Anglosaxon is a command line tool to parse XML files using SAX

anglosaxon - Convert large XML files to other formats anglosaxon is a command line tool to parse XML files using SAX. You can do simple transformation

Command line tool for editing .ini files

Edit-ini Command line tool for editing .ini files Usage Usage: edit-ini [OPTIONS] Options: -i, --input file Input file to read f

Potr (Po Translator) is a command line tool for translating gettext PO files.

Potr Potr (Po Translator) is a command line tool for translating Gettext PO files. Currently, it supports translation using OpenAI, Azure OpenAI Servi

Comments
  • Adds support for appending values to arrays

    Adds support for appending values to arrays

    Hey there. Thanks for sharing this super handy tool! I'd like to use it for editing toml configuration files inside container builds and noticed there was no support for appending values to arrays, so I've added it along with a few tests.

    You can now append <KEY> <VALUE> which returns the previous array or nothing if it didn't exist. Errors if the key exists but is not an array.

    I feel like the code could be factored here and there as this creates a few redundancies but I preferred to keep the diff as simple as possible rather than starting shaving the yak. Hope this is fine.

    Let me know if there's anything else I should do. Cheers!

    opened by flavioroth 0
Releases(v0.3.0)
Owner
C J Silverio
I've been on the internet longer than you have. This earns me one (1) cup of coffee.
C J Silverio
JiaShiwen 12 Nov 5, 2022
A command line tool that resembles a debugger as well as Cheat Engine, to search for values in memory

Summary This is a small command-line tool designed to peek around memory of a running Linux process. It also provides filtering mechanisms similar to

null 213 Jul 4, 2023
Given a set of kmers (fasta format) and a set of sequences (fasta format), this tool will extract the sequences containing the kmers.

Kmer2sequences Description Given a set of kmers (fasta / fastq [.gz] format) and a set of sequences (fasta / fastq [.gz] format), this tool will extra

Pierre Peterlongo 22 Sep 16, 2023
ruborute is an interactive command-line tool to get asphyxia@sdvx gaming data.

ruborute Are you ๆšด้พๅคฉ ?. The ruborute is an interactive command-line tool to get asphyxia@sdvx gaming data. asphyxia-core/plugins: https://github.com/a

RinChanNOW! 9 Sep 28, 2022
Rust TUI library - Clipping region is a set of min/max x/y values applied to the existing region

TinyBit Clipping region is a set of min/max x/y values applied to the existing region A TUI lib This is not yet production ready T O D O TODO: bugs: T

Togglebit 13 May 3, 2022
tmplt is a command-line interface tool that allows you to quickly and easily set up project templates for various programming languages and frameworks

tmplt A User Friendly CLI Tool For Creating New Projects With Templates About tmplt is a command-line tool that lets users quickly create new projects

Humble Penguin 35 Apr 8, 2023
Small command-line tool to switch monitor inputs from command line

swmon Small command-line tool to switch monitor inputs from command line Installation git clone https://github.com/cr1901/swmon cargo install --path .

William D. Jones 5 Aug 20, 2022
This CLI utility facilitates effortless manipulation and exploration of TOML, YAML, JSON and RON files.

???????? This CLI utility facilitates effortless manipulation and exploration of TOML, YAML, JSON and RON files.

Moe 3 Apr 26, 2023
Sleek is a CLI tool for formatting SQL. It helps you maintain a consistent style across your SQL code, enhancing readability and productivity.

Sleek: SQL Formatter โœจ Sleek is a CLI tool for formatting SQL. It helps you maintain a consistent style across your SQL code, enhancing readability an

Nick Rempel 40 Apr 20, 2023
A simple CLI I made while practicing rust to easily make QR codes with just one command, all in your terminal.

Welcome to rust-qrcode-cli ?? A CLI I made while practicing rust to easily make QR codes with just one command, all in your terminal. Install git clon

Dhravya Shah 2 Mar 2, 2022