projects/sievemgr

view cmd/sievemgr/main.go @ 8:8caacf702c0d

Add edit subcommand for interactive editing of a script
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Fri Nov 13 10:49:08 2020 +0100 (2020-11-13)
parents 3abc8be485c0
children 44c07eb8ef08
line source
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.
22 package main
24 import (
25 "errors"
26 "flag"
27 "fmt"
28 "os"
30 "go.guido-berhoerster.org/sievemgr/cmd/sievemgr/internal/config"
31 )
33 const (
34 exitSuccess = iota
35 exitFailure
36 exitUsage
37 )
39 type usageError string
41 func (e usageError) Error() string {
42 return string(e)
43 }
45 // command-line flags
46 var (
47 confFilename string
48 acctName string
49 )
51 var conf config.Configuration
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 }
62 var cmds = []*command{
63 cmdList,
64 cmdPut,
65 cmdGet,
66 cmdActivate,
67 cmdDeactivate,
68 cmdDelete,
69 cmdInfo,
70 cmdCheck,
71 cmdCheckSpace,
72 cmdRename,
73 cmdEdit,
74 }
76 func usage() {
77 fmt.Fprintf(flag.CommandLine.Output(),
78 "usage:\n %s [options] [subcommand [options] [arguments]]\n",
79 flag.CommandLine.Name())
80 fmt.Fprintln(flag.CommandLine.Output(), "subcommands:")
81 for _, cmd := range cmds {
82 fmt.Fprintf(flag.CommandLine.Output(), " %s\n", cmd.Name())
83 }
84 fmt.Fprintln(flag.CommandLine.Output(), "global options:")
85 flag.PrintDefaults()
86 }
88 func main() {
89 flag.Usage = usage
90 flag.StringVar(&confFilename, "f", confFilename,
91 "Set the name of the configuration file")
92 flag.Parse()
93 if flag.NArg() == 0 {
94 fmt.Fprintln(flag.CommandLine.Output(), "missing subcommand")
95 usage()
96 os.Exit(exitUsage)
97 }
99 if err := config.ParseFile(confFilename, &conf); err != nil {
100 fmt.Fprintln(flag.CommandLine.Output(), err)
101 os.Exit(exitFailure)
102 }
104 name := flag.Arg(0)
105 var cmd *command
106 for _, c := range cmds {
107 if c.Name() == name {
108 cmd = c
109 break
110 }
111 }
112 if cmd == nil {
113 fmt.Fprintf(flag.CommandLine.Output(),
114 "unknown subcommand %q\n", name)
115 usage()
116 os.Exit(exitUsage)
117 }
119 cmd.Flag.Init(cmd.Name(), flag.ExitOnError)
120 cmd.Flag.Usage = cmd.Usage
121 args := flag.Args()
122 if err := cmd.Flag.Parse(args[1:]); err != nil {
123 fmt.Fprintln(flag.CommandLine.Output(), err)
124 os.Exit(exitFailure)
125 }
127 if err := cmd.Run(cmd, cmd.Flag.Args()); err != nil {
128 fmt.Fprintln(flag.CommandLine.Output(), err)
130 var uerr usageError
131 if errors.As(err, &uerr) {
132 cmd.Usage()
133 os.Exit(exitUsage)
134 }
136 os.Exit(exitFailure)
137 }
138 }