Rust TOML处理工具toml-cli的使用:高效解析、编辑和操作TOML配置文件

toml-cli

这是一个简单的命令行工具 toml,用于编辑和查询 TOML 文件。

toml 命令的设计目标是:

  • 在 shell 脚本中用于查询或编辑配置文件
  • 提供人类可遵循的配置文件编辑指令,作为可复制粘贴运行的命令

其接口设计灵感来源于 git config 命令,该命令在不了解 Git 配置文件语义的情况下很好地服务于这两个目的。

关键特性是编辑时会保留格式和注释——文件唯一的变化应该是用户明确要求的更改。这依赖于 toml_edit 库(也是 cargo-edit 的基础)。

当前状态是实验性的。当前接口尚未完全达到设计目标,预计会有不兼容的变更

基本用法

读取:toml get

要读取特定数据,传递一个 TOML 路径(由路径段组成):

$ toml get Cargo.toml bin[0]  # 获取Cargo.toml中bin数组第一个元素
{"name":"toml","path":"src/main.rs"}

当数据是字符串时,--raw/-r 选项直接打印:

$ toml get Cargo.toml dependencies.serde --raw  # 获取serde依赖版本(原始字符串)
1.0

写入:toml set

要编辑数据,传递一个 TOML 路径和要设置的值:

$ cat >foo.toml <<EOF  # 创建测试文件
[a]
b = "c"
EOF

$ toml set foo.toml x.y z  # 添加新字段x.y
[a]
b = "c"

[x]
y = "z"

完整示例

示例1:读取嵌套值

# config.toml
[database]
url = "postgres://user:pass@localhost:5432/db"
pool = 10

[server]
port = 8080
# 读取数据库URL(原始字符串)
$ toml get config.toml database.url --raw
postgres://user:pass@localhost:5432/db

# 读取服务器端口(JSON格式)
$ toml get config.toml server.port
8080

示例2:修改配置文件

# app.toml
[logging]
level = "info"
path = "/var/log/app.log"
# 添加新配置项 max_size
$ toml set app.toml logging.max_size 10MB
[logging]
level = "info"
path = "/var/log/app.log"
max_size = "10MB"

# 修改现有配置项 level
$ toml set app.toml logging.level debug
[logging]
level = "debug"
path = "/var/log/app.log"

示例3:处理数组

# services.toml
[[servers]]
name = "web"
port = 80

[[servers]]
name = "api" 
port = 3000
# 读取第一个服务器名称(原始字符串)
$ toml get services.toml servers[0].name --raw
web

# 添加新服务器(索引从0开始,所以新服务器是servers[2])
$ toml set services.toml servers[2].name db
$ toml set services.toml servers[2].port 5432
[[servers]]
name = "web"
port = 80

[[servers]]
name = "api"
port = 3000

[[servers]]
name = "db"
port = 5432

安装

cargo install toml-cli  # 使用cargo安装toml-cli工具

安装后即可使用 toml 命令。

完整示例代码

以下是一个完整的演示脚本,展示了toml-cli的各种用法:

#!/bin/bash

# 创建测试配置文件
cat >demo.toml <<EOF
[app]
name = "demo"
version = "0.1.0"

[database]
host = "localhost"
port = 5432

[[users]]
name = "Alice"
role = "admin"

[[users]]
name = "Bob"
role = "user"
EOF

echo "初始配置文件内容:"
cat demo.toml
echo ""

# 查询应用名称
echo "应用名称:"
toml get demo.toml app.name --raw

# 查询数据库端口(JSON格式)
echo "数据库端口:"
toml get demo.toml database.port

# 添加新配置项
echo "添加新配置项后:"
toml set demo.toml app.author "John Doe"

# 修改现有配置
echo "修改数据库主机后:"
toml set demo.toml database.host "db.example.com"

# 添加新用户
echo "添加新用户后:"
toml set demo.toml users[2].name "Charlie"
toml set demo.toml users[2].role "guest"

# 最终配置文件内容
echo "最终配置文件内容:"
cat demo.toml

运行此脚本将创建一个demo.toml文件,并演示各种查询和修改操作,最后显示修改后的完整配置文件内容。


1 回复

Rust TOML处理工具toml-cli的使用:高效解析、编辑和操作TOML配置文件

toml-cli 是一个强大的命令行工具,用于解析、编辑和操作TOML配置文件。它特别适合在Shell脚本或自动化流程中处理TOML文件。

安装方法

使用Cargo安装:

cargo install toml-cli

基本功能和使用示例

1. 获取TOML文件中的值

# 获取根级键值
toml get config.toml title

# 获取嵌套值
toml get config.toml database.port

2. 设置/修改TOML值

# 设置根级键值
toml set config.toml title "New Title"

# 设置嵌套值
toml set config.toml database.port 5432

3. 删除TOML键

toml delete config.toml database.password

4. 合并TOML文件

toml merge base.toml override.toml > merged.toml

5. 验证TOML文件

toml validate config.toml

6. 格式化TOML文件

toml format config.toml

高级用法示例

在Shell脚本中使用

#!/bin/bash

# 读取配置值
PORT=$(toml get config.toml server.port)
echo "Server port is: $PORT"

# 修改配置
toml set config.toml server.port 8080

使用jq风格的查询

# 获取所有数据库配置
toml get config.toml database

# 获取数组元素
toml get config.toml servers[0].name

创建新的TOML文件

echo "{}" > new.toml
toml set new.toml title "My Config"
toml set new.toml author.name "John Doe"

输出格式控制

# JSON格式输出
toml get --output-format json config.toml database

# YAML格式输出
toml get --output-format yaml config.toml database

完整示例demo

以下是一个完整的TOML配置文件处理示例,展示如何使用toml-cli进行常见操作:

#!/bin/bash

# 创建新的TOML配置文件
echo "Creating new config file..."
echo "{}" > app_config.toml

# 添加基本配置
toml set app_config.toml app.name "My Application"
toml set app_config.toml app.version "1.0.0"
toml set app_config.toml app.debug false

# 添加数据库配置
toml set app_config.toml database.host "localhost"
toml set app_config.toml database.port 3306
toml set app_config.toml database.user "admin"
toml set app_config.toml database.password "secret"

# 添加服务器数组配置
toml set app_config.toml servers[0].name "production"
toml set app_config.toml servers[0].ip "192.168.1.100"
toml set app_config.toml servers[1].name "staging"
toml set app_config.toml servers[1].ip "192.168.1.101"

# 显示配置文件内容
echo "Current config file content:"
cat app_config.toml

# 查询特定配置值
APP_NAME=$(toml get app_config.toml app.name)
DB_PORT=$(toml get app_config.toml database.port)
echo "Application name: $APP_NAME"
echo "Database port: $DB_PORT"

# 修改配置值
echo "Updating configuration..."
toml set app_config.toml database.port 5432
toml set app_config.toml app.debug true

# 删除敏感信息
toml delete app_config.toml database.password

# 验证配置文件
echo "Validating config file..."
toml validate app_config.toml

# 格式化配置文件
echo "Formatting config file..."
toml format app_config.toml

# 导出为JSON格式
echo "Exporting database config as JSON:"
toml get --output-format json app_config.toml database

# 创建覆盖文件用于合并
echo "Creating override file..."
echo "[database]
port = 8000
max_connections = 100" > override.toml

# 合并配置文件
echo "Merging config files..."
toml merge app_config.toml override.toml > merged_config.toml

echo "All operations completed!"

toml-cli 是处理TOML配置文件的强大工具,特别适合在自动化脚本和CI/CD流程中使用。它的简单API和管道友好设计使其成为Rust生态系统中处理TOML的首选命令行工具。

回到顶部