Merge pull request #23373 from unigeorge/unigeorge-branch

translated
This commit is contained in:
Xingyu.Wang 2021-09-28 08:30:47 +08:00 committed by GitHub
commit 8255264b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 173 additions and 184 deletions

View File

@ -1,184 +0,0 @@
[#]: subject: (What is a config file?)
[#]: via: (https://opensource.com/article/21/6/what-config-files)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
[#]: collector: (lujun9972)
[#]: translator: (unigeorge)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
What is a config file?
======
There are several popular formats for configuration files, each with its
own strengths. Find what works best for you.
![Computer screen with files or windows open][1]
There are thousands of configuration files on your computer. You may never directly interact with the bulk of them, but they're scattered throughout your `/etc` folder and in `~/.config` and `~/.local` and `/usr`. There are probably some in `/var` and possibly even in `/opt`. If you've ever opened one by accident or to make a change, you may have wondered why some configuration files look one way while others look completely different.
Storing configurations is a flexible task because as long as developers know how their code puts data into a file, they can easily write code to extract that data as needed. However, the tech industry graciously favors well-documented standardization, so several well-known formats have evolved over the years to make configuration easy.
### Why we need configuration
Configuration files ("config files" for short) are important to modern computing. They allow you to customize how you interact with an application or how an application interacts with the rest of your system. It's thanks to config files that any time you launch an application, it has "memories" of how you like to use it.
Configuration files can be, and often are, very simple in structure. For instance, if you were to write an application, and the only thing it ever needed to know was its user's preferred name, then its one and only config file could contain exactly one word: the name of the user. For example:
```
`Tux`
```
Usually, though, an application needs to keep track of more than just one piece of information, so configuration often uses a key and a value:
```
NAME='Tux'
SPECIES='Penguin'
```
Even without programming experience, you can imagine how code parses that data. Here are two simple examples, one using the [`awk` command][2] and the other using the [grep command][3], focusing on just the line containing the "key" of `NAME`, and returning the "value" appearing after the equal sign (`=`):
```
$ awk -F'=' '/NAME/ { print $2; }' myconfig.ini
'Tux'
$ grep NAME fake.txt | cut -d'=' -f2
'Tux'
```
The same principle applies to any programming language and any configuration file. As long as you have a consistent data structure, you can write simple code to extract and parse it when necessary.
### Choose a format
To be broadly effective, the most important thing about configuration files is that they are consistent and predictable. The last thing you want to do is dump information into a file under the auspices of saving user preferences and then spend days writing code to reverse-engineer the random bits of information that have ended up in the file.
There are several popular formats for configuration files, each with its own strengths.
#### INI
INI files take the format of key and value pairs:
```
[example]
name=Tux
style=widgety,fidgety
enabled=1
```
This simple style of configuration can be intuitive, with the only point of confusion being poor key names (for example, cryptic names like `unampref` instead of `name`). They're easy to parse and easy to edit.
The INI format features sections in addition to keys and values. In this sample code, `[example]` and `[demo]` are configuration sections:
```
[example]
name=Tux
style=widgety,fidgety
enabled=1
[demo]
name=Beastie
fullscreen=1
```
This is a little more complex to parse because there are _two_ `name` keys. You can imagine a careless programmer querying this config file for `name` and always getting back `Beastie` because that's the last name defined by the file. When parsing such a file, a developer must be careful to search within sections for keys, which can be tricky depending on the language used to parse the file. However, it's a popular enough format that most languages have an existing library to help programmers parse INI files.
#### YAML
[YAML files][4] are structured lists that can contain values or key and value pairs:
```
\---
Example:
  Name: 'Tux'
  Style:
   - 'widgety'
    - 'fidgety'
  Enabled: 1
```
YAML is popular partly because it looks clean. It doesn't have much of a syntax aside from where you place the data in relation to previous data. What's a feature for some, though, is a bug for others, and many developers avoid YAML because of the significance it places on what is essentially _not there_. If you get indentation wrong in YAML, YAML parsers may see your file as invalid, and even if it's tolerated, it may return incorrect data.
Most languages have YAML parsers, and there are good open source YAML linters (applications to validate syntax) to help you ensure the integrity of a YAML file.
#### JSON
JSON files are technically subsets of YAML, so its data structure is the same, although its syntax is completely different:
```
{
  "Example": {
    "Name": [
      "Tux"
    ],
    "Style": [
      "widgety",
      "fidgety"
    ],
    "Enabled": 1
  }
}
```
JSON is popular among JavaScript programmers, which isn't surprising, given that JSON stands for JavaScript Object Notation. As a result of being strongly associated with web development, JSON is a common output format for web APIs. Most programming languages have libraries to parse JSON.
#### XML
XML uses tags as keys that surround a configuration value:
```
<example>
  <name>Tux</name>
  <style priority="user">widgety</style>
  <style priority="fallback">fidgety</style>
  <enabled>1</enabled>
</example>
```
XML is often used by Java programmers, and Java has a rich set of XML parsers. While it has a reputation of being quite strict, XML is simultaneously very flexible. Unlike HTML, which has a set of tags you're allowed to use, you can arbitrarily invent your own XML tags. As long as you structure it consistently and have a good library to parse it, you can extract your data with precision and ease.
There are some good open source linters to help you validate XML files, and most programming languages have a library to parse XML.
#### Binary formats
Linux prides itself on plain-text configuration. The advantage is that you can see configuration data using basic tools like [cat][5], and you can even edit a configuration with your [favorite text editor][6].
Some applications use binary formats, though, which means the data is encoded in some format that is not a natural language. These files usually require a special application (usually the application they're meant to configure) to interpret their data.
You can't view these files, at least not in a way that makes any sense, and you can't edit them outside of their host application. Some reasons for resorting to binary formats are:
* **Speed:** A programmer can register specific bits of information at certain points within a binary's config file using custom notation. When the data is extracted, there's no searching involved because everything is already indexed.
* **Size:** Text files can get big, and should you choose to compress a text file, you're functionally turning it into a binary format. Binary files can be made smaller through tricks of encoding (the same is true of text files, but at some point, your optimizations make your data so obscure that it may as well be binary).
* **Obfuscation:** Some programmers don't want people even looking at their configuration files, so they encode them as binary data. This usually succeeds only in frustrating users. This is not a good reason to use binary formats.
If you must use a binary format for configuration, use one that already exists as an open standard, such as [NetCDF][7].
### Find what works
Configuration formats help developers store the data their applications need and help users store preferences for how they want applications to act. There's probably no wrong answer to the question of what format you should use, as long as you feel well supported by the language you're using. When developing your application, look at the formats available, model some sample data, review and evaluate the libraries and utilities your programming language provides, and choose the one you feel the most confident about.
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/6/what-config-files
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[unigeorge](https://github.com/unigeorge)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_screen_windows_files.png?itok=kLTeQUbY (Computer screen with files or windows open)
[2]: https://opensource.com/article/20/9/awk-ebook
[3]: https://opensource.com/downloads/grep-cheat-sheet
[4]: https://www.redhat.com/sysadmin/yaml-beginners
[5]: https://opensource.com/article/19/2/getting-started-cat-command
[6]: https://opensource.com/article/21/2/open-source-text-editors
[7]: https://www.unidata.ucar.edu/software/netcdf/

View File

@ -0,0 +1,173 @@
[#]: subject: (What is a config file?)
[#]: via: (https://opensource.com/article/21/6/what-config-files)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
[#]: collector: (lujun9972)
[#]: translator: (unigeorge)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
什么是配置文件?
======
流行的配置文件格式有若干种,每种都有其独特优势。从中找到最适合你的格式吧!
![Computer screen with files or windows open][1]
计算机上有数以千计的配置文件。你可能永远不会直接与其中的大部分文件打交道,但它们确实散落在你的 `/etc` 以及 `~/.config`、`~/.local`、`/usr` 文件夹中。还有一些可能在 `/var`,甚至 `/opt` 文件夹中。如果无意中打开过或更改过它们,你就可能会有疑问:为什么有些配置文件看起来是某一种格式,而另一些则是看起来完全不同的格式?
存储配置是一项很灵活的任务,因为只要开发人员知道他们的代码如何将数据存入文件,他们就可以轻松编写代码来根据需要提取数据。然而,科技行业非常青睐有详细文档的标准化事物,因此多年来出现了几种比较普遍的格式用来简化配置任务。
### 为什么我们需要配置文件
配置文件对于现代计算来说很重要。它们使你能够自定义与应用程序交互的方式,或自定义应用程序与系统内其他程序的交互方式。有了配置文件,每当你启动某个应用程序时,它都会有“记忆”,记录了你喜欢如何去使用该程序。
配置文件的结构可以很简单,而且通常确实也很简单。例如,如果你要编写一个应用程序,程序唯一需要知道的是其用户的偏好名称,那么它的唯一配置文件就可以只包含一个词:用户名。就像下面这样:
```
Tux
```
但通常应用程序需要追踪的不仅仅是一条信息,因此配置文件通常会有一个键和一个值:
```
NAME='Tux'
SPECIES='Penguin'
```
即使没有编程经验,你也可以想象出代码如何解析这些数据。这里有两个简单的例子,一个使用 [`awk` 命令][2],另一个使用 [grep 命令][3]。两个例子都是只关注包含 `NAME`“键”的行,并返回出现在等号 (`=`) 之后的“值”:
```
$ awk -F'=' '/NAME/ { print $2; }' myconfig.ini
'Tux'
$ grep NAME fake.txt | cut -d'=' -f2
'Tux'
```
同样的原则适用于任何编程语言和任何配置文件。只要你有统一的数据结构,就可以在需要的时候编写简单的代码来提取和解析它。
### 选择格式
为了保证普遍有效性,配置文件最重要的一点是它们是一致的和可预测的。你绝对不会想做这样的事:以保存用户首选项的名义,将信息随意存储到文件中,然后就得花好几天时间来编写代码,以对整个文件中的随机信息实现读取操作。
流行的配置文件格式有若干种,每种格式都有自己的优势。
#### INI
INI 文件采用了键值对的格式:
```
[example]
name=Tux
style=widgety,fidgety
enabled=1
```
这种简单的配置风格很直观,只要你别选择使用糟糕的键名(比如用 `unampref` 这样的神秘键名来代替 `name`)就好。这些键值对很容易解析和编辑。
除了键和值之外INI 格式还可以分 <ruby><rt>section</rt></ruby>。在下列示例代码中,`[example]` 和 `[demo]` 就是配置文件中的两节:
```
[example]
name=Tux
style=widgety,fidgety
enabled=1
[demo]
name=Beastie
fullscreen=1
```
这几个配置语句解析起来有点复杂,因为有 _两个_ `name` 键。想象一下,一个粗心的程序员在这个配置文件中查询 `name`,结果总是返回 `Beastie`,因为这是文件中对 name 的最后一个定义值。在解析这样的文件时,开发人员必须加倍小心地在各节中搜索键,这可能会很棘手,具体取决于用来解析该文件的语言。然而,它仍然是一种很流行的格式,大多数语言都会有一个现成的库来帮助程序员解析 INI 文件。
#### YAML
[YAML 文件][4] 是结构化列表,可以包含值或者键值对:
```
\---
Example:
  Name: 'Tux'
  Style:
   - 'widgety'
    - 'fidgety'
  Enabled: 1
```
YAML 格式很流行,部分原因是它看起来很整洁。数据要放置到相对其上层数据的特定位置,除此之外没有太多其他语法。然而,对于某些人来说的特色,在其他人眼中可能就是一个 bug。许多开发人员不愿使用 YAML正是因为它很看重本质上 _不存在_ 的东西。如果你在 YAML 中缩进错误YAML 解析器可能会将你的文件视为无效文件,即使不视为无效,返回的数据也可能是错误的。
大多数语言都有 YAML 解析器,并且有很好的开源 YAML linters验证语法的应用程序来帮你确保 YAML 文件的完整性。
#### JSON
JSON 文件在技术上来说是 YAML 的子类,因此其数据结构是相同的,尽管其语法完全不同:
```
{
  "Example": {
    "Name": [
      "Tux"
    ],
    "Style": [
      "widgety",
      "fidgety"
    ],
    "Enabled": 1
  }
}
```
JSON 在 JavaScript 程序员中很流行,这并不奇怪,因为 JSON 全称为 JavaScript Object Notation 即 JavaScript 对象符号。由于与 Web 开发密切相关JSON 是 Web API 的常见输出格式。大多数编程语言都有解析 JSON 的库。
#### XML
XML 使用标签作为键,将配置值围绕起来:
```
<example>
  <name>Tux</name>
  <style priority="user">widgety</style>
  <style priority="fallback">fidgety</style>
  <enabled>1</enabled>
</example>
```
XML 经常被 Java 程序员使用Java 有一套丰富的 XML 解析器。虽然 XML 以非常严格而著称,但同时也非常灵活。与有一系列特定标签的 HTML 不同XML 中可以随意发明自己的标签。只要始终坚持相同的构建规则,并有一个良好的库来解析它,你就可以准确而轻松地提取数据。
有一些很好的开源 linter 可以帮你验证 XML 文件,并且大多数编程语言都提供用于解析 XML 的库。
#### 二进制格式
Linux 以纯文本配置为傲。这样做的优点是可以使用 [cat][5] 等基本工具查看配置数据,甚至可以使用你 [最喜欢的文本编辑器][6] 来编辑配置。
但是,某些应用程序使用二进制格式配置,就意味着数据以某种非自然语言的格式进行编码。这些文件通常需要一个特殊的应用程序(通常是它们要配置的应用程序)来解释它们的数据。你无法查看这些文件,至少无法以任何有意义的方式查看,并且无法在其宿主应用程序之外编辑它们。选用二进制格式的一些原因如下:
* **速度:** 程序员可以使用自定义符号在二进制配置文件中的某些点注册特定的信息位。提取数据时不涉及搜索,因为所有内容都已标注了索引。
* **大小:** 文本文件可能会变大,如果选择压缩文本文件,实际上是在将其转换为二进制格式。二进制文件可以通过编码技巧变得更小(文本文件也是如此,但在某些时候,你的优化会使数据变得晦涩,以至于文件也成了二进制文件)。
* **晦涩:** 一些程序员甚至不希望人们查看他们的配置文件,因此将它们编码为二进制数据。这通常只会让用户感到沮丧,并不是使用二进制格式的好理由。
如果必须使用二进制格式进行配置,请使用已作为开放标准存在的格式,例如 [NetCDF][7]。
### 找到有效的配置格式
配置格式帮助开发人员存储应用程序所需的数据,并帮助用户存储他们希望应用程序如何操作的偏好项。对于应该使用什么格式的问题,可能没有错误的答案,只要你觉得所使用的语言能很好地支持就可以。在开发应用程序时,查看可用格式,用一些样例数据建模,查看和评估你的编程语言提供的库和实用程序,然后选择你觉得最合适的一种格式吧。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/6/what-config-files
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[unigeorge](https://github.com/unigeorge)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_screen_windows_files.png?itok=kLTeQUbY (Computer screen with files or windows open)
[2]: https://opensource.com/article/20/9/awk-ebook
[3]: https://opensource.com/downloads/grep-cheat-sheet
[4]: https://www.redhat.com/sysadmin/yaml-beginners
[5]: https://opensource.com/article/19/2/getting-started-cat-command
[6]: https://opensource.com/article/21/2/open-source-text-editors
[7]: https://www.unidata.ucar.edu/software/netcdf/