#config-file #configuration-language #configuration #compiler #cli #single-file

bin+lib onefig

您唯一需要的配置语言;配置文件的配置语言

6 个稳定版本

1.3.4 2024年8月11日
1.3.3 2023年12月21日
1.3.2 2023年11月27日
1.3.0 2023年11月19日
1.2.4 2023年11月19日

#89 in 配置

Download history 10/week @ 2024-07-01 66/week @ 2024-08-05 49/week @ 2024-08-12

每月115次下载

GPL-3.0-or-later

62KB
1.5K SLoC

onefig

一个配置文件;您需要的所有配置。Onefig 消除了多个配置文件的杂乱,这是基于 Unix 的系统中常见的问题,尤其是在 NixOS 中。通过将配置设置合并到一个语言、一个文件或目录中,Onefig 提高了组织、对系统功能的控制以及该系统的真正可重复性。

安装


您需要

  • 一个 cargo 安装
  • 一个 unix 操作系统 (目前)

安装

  1. 运行 cargo install onefig 来安装
  2. 如果您还没有添加,请将 $HOME/.cargo/bin 添加到您的路径中
    • 您可以通过将 PATH 变量设置为 $HOME/.cargo/bin:$PATH
  3. 运行 onefig test 来测试是否一切正常
  4. 现在运行 onefig --help 获取使用 CLI 的帮助 :D

语言特性


Onefig 被设计得既简单又简洁,同时足够灵活,可以结合最常见的配置语言的功能;这体现在语言的功能中。

example.nf

# An example onefig comment
// Another example onefig comment (they both work)

// it's generally prefered to use `//` for commenting about configurations
# while `#` is used for commenting out code

############################
# SCALAR / PRIMATIVE TYPES #
############################

# Our root object (which continues for the entire document) will be a map,
# which is equivalent to a dictionary, hash or object in other languages.

key = "value"; // configurations have the syntax of `key set-symbol value`
string = "hello" // semi-colons are optional though prefered
number: 42; // `:` is another valid set symbol along with `=`
float = 3.14, // `,` can also be used instead of `;`
boolean: true;
multi-line_string: "first line\nand second line"; // escape characters are `\n`, `\t` and `\'` or `\"`
path = bob.dog.name;
raw: </
		&)(*&%)(*&)@(#*$&)(*@&#$)
		woah, it's a statement that onefig may not support but allows anyways!
		(this gets directly written into the target configuration file)
	 \>;

"keys can also be quoted" = 'value'; // Both " and ' work
valid-Key123_name456 = true; // numbers, letters, `-` and `_` are all valid bare keys
12 = "number?"; // valid but discouraged

# A bare-quoted key is valid though heavily discouraged
"" = "blank"
'' = 'something blank' // valid but still, don't do it

# Two scalar types cannot be assigned to the same key
dont-do-this = true; // works
dont-do-this = false; // throws error as the two definitions conflict

####################
# COLLECTION TYPES #
####################

##########
# Arrays #
##########

array1 = [ 1, 2, 3 ]
array2 = [ "commas" "don't" "really"; "matter" ] // you can also use semi-colons
array3 = [ [1.2, "nice"] "types don't really matter" ]
array4 = [ "unless" "you", "compile" "to", "toml", "then" "types"; "matter" ] // see, commas don't matter
array5: [
    "whitespace"
    "never"
    "matters"
]

# Two arrays cannot be assigned to the same key (though this may change in future updates)
dont-do-this = [ 1, 2, 3 ]; // works
dont-do-this = [ 1 2 3 ]; // throws error as the two definitions conflict

##########
# Tables #
##########

# Tables are like another scope with the file as a root table
jim = {
    name = "Jim Johnson";
    "age" = 23;
}

# Tables get combined NOT overwritten
stewart = {
    name = "Stewart Lee"
}
stewart: { // gets combined as stewart = { name = "Stewart Lee" age = 94 };
    age = 94;
}

# Tables (or hash tables or dictionaries) are collections of key/value
# pairs. They appear in square brackets on a line by themselves.
# Empty tables are allowed and simply have no key/value pairs within them.

empty-table = {};

# Dots are prohibited in bare keys because dots are used to signify nested tables.
# Naming rules for each dot separated part are the same as for keys.
dave.dog.name = "Biscut"; // same as `dave: { dog: { name = "Biscut" } };`

# Tables are automatically initialised when using dot keys.
another.dot.key = 2; // automatically initalises tables `another: { dot: { key = 2 } };`

# Two tables actually can be assigned to the same key
actually_do-this = { one: "dog", three: "fish" }; // works
actually_do-this = { two = "cat" }; // the two tables get merged, `actually_do-this` becomes `{ one: "dog", two: "cat", three: "fish" }`
actually_do-this.four = "bird"; // same applies to dot keys

######################
# Special Statements #
######################

# There are quite a few special statements that define different things and perform different actions in onefig:
#	 - config file statements
#	 - import statements
#	 - include statements
#	 - shell commands

##########################
# Config File Statements #
##########################

# they define a target configuration file that onefig writes to

# valid config file types are (this may change in the future):
# 	- json
# 	- nix
# 	- toml

# config file statement syntax
// conff <config file type> <table name>   : <config file path>
   conff json               my-config-file : "my-config-file.json";

# they can then be written to
my-config-file.data = "Hello, world!"; // compiles to json `{ "data": "Hello, world" }`

###############################
# Import & Include Statements #
###############################

# Import statements simply import the configurations from another onefig file as if it were one large file instead of multiple
# While include statements include the contents of a specified file into the compiled binary and copies it to the desired place on running of the binary

# import statment syntax
// import <config file path>
   import "another-config-file.nf";

# include statment syntax
// include <current file-path> as <target file-path>
   include "example-file.jpg"  as "desired/example-file.jpg";
   
#####################################
# Shell Commands & Apply Operations #
#####################################

# Shell commands are executed once their parent conff (config file) is evaluated

# Shell command syntax
<parent conff> $ <shell command>
example-config $ echo "example config evaluated! :D";

# Apply operations are an operation in which all the paths in an array are prefixed with another path
# eg
paths = prefix >> [
	one,
	two,
	three,
	four,
	five,
	"not a path",
];
// same as
paths = [
	prefix.one,
	prefix.two,
	prefix.three,
	prefix.four,
	prefix.five,
	"not a path",
];

依赖项

~3–11MB
~103K SLoC