Mercurial > projects > sievemgr
comparison cmd/sievemgr/internal/config/parser.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 | 854167f55839 |
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 "fmt" | |
26 "strconv" | |
27 ) | |
28 | |
29 type parser struct { | |
30 s *scanner | |
31 } | |
32 | |
33 func (p *parser) scanType(expectedTok token) (string, error) { | |
34 tok, lit, err := p.s.scan() | |
35 if err != nil { | |
36 return lit, NewParserError(err.Error(), p.s.line) | |
37 } else if tok != expectedTok { | |
38 return lit, NewParserError(fmt.Sprintf("expected %s, got %s %q", expectedTok, tok, lit), p.s.line) | |
39 } | |
40 return lit, nil | |
41 } | |
42 | |
43 func (p *parser) validateAccount(acct *Account) error { | |
44 if acct.Host == "" { | |
45 return NewParserError(fmt.Sprintf("no host specified for account %q", | |
46 acct.Name), 0) | |
47 } else if acct.User == "" { | |
48 return NewParserError(fmt.Sprintf("no user specified for account %q", | |
49 acct.Name), 0) | |
50 } | |
51 return nil | |
52 } | |
53 | |
54 func (p *parser) parse(conf *Configuration) error { | |
55 var acct *Account | |
56 var isDefault bool | |
57 var tok token | |
58 var lit string | |
59 var err error | |
60 parsing: | |
61 for { | |
62 tok, lit, err = p.s.scan() | |
63 if err != nil { | |
64 err = NewParserError(err.Error(), p.s.line) | |
65 break parsing | |
66 } | |
67 if tok == tokenIllegal { | |
68 err = NewParserError(fmt.Sprintf("illegal token %q", lit), p.s.line) | |
69 break parsing | |
70 } else if tok == tokenEOF { | |
71 break parsing | |
72 } else if tok != tokenIdent { | |
73 err = NewParserError(fmt.Sprintf("expected identifier, got %s %q", tok, lit), p.s.line) | |
74 break parsing | |
75 } | |
76 switch lit { | |
77 case "account": | |
78 if acct != nil { | |
79 if err = p.validateAccount(acct); err != nil { | |
80 break parsing | |
81 } | |
82 conf.Accounts = append(conf.Accounts, acct) | |
83 if isDefault { | |
84 conf.Default = acct | |
85 isDefault = false | |
86 } | |
87 } | |
88 | |
89 // account name | |
90 if lit, err = p.scanType(tokenString); err != nil { | |
91 break parsing | |
92 } | |
93 acct = &Account{ | |
94 Name: lit, | |
95 Port: "4190", | |
96 } | |
97 case "default": | |
98 isDefault = true | |
99 case "host": | |
100 if lit, err = p.scanType(tokenString); err != nil { | |
101 break parsing | |
102 } | |
103 acct.Host = lit | |
104 case "port": | |
105 if lit, err = p.scanType(tokenNumber); err != nil { | |
106 break parsing | |
107 } | |
108 var port int | |
109 if port, err = strconv.Atoi(lit); err != nil { | |
110 err = NewParserError(fmt.Sprintf("failed to parse port: %s", err), p.s.line) | |
111 break parsing | |
112 } else if port < 1 || port > 65535 { | |
113 err = NewParserError(fmt.Sprintf("invalid port number %d", port), p.s.line) | |
114 break parsing | |
115 } | |
116 acct.Port = lit | |
117 case "user": | |
118 if lit, err = p.scanType(tokenString); err != nil { | |
119 break parsing | |
120 } | |
121 acct.User = lit | |
122 case "pass": | |
123 if lit, err = p.scanType(tokenString); err != nil { | |
124 break parsing | |
125 } | |
126 acct.Password = lit | |
127 case "insecure": | |
128 acct.Insecure = true | |
129 default: | |
130 err = NewParserError(fmt.Sprintf("unknown %s: %q", tok, lit), p.s.line) | |
131 break parsing | |
132 } | |
133 } | |
134 if err != nil { | |
135 return err | |
136 } | |
137 if acct != nil { | |
138 if err = p.validateAccount(acct); err != nil { | |
139 return err | |
140 } | |
141 conf.Accounts = append(conf.Accounts, acct) | |
142 if isDefault { | |
143 conf.Default = acct | |
144 } | |
145 } | |
146 | |
147 return nil | |
148 } |