| 1 | package rsyncdconfig |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | |
| 7 | "github.com/BurntSushi/toml" |
| 8 | "github.com/gokrazy/rsync/rsyncd" |
| 9 | ) |
| 10 | |
| 11 | type SSHListener struct { |
| 12 | Address string `toml:"address"` |
| 13 | AuthorizedKeys string `toml:"authorized_keys"` |
| 14 | } |
| 15 | |
| 16 | type Listener struct { |
| 17 | HostKeyPath string `toml:"host_key_path"` |
| 18 | Rsyncd string `toml:"rsyncd"` |
| 19 | HTTPMonitoring string `toml:"http_monitoring"` |
| 20 | AnonSSH string `toml:"anon_ssh"` |
| 21 | AuthorizedSSH SSHListener `toml:"authorized_ssh"` |
| 22 | } |
| 23 | |
| 24 | type Config struct { |
| 25 | Listeners []Listener `toml:"listener"` |
| 26 | Modules []rsyncd.Module `toml:"module"` |
| 27 | DontNamespace bool `toml:"dont_namespace"` |
| 28 | } |
| 29 | |
| 30 | func FromString(input string) (*Config, error) { |
| 31 | var cfg Config |
| 32 | if _, err := toml.Decode(input, &cfg); err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | return &cfg, nil |
| 36 | } |
| 37 | |
| 38 | func FromFile(path string) (*Config, error) { |
| 39 | input, err := os.ReadFile(path) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | return FromString(string(input)) |
| 44 | } |
| 45 | |
| 46 | func FromDefaultFiles() (*Config, string, error) { |
| 47 | configDir, err := os.UserConfigDir() |
| 48 | if err != nil { |
| 49 | return nil, "", err |
| 50 | } |
| 51 | fn := filepath.Join(configDir, "gokr-rsyncd.toml") |
| 52 | cfg, err := FromFile(fn) |
| 53 | if err != nil { |
| 54 | return nil, "", err |
| 55 | } |
| 56 | return cfg, fn, nil |
| 57 | } |