aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: d469deece2a7e8afda4121d0398ade5c0930d12d (plain)
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
# Config

Extensible configuration class for the Raku programming language.

## Installation

This module can be installed using `zef`:

```
zef install Config
```

Depending on the type of configuration file you want to work on, you will need a
`Config::Parser::` module as well. If you just want an easy-to-use configuration
object without reading/writing a file, no parser is needed.

## Usage
Include the `Config` module in your script, instantiate it and use it as you
please.

```raku
use Config;

my Config $config = Config.new();

# Load a simple configuration hash
$config.read({
    keyOne => "value",
    keyTwo => {
        NestedKey => "other value"
    }
});

# Load a configuration files
$config.read("/etc/config.yaml");

# Load a configuration file with a specific parser
$config.read("/etc/config", "Config::Parser::ini");

# Retrieve a simple key
$config.get("keyOne");

# As of v1.2.0, `Config` support associative indexing:
$config<keyOne>;

# Retrieve a nested key
$config.get("keyTwo.NestedKey");

# Write out the configuration file
$config.write("/etc/config.yaml");

# Write out the configuration in another format
$config.write("/etc/config.json", "Config::Parser::json");
```

### Available parsers

Because there's so many ways to structure your configuration files, the parsers
for these are their own modules. This allows for easy implementing new parsers,
or providing a custom parser for your project's configuration file.

The parser will be loaded during runtime, but you have to make sure it is
installed yourself.

The following parsers are available:

- json:
  - [`Config::Parser::json`](https://github.com/arjancwidlak/p6-Config-Parser-json)
  - [`Config::Parser::json`](https://github.com/robertlemmen/perl6-config-json)
- [`Config::Parser::toml`](https://github.com/scriptkitties/p6-Config-Parser-toml)
- [`Config::Parser::yaml`](https://github.com/scriptkitties/p6-Config-Parser-yaml)

### Writing your own parser

If you want to make your own parser, simply make a new class which extends the
`Config::Parser` class, and implements the `read` and `write` methods. The
`read` method *must* return a `Hash`. The `write` method *must* return a
`Bool`, `True` when writing was successful, `False` if not. Throwing
`Exception`s to indicate the kind of failure is recommended.

## Contributing

If you want to contribute to `Config`, you can do so by mailing your patches to
`~tyil/raku-devel@lists.sr.ht`. Any questions or other forms of feedback are
welcome too!

## License

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.