2021-03-31 20:24:15 +03:00
|
|
|
# Examples
|
|
|
|
|
2021-05-17 22:06:52 +03:00
|
|
|
This directory contains an evolving selection of examples of Nickel programs.
|
2021-03-31 20:24:15 +03:00
|
|
|
|
|
|
|
## Execution
|
|
|
|
|
|
|
|
Please follow the main repository's README instructions to have a working nickel
|
2021-04-19 12:27:41 +03:00
|
|
|
executable. You can then either use the command line or launch an interactive
|
|
|
|
session. Play with the values to see contracts failing when fed with invalid
|
|
|
|
data!
|
2021-03-31 20:24:15 +03:00
|
|
|
|
|
|
|
### From the command line
|
|
|
|
|
|
|
|
- *Base values*: some examples just return numbers, strings or booleans. You can run them directly:
|
|
|
|
```
|
2021-04-19 12:11:10 +03:00
|
|
|
$ nickel -f fibonacci.ncl
|
2021-03-31 20:24:15 +03:00
|
|
|
Done: Num(55.0)
|
|
|
|
```
|
|
|
|
- *Configurations*: some return records representing configuration. Because of laziness, Nickel
|
|
|
|
won't currently print any readable result as is. Please use the `export`
|
|
|
|
subcommand to see the result serialized as JSON:
|
|
|
|
```
|
2021-04-19 12:11:10 +03:00
|
|
|
$ nickel -f merge-main.ncl export
|
2021-03-31 20:24:15 +03:00
|
|
|
{
|
|
|
|
"firewall": {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Alternatively, you can query a configuration or a sub-element to get
|
2022-02-23 13:32:10 +03:00
|
|
|
a list of attributes or the documentation:
|
2021-03-31 20:24:15 +03:00
|
|
|
|
|
|
|
```
|
2021-04-19 12:11:10 +03:00
|
|
|
$ nickel -f record-contract.ncl query kind
|
2021-03-31 20:24:15 +03:00
|
|
|
• contract: <ReplicationController, ReplicaSet, Pod>
|
|
|
|
• value: `ReplicationController
|
|
|
|
• documentation: The kind of the element being configured.
|
|
|
|
```
|
|
|
|
|
|
|
|
### From the REPL
|
|
|
|
|
|
|
|
First start the REPL:
|
|
|
|
|
|
|
|
```
|
2021-04-19 12:11:10 +03:00
|
|
|
$ nickel repl
|
2021-03-31 20:24:15 +03:00
|
|
|
nickel>
|
|
|
|
```
|
|
|
|
|
|
|
|
- Just import a file directly to evaluate it:
|
|
|
|
```
|
|
|
|
nickel> import "fibonacci.ncl"
|
|
|
|
55
|
|
|
|
|
|
|
|
nickel>
|
|
|
|
```
|
2022-02-23 13:32:10 +03:00
|
|
|
- Use `builtin.serialize` to have the same behavior as the `export` subcommand
|
2021-03-31 20:24:15 +03:00
|
|
|
and print the result as JSON:
|
|
|
|
|
|
|
|
```
|
2022-02-23 13:32:10 +03:00
|
|
|
nickel> builtin.serialize `Json (import "merge-main.ncl")
|
2021-03-31 20:24:15 +03:00
|
|
|
"{
|
|
|
|
\"firewall\": {
|
|
|
|
...
|
|
|
|
}"
|
|
|
|
|
|
|
|
nickel>
|
|
|
|
```
|
|
|
|
- Use `:query` to retrieve information and documentation about a field:
|
|
|
|
|
|
|
|
```
|
|
|
|
nickel>let config = import "record-contract.ncl"
|
|
|
|
nickel>:query config.kind
|
|
|
|
• contract: <ReplicationController, ReplicaSet, Pod>
|
|
|
|
• value: `ReplicationController
|
|
|
|
• documentation: The kind of the element being configured.
|
|
|
|
|
|
|
|
nickel>
|
|
|
|
```
|