1 // Configuration file parsing and validation
3 // Copyright (C) 2019 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Affero General Public License for more details.
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program. If not, see <https://www.gnu.org/licenses/>.
24 "github.com/BurntSushi/toml"
29 Files []File `toml:"file"`
40 body []byte // internally used by handleFiles()
43 //go:generate stringer -type=FileType
47 FileTypePlain FileType = iota
52 func (t *FileType) UnmarshalText(text []byte) error {
61 return fmt.Errorf("invalid file type %q", text)
66 func LoadConfig(path string) (*Config, error) {
69 md, err := toml.DecodeFile(path, &cfg)
73 undecoded := md.Undecoded()
74 if len(undecoded) != 0 {
75 return nil, fmt.Errorf("invalid fields used: %q", undecoded)
78 f, err := os.Stat(path)
82 perms := f.Mode().Perm()
83 unsafe := (perms & 0077) != 0 // readable by others
85 if cfg.StatePath == "" {
86 return nil, fmt.Errorf("statepath must not be empty")
89 for i, f := range cfg.Files {
91 return nil, fmt.Errorf(
92 "file[%d].url must not be empty", i)
95 return nil, fmt.Errorf(
96 "file[%d].path must not be empty", i)
98 if (f.Username != "" || f.Password != "") && unsafe {
99 return nil, fmt.Errorf(
100 "file[%d].username/passsword in use and "+
101 "unsafe permissions %v on %q",