comparison cmd/sievemgr/put.go @ 4:f925f15d8ce5

Add check subcommand for checking the script validity
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue, 27 Oct 2020 19:17:56 +0100
parents 3cd182bff992
children 4dff4c3f0fbb
comparison
equal deleted inserted replaced
3:3cd182bff992 4:f925f15d8ce5
32 32
33 func init() { 33 func init() {
34 cmdPut.Flag.StringVar(&username, "u", "", "Set the username") 34 cmdPut.Flag.StringVar(&username, "u", "", "Set the username")
35 cmdPut.Flag.StringVar(&passwordFilename, "P", "", 35 cmdPut.Flag.StringVar(&passwordFilename, "P", "",
36 "Set the name of the password file") 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")
37 } 40 }
38 41
39 var cmdPut = &command{ 42 var cmdPut = &command{
40 UsageLine: "put [options] host[:port] name [file]", 43 UsageLine: "put [options] host[:port] name [file]",
44 Run: runPut,
45 }
46
47 var cmdCheck = &command{
48 UsageLine: "check [options] host[:port] [file]",
41 Run: runPut, 49 Run: runPut,
42 } 50 }
43 51
44 func runPut(cmd *command, args []string) error { 52 func runPut(cmd *command, args []string) error {
45 var scriptName string 53 var scriptName string
46 var r io.Reader = os.Stdin 54 var r io.Reader = os.Stdin
47 var host, port string 55 var host, port string
48 var err error 56 var err error
49 switch len(args) { 57 if cmd.Name() == "put" {
50 case 3: // name and filename 58 switch len(args) {
51 scriptFile, err := os.Open(args[2]) 59 case 3: // name and filename
52 if err != nil { 60 scriptFile, err := os.Open(args[2])
53 return fmt.Errorf("failed to open script file: %s\n", 61 if err != nil {
54 err) 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")
55 } 77 }
56 defer scriptFile.Close() 78 } else if cmd.Name() == "check" {
57 r = scriptFile 79 switch len(args) {
58 fallthrough 80 case 2: // filename
59 case 2: // only name 81 scriptFile, err := os.Open(args[1])
60 host, port, err = parseHostPort(args[0]) 82 if err != nil {
61 if err != nil { 83 return fmt.Errorf("failed to open script file: %s\n",
62 return err 84 err)
85 }
86 defer scriptFile.Close()
87 r = scriptFile
88 fallthrough
89 case 1:
90 host, port, err = parseHostPort(args[0])
91 if err != nil {
92 return err
93 }
94 default:
95 return usageError("invalid number of arguments")
63 } 96 }
64 97 } else if cmd.Name() == "check" {
65 scriptName = args[1] 98 switch len(args) {
66 default: 99 case 1: // filename
67 return usageError("invalid number of arguments") 100 scriptFile, err := os.Open(args[0])
101 if err != nil {
102 return fmt.Errorf("failed to open script file: %s\n",
103 err)
104 }
105 defer scriptFile.Close()
106 r = scriptFile
107 case 0:
108 default:
109 return usageError("invalid number of arguments")
110 }
68 } 111 }
69 112
70 script, err := readLimitedString(r, managesieve.ReadLimit) 113 script, err := readLimitedString(r, managesieve.ReadLimit)
71 if err != nil { 114 if err != nil {
72 return fmt.Errorf("failed to read script: %s\n", err) 115 return fmt.Errorf("failed to read script: %s\n", err)
83 if err != nil { 126 if err != nil {
84 return err 127 return err
85 } 128 }
86 defer c.Logout() 129 defer c.Logout()
87 130
88 if warnings, err := c.PutScript(scriptName, script); err != nil { 131 var warnings string
132 if cmd.Name() == "put" {
133 warnings, err = c.PutScript(scriptName, script)
134 } else if cmd.Name() == "check" {
135 warnings, err = c.CheckScript(script)
136 }
137 if err != nil {
89 return err 138 return err
90 } else if warnings != "" { 139 } else if warnings != "" {
91 fmt.Fprintln(os.Stderr, warnings) 140 fmt.Fprintln(os.Stderr, warnings)
92 } 141 }
93 142