comparison cmd/sievemgr/internal/config/config.go @ 5:4dff4c3f0fbb

Introduce configuration file where account information is specified Introduce a configuration file where account information must be specified instead of passing it with each invocation on the command line. Each account has a name by which it can be selected and one may be specified as the default account. This is intended to improve usability for productive usage. Enforce strict permissions since passwords may be specified for non-interactive usage. Remove command-line flags for passing account information.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue, 03 Nov 2020 23:44:45 +0100
parents
children
comparison
equal deleted inserted replaced
4:f925f15d8ce5 5:4dff4c3f0fbb
1 // Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 package config
23
24 import (
25 "bufio"
26 "bytes"
27 "fmt"
28 "os"
29 "path/filepath"
30 )
31
32 const bom = '\ufeff'
33
34 type ParserError struct {
35 Message string
36 LineNo int
37 }
38
39 func (e *ParserError) Error() string {
40 if e.LineNo > 0 {
41 return fmt.Sprintf("error in line %d: %s", e.LineNo, e.Message)
42 }
43 return e.Message
44 }
45
46 func NewParserError(message string, lineNo int) *ParserError {
47 return &ParserError{message, lineNo}
48 }
49
50 type Configuration struct {
51 Accounts []*Account
52 Default *Account
53 }
54
55 type Account struct {
56 Name string
57 Host string
58 Port string
59 User string
60 Password string
61 Insecure bool
62 }
63
64 func Parse(data []byte, conf *Configuration) error {
65 rd := bytes.NewReader(data)
66 s := newScanner(bufio.NewReader(rd))
67 p := &parser{s: s}
68 return p.parse(conf)
69 }
70
71 func DefaultFilename() (string, error) {
72 dir, err := os.UserConfigDir()
73 if err != nil {
74 return "", fmt.Errorf("failed to determine the name of the configuration file: %s", err)
75 }
76 return filepath.Join(dir, "sievemgr", "sievemgr.conf"), nil
77 }
78
79 func ParseFile(filename string, conf *Configuration) error {
80 f, err := os.Open(filename)
81 if os.IsNotExist(err) {
82 return nil
83 } else if err != nil {
84 return fmt.Errorf("failed to open configuration file: %s", err)
85 }
86 defer f.Close()
87
88 // configutaion file must be a regular file with no more than 0600
89 // permissions
90 info, err := f.Stat()
91 if err != nil {
92 return fmt.Errorf("failed to stat configuration file: %s", err)
93 }
94 mode := info.Mode()
95 if !mode.IsRegular() {
96 return fmt.Errorf("configuration file is not a regular file")
97 }
98 if perm := mode.Perm(); perm&0077 != 0 {
99 return fmt.Errorf("permissions %04o on configuration file are too open", perm)
100 }
101
102 s := newScanner(bufio.NewReader(f))
103 p := &parser{s: s}
104 return p.parse(conf)
105 }