Mercurial > projects > sievemgr
comparison cmd/sievemgr/main.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 | f925f15d8ce5 |
children | 2130614cd64a |
comparison
equal
deleted
inserted
replaced
4:f925f15d8ce5 | 5:4dff4c3f0fbb |
---|---|
24 import ( | 24 import ( |
25 "errors" | 25 "errors" |
26 "flag" | 26 "flag" |
27 "fmt" | 27 "fmt" |
28 "os" | 28 "os" |
29 | |
30 "go.guido-berhoerster.org/sievemgr/cmd/sievemgr/internal/config" | |
29 ) | 31 ) |
30 | 32 |
31 const ( | 33 const ( |
32 exitSuccess = iota | 34 exitSuccess = iota |
33 exitFailure | 35 exitFailure |
38 | 40 |
39 func (e usageError) Error() string { | 41 func (e usageError) Error() string { |
40 return string(e) | 42 return string(e) |
41 } | 43 } |
42 | 44 |
45 // command-line flags | |
43 var ( | 46 var ( |
44 skipCertVerify bool | 47 confFilename string |
45 username string | 48 acctName string |
46 passwordFilename string | |
47 ) | 49 ) |
50 | |
51 var conf config.Configuration | |
52 | |
53 func init() { | |
54 var err error | |
55 confFilename, err = config.DefaultFilename() | |
56 if err != nil { | |
57 fmt.Fprintln(flag.CommandLine.Output(), err) | |
58 os.Exit(exitFailure) | |
59 } | |
60 } | |
48 | 61 |
49 var cmds = []*command{ | 62 var cmds = []*command{ |
50 cmdList, | 63 cmdList, |
51 cmdPut, | 64 cmdPut, |
52 cmdGet, | 65 cmdGet, |
69 flag.PrintDefaults() | 82 flag.PrintDefaults() |
70 } | 83 } |
71 | 84 |
72 func main() { | 85 func main() { |
73 flag.Usage = usage | 86 flag.Usage = usage |
74 flag.BoolVar(&skipCertVerify, "I", false, | 87 flag.StringVar(&confFilename, "f", confFilename, |
75 "Skip TLS certificate verification") | 88 "Set the name of the configuration file") |
76 flag.Parse() | 89 flag.Parse() |
77 if flag.NArg() == 0 { | 90 if flag.NArg() == 0 { |
78 fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand") | 91 fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand") |
79 usage() | 92 usage() |
80 os.Exit(exitUsage) | 93 os.Exit(exitUsage) |
94 } | |
95 | |
96 if err := config.ParseFile(confFilename, &conf); err != nil { | |
97 fmt.Fprintln(flag.CommandLine.Output(), err) | |
98 os.Exit(exitFailure) | |
81 } | 99 } |
82 | 100 |
83 name := flag.Arg(0) | 101 name := flag.Arg(0) |
84 var cmd *command | 102 var cmd *command |
85 for _, c := range cmds { | 103 for _, c := range cmds { |