aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: f8fa84092a69e4fa53451ff55c59b3fbcb518994 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# conf

[![🌱 Organic Code -- Code written by human](https://oc.neonxp.ru/organiccode.svg)](https://oc.neonxp.ru)
[![Go Doc](https://pkg.go.dev/badge/go.neonxp.ru/conf.svg)](https://pkg.go.dev/go.neonxp.ru/conf)

Go библиотека для парсинга конфигурационных файлов `.conf` похожем на
классические UNIX конфиги, как у nginx или bind9.

[English version](#english)

## Установка

```bash
go get go.neonxp.ru/conf
```

## Особенности формата

- **Директивы**: `directive arg1 arg2;`
- **Директивы с телом**: `directive arg1 arg2 { ... }`
- **Типы аргументов**: строки (двойные/одинарные кавычки/backticks для многострочных строк), числа (целые/дробные), булевы значения
- **Вложенные блоки**: произвольная глубина вложенности
- **Комментарии**: `#` до конца строки
- **UTF-8**: включая кириллицу

## Быстрый старт

```go
package main

import (
    "fmt"
    "go.neonxp.ru/conf"
)

func main() {
    // Загрузка из файла
    cfg, err := conf.LoadFile("config.conf")
    if err != nil {
        panic(err)
    }

    // Получение директивы и её значения
    if hostCmd := cfg.Get("server"); hostCmd != nil {
        fmt.Printf("Server: %v\n", hostCmd.Value())
    }

    // Навигация по вложенной структуре
    sslEnabled := cfg.Get("server").Group().Get("ssl").Group().Get("enabled")
    fmt.Printf("SSL enabled: %v\n", sslEnabled.Value())
}
```

## Пример конфигурационного файла

```conf
# Простые директивы без тела
listen 8080;
host "127.0.0.1";
debug false;

# Директивы с аргументами и телом
server "web" {
    host "localhost";
    port 8080;

    ssl {
        enabled true;
        cert "/etc/ssl/cert.pem";
        key "/etc/ssl/key.pem";
    }

    middleware "auth" {
        enabled true;
        secret "secret123";
    }
}

# Несколько директив с одинаковым именем
cache "redis" {
    host "redis.local";
    port 6379;
}

cache "memcached" {
    host "memcached.local";
    port 11211;
}

# Многострочные строки
template `
    <!DOCTYPE html>
    <html>
        <body>Hello</body>
    </html>
`;
```

## Требования

- Go 1.23+ (для использования `iter.Seq`)

## Лицензия

Этот проект лицензирован в соответствии с GNU General Public License версии 3
(GPLv3). Подробности смотрите в файле [LICENSE](LICENSE).

```
                    GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2026 Alexander NeonXP Kiryukhin <i@neonxp.ru>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
```

## Автор

- Александр Кирюхин <i@neonxp.ru>

---

<a name="english"></a>

# conf (English)

[![🌱 Organic Code -- Code written by human](https://oc.neonxp.ru/organiccode.svg)](https://oc.neonxp.ru)
[![Go Doc](https://pkg.go.dev/badge/go.neonxp.ru/conf.svg)](https://pkg.go.dev/go.neonxp.ru/conf)

Go library for parsing `.conf` configuration files (like many classic UNIX programms like nginx or bind9).

## Installation

```bash
go get go.neonxp.ru/conf
```

## Format Features

- **Directives**: `directive arg1 arg2;`
- **Directives with body**: `directive arg1 arg2 { ... }`
- **Argument types**: strings (double/single quotes, backticks for multiline strings), numbers (integer/float), boolean values
- **Nested blocks**: arbitrary nesting depth
- **Comments**: `#` until end of line
- **UTF-8**: including Cyrillic

## Quick Start

```go
package main

import (
    "fmt"
    "go.neonxp.ru/conf"
)

func main() {
    // Load from file
    cfg, err := conf.LoadFile("config.conf")
    if err != nil {
        panic(err)
    }

    // Get directive and its value
    if hostCmd := cfg.Get("server"); hostCmd != nil {
        fmt.Printf("Server: %v\n", hostCmd.Value())
    }

    // Navigate through nested structure
    sslEnabled := cfg.Get("server").Group.Get("ssl").Group.Get("enabled")
    fmt.Printf("SSL enabled: %v\n", sslEnabled.Value())
}
```

## Example Configuration File

```conf
# Simple directives without body
listen 8080;
host "127.0.0.1";
debug false;

# Directives with arguments and body
server "web" {
    host "localhost";
    port 8080;

    ssl {
        enabled true;
        cert "/etc/ssl/cert.pem";
        key "/etc/ssl/key.pem";
    }

    middleware "auth" {
        enabled true;
        secret "secret123";
    }
}

# Multiple directives with same name
cache "redis" {
    host "redis.local";
    port 6379;
}

cache "memcached" {
    host "memcached.local";
    port 11211;
}

# Multiline strings
template `
    <!DOCTYPE html>
    <html>
        <body>Hello</body>
    </html>
`;
```

## Requirements

- Go 1.23+ (for `iter.Seq`)

## License

This project is licensed under GNU General Public License version 3 (GPLv3).
See [LICENSE](LICENSE) file for details.

```
                   GNU GENERAL PUBLIC LICENSE
                      Version 3, 29 June 2007

Copyright (C) 2026 Alexander NeonXP Kiryukhin <i@neonxp.ru>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
```

## Author

- Alexander Kiryukhin <i@neonxp.ru>