Skip to content

Commit

Permalink
config: switch to yaml
Browse files Browse the repository at this point in the history
1. Configuration is now strongly typed everywhere
2. Sample file is autogenerated from the Config struct
3. Strict parsing - specifying an illegal option will now panic
  • Loading branch information
pulsejet committed Dec 17, 2024
1 parent 0626273 commit 74a5b9d
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ yanfd: clean
install:
install -m 755 yanfd /usr/local/bin
mkdir -p /usr/local/etc/ndn
install -m 644 yanfd.toml.sample /usr/local/etc/ndn
install -m 644 yanfd.sample.yml /usr/local/etc/ndn

clean:
rm -f yanfd coverage.out
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ Get it from: https://www.microsoft.com/store/apps/9NBK3ZJT4CL8

### On MacOS/Linux
```bash
curl -o ./yanfd.toml https://raw.githubusercontent.com/named-data/YaNFD/master/yanfd.toml.sample
curl -o ./yanfd.yml https://raw.githubusercontent.com/named-data/YaNFD/master/yanfd.sample.yml
mkdir -p /usr/local/etc/ndn
install -m 644 ./yanfd.toml /usr/local/etc/ndn
rm ./yanfd.toml
install -m 644 ./yanfd.yml /usr/local/etc/ndn
rm ./yanfd.yml
```

On MacOS, one also needs to change `socket_path` to `/var/run/nfd/nfd.sock` in the copied configuration file.

### On Windows 10/11

```powershell
curl -o yanfd.toml https://raw.githubusercontent.com/named-data/YaNFD/master/yanfd.toml.sample
curl -o yanfd.yml https://raw.githubusercontent.com/named-data/YaNFD/master/yanfd.sample.yml
mkdir %APPDATA%\ndn
move yanfd.toml %APPDATA%\ndn\
move yanfd.yml %APPDATA%\ndn\
```

One needs to change `socket_path` to `${TEMP}\\nfd\\nfd.sock` in the copied configuration file.
Also, to execute YaNFD on Windows 10, one needs to explicitly specify the configuration path:

```bash
yanfd.exe --config=%APPDATA%\ndn\yanfd.toml
```powershell
yanfd.exe --config=%APPDATA%\ndn\yanfd.yml
```

# Building from source
Expand All @@ -71,7 +71,7 @@ To build and install YaNFD on Windows, please run the `go build` command in the
go build github.com/named-data/YaNFD/cmd/yanfd
```

At the moment, you will need to manually install the executable (`yanfd.exe`) and the configuration file (`yanfd.toml.sample`) to a location of your choice.
At the moment, you will need to manually install the executable (`yanfd.exe`) and the configuration file (`yanfd.sample.yml`) to a location of your choice.

# Configuration

Expand All @@ -81,8 +81,8 @@ Meanwhile, runtime configuration is used to create NDN faces, set routes and str

## Startup configuration

Startup configuration for YaNFD is performed via a TOML file, by default read from `/usr/local/etc/ndn/yanfd.toml` on Unix-like systems.
Note that you will need to copy the sample config file installed to `/usr/local/etc/ndn/yanfd.toml.sample` to this location before running YaNFD for the first time.
Startup configuration for YaNFD is performed via a YAML file, by default read from `/usr/local/etc/ndn/yanfd.yml` on Unix-like systems.
Note that you will need to copy the sample config file installed to `/usr/local/etc/ndn/yanfd.sample.yml` to this location before running YaNFD for the first time.
The configuration options are documented via comments in the sample file.

On Windows, at this time, you will need to specify the location of the configuration file manually when starting YaNFD via the `--config` argument.
Expand Down
88 changes: 88 additions & 0 deletions cmd/make_sample_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"reflect"
"strings"

"github.com/goccy/go-yaml"
"github.com/named-data/YaNFD/core"
)

func getDocstrings(
cm yaml.CommentMap,
jstr string,
srt *ast.StructType,
obj reflect.Type,
) {
for _, field := range srt.Fields.List {
name := field.Names[0].Name
obj2, _ := obj.FieldByName(name)
jstr2 := jstr + "." + obj2.Tag.Get("json")

text := strings.TrimSpace(field.Doc.Text())
if len(text) > -1 {
lines := strings.Split(text, "\n")
for i, line := range lines {
lines[i] = " " + strings.TrimSpace(line)
}

cm[jstr2] = []*yaml.Comment{{
Texts: lines,
Position: yaml.CommentHeadPosition,
}}
}

if srt2, ok := field.Type.(*ast.StructType); ok {
getDocstrings(cm, jstr2, srt2, obj2.Type)
}
}
}

func main() {
f, err := parser.ParseFile(token.NewFileSet(), "core/config.go", nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
return
}

cObj := *core.DefaultConfig()
cSrt := f.Scope.
Lookup("Config").
Decl.(*ast.TypeSpec).
Type.(*ast.StructType)
cm := yaml.CommentMap{}
getDocstrings(cm, "$", cSrt, reflect.TypeOf(cObj))

file, err := os.Create("yanfd.sample.yml")
if err != nil {
panic(err)
}
defer file.Close()

bytes, err := yaml.MarshalWithOptions(cObj, yaml.WithComment(cm))
if err != nil {
panic(err)
}

// This is a hack to get the right spacing. For each line, if it is
// just a comment (#), then get rid of it altogether
lines := strings.Split(string(bytes), "\n")
for i, line := range lines {
if strings.TrimSpace(line) == "#" {
lines[i] = ""
}
}

// Join all lines with newline
output := strings.Join(lines, "\n")

// Write to file
file.WriteString("# YaNFD Sample Configuration\n")
file.WriteString("# This file is autogenerated by cmd/make_sample_config.go\n")
file.WriteString(output)
}
2 changes: 1 addition & 1 deletion cmd/yanfd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
var shouldPrintVersion bool
flag.BoolVar(&shouldPrintVersion, "version", false, "Print version and exit")
var configFileName string
flag.StringVar(&configFileName, "config", "/usr/local/etc/ndn/yanfd.toml", "Configuration file location")
flag.StringVar(&configFileName, "config", "/usr/local/etc/ndn/yanfd.yml", "Configuration file location")
var disableUnix bool
flag.BoolVar(&disableUnix, "disable-unix", false,
"Disable Unix stream transports (deprecated; set.faces.unix.enabled=false in config file instead)")
Expand Down
Loading

0 comments on commit 74a5b9d

Please sign in to comment.