comparison cmd/sievemgr/put.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 fc5e6970a0d5
comparison
equal deleted inserted replaced
4:f925f15d8ce5 5:4dff4c3f0fbb
22 package main 22 package main
23 23
24 import ( 24 import (
25 "fmt" 25 "fmt"
26 "io" 26 "io"
27 "net"
28 "os" 27 "os"
29 28
30 "go.guido-berhoerster.org/managesieve" 29 "go.guido-berhoerster.org/managesieve"
31 ) 30 )
32 31
33 func init() { 32 func init() {
34 cmdPut.Flag.StringVar(&username, "u", "", "Set the username") 33 cmdPut.Flag.StringVar(&acctName, "a", "", "Select the account")
35 cmdPut.Flag.StringVar(&passwordFilename, "P", "", 34 cmdCheck.Flag.StringVar(&acctName, "a", "", "Select the account")
36 "Set the name of the password file")
37 cmdCheck.Flag.StringVar(&username, "u", "", "Set the username")
38 cmdCheck.Flag.StringVar(&passwordFilename, "P", "",
39 "Set the name of the password file")
40 } 35 }
41 36
42 var cmdPut = &command{ 37 var cmdPut = &command{
43 UsageLine: "put [options] host[:port] name [file]", 38 UsageLine: "put [options] name [file]",
44 Run: runPut, 39 Run: runPut,
45 } 40 }
46 41
47 var cmdCheck = &command{ 42 var cmdCheck = &command{
48 UsageLine: "check [options] host[:port] [file]", 43 UsageLine: "check [options] [file]",
49 Run: runPut, 44 Run: runPut,
50 } 45 }
51 46
52 func runPut(cmd *command, args []string) error { 47 func runPut(cmd *command, args []string) error {
48 var err error
49 acct, err := getAccount(&conf, acctName)
50 if err != nil {
51 return err
52 }
53
53 var scriptName string 54 var scriptName string
54 var r io.Reader = os.Stdin 55 var r io.Reader = os.Stdin
55 var host, port string
56 var err error
57 if cmd.Name() == "put" { 56 if cmd.Name() == "put" {
58 switch len(args) { 57 switch len(args) {
59 case 3: // name and filename 58 case 2: // name and filename
60 scriptFile, err := os.Open(args[2])
61 if err != nil {
62 return fmt.Errorf("failed to open script file: %s\n",
63 err)
64 }
65 defer scriptFile.Close()
66 r = scriptFile
67 fallthrough
68 case 2: // only name
69 host, port, err = parseHostPort(args[0])
70 if err != nil {
71 return err
72 }
73
74 scriptName = args[1]
75 default:
76 return usageError("invalid number of arguments")
77 }
78 } else if cmd.Name() == "check" {
79 switch len(args) {
80 case 2: // filename
81 scriptFile, err := os.Open(args[1]) 59 scriptFile, err := os.Open(args[1])
82 if err != nil { 60 if err != nil {
83 return fmt.Errorf("failed to open script file: %s\n", 61 return fmt.Errorf("failed to open script file: %s\n",
84 err) 62 err)
85 } 63 }
86 defer scriptFile.Close() 64 defer scriptFile.Close()
87 r = scriptFile 65 r = scriptFile
88 fallthrough 66 fallthrough
89 case 1: 67 case 1: // only name
90 host, port, err = parseHostPort(args[0]) 68 scriptName = args[0]
91 if err != nil {
92 return err
93 }
94 default: 69 default:
95 return usageError("invalid number of arguments") 70 return usageError("invalid number of arguments")
96 } 71 }
97 } else if cmd.Name() == "check" { 72 } else if cmd.Name() == "check" {
98 switch len(args) { 73 switch len(args) {
113 script, err := readLimitedString(r, managesieve.ReadLimit) 88 script, err := readLimitedString(r, managesieve.ReadLimit)
114 if err != nil { 89 if err != nil {
115 return fmt.Errorf("failed to read script: %s\n", err) 90 return fmt.Errorf("failed to read script: %s\n", err)
116 } 91 }
117 92
118 username, password, err := usernamePassword(host, port, username, 93 if err := lookupHostPort(acct); err != nil {
119 passwordFilename)
120 if err != nil {
121 return err 94 return err
122 } 95 }
123 96
124 c, err := dialPlainAuth(net.JoinHostPort(host, port), username, 97 if err := readPassword(acct); err != nil {
125 password) 98 return err
99 }
100
101 c, err := dialPlainAuth(acct)
126 if err != nil { 102 if err != nil {
127 return err 103 return err
128 } 104 }
129 defer c.Logout() 105 defer c.Logout()
130 106