improve help flag, configuration only via env variables, allow duration to be changed at runtime

master
Carlo Strub 7 years ago
parent 755f30b1d6
commit 77fa162ee0

@ -24,15 +24,11 @@ func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "Sisyphus" app.Name = "Sisyphus"
app.Usage = "Intelligent Junk Mail Handler" app.Usage = "Intelligent Junk Mail Handler"
app.UsageText = `sisyphus [GLOBAL OPTIONS] command app.UsageText = `
Sisyphus applies artificial intelligence to filter Junk mail in an
Sisyphus applies artificial intelligence to filter unobtrusive way. Both, classification and learning operate directly on
Junk mail in an unobtrusive way. Both, classification and learning the Maildir of a user in a fully transparent mode, without any need for
operate directly on the Maildir of a user in a fully transparent mode, configuration or active operation.`
without any need for configuration or active operation.
It is highly recommended to operate Sisyphus by setting environment
variables for the global options instead of using flags.`
app.HelpName = "Intelligent Junk Mail Handler" app.HelpName = "Intelligent Junk Mail Handler"
app.Version = version app.Version = version
app.Copyright = "(c) 2017, Carlo Strub. All rights reserved. This binary is licensed under a BSD 3-Clause License." app.Copyright = "(c) 2017, Carlo Strub. All rights reserved. This binary is licensed under a BSD 3-Clause License."
@ -42,29 +38,77 @@ func main() {
Email: "cs@carlostrub.ch", Email: "cs@carlostrub.ch",
}, },
} }
app.ExtraInfo = func() map[string]string {
return map[string]string{
"ENVIRONMENT VARIABLES": `For configuration, set the following environment
variables:
SISYPHUS_DIRS: Comma-separated list of maildirs,
e.g. ./Maildir,/home/JohnDoe/Maildir
SISYPHUS_DURATION: Interval between learning periods, e.g. 12h
`,
}
}
app.CustomAppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
sisyphus {{if .VisibleFlags}}[FLAGS] {{end}}COMMAND{{if .VisibleFlags}}{{end}}
{{.UsageText}}
COMMANDS:
{{range .VisibleCommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{if .VisibleFlags}}
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}
{{range $key, $value := ExtraInfo}}
{{$key}}:
{{$value}}
{{end}}VERSION:
{{.Version}}
AUTHOR:{{range .Authors}}
{{.}}{{end}}
COPYRIGHT:
{{.Copyright}}
`
dirsRaw, ok := os.LookupEnv("SISYPHUS_DIRS")
if !ok {
log.Fatal("Environment variable SISYPHUS_DIRS not set.")
}
dirsSplit := strings.Split(dirsRaw, ",")
maildirPaths := cli.StringSlice([]string{}) var maildirs []sisyphus.Maildir
for i := 0; i < len(dirsSplit); i++ {
var learnafter *string maildirs = append(maildirs, sisyphus.Maildir(dirsSplit[i]))
learnafter = new(string) }
app.Flags = []cli.Flag{
cli.StringSliceFlag{ _, ok = os.LookupEnv("SISYPHUS_DURATION")
Name: "maildir, d", if !ok {
Value: &maildirPaths, log.Fatal("Environment variable SISYPHUS_DURATION not set.")
EnvVar: "SISYPHUS_DIRS",
Usage: "Call multiple Maildirs by repeating this flag, i.e. --maildir \"./Maildir\" --maildir \"./Maildir2\"",
},
cli.StringFlag{
Name: "learn",
Value: "12h",
EnvVar: "SISYPHUS_DURATION",
Usage: "Time interval between to learn cycles",
Destination: learnafter,
},
} }
// app.Flags = []cli.Flag{
//
// &cli.StringSliceFlag{
// Name: "maildir, d",
// Value: &maildirPaths,
// EnvVars: []string{"SISYPHUS_DIRS"},
// Usage: "Call multiple Maildirs by repeating this flag, i.e. --maildir \"./Maildir\" --maildir \"./Maildir2\"",
// },
// &cli.StringFlag{
// Name: "learn",
// Value: "12h",
// EnvVars: []string{"SISYPHUS_DURATION"},
// Usage: "Time interval between to learn cycles",
// Destination: learnafter,
// },
// }
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "run", Name: "run",
@ -89,16 +133,6 @@ func main() {
`) `)
if len(maildirPaths) < 1 {
log.Fatal("No Maildir set. Please check the manual.")
}
// Populate maildir with the maildirs given by setting the flag.
var maildirs []sisyphus.Maildir
for _, val := range maildirPaths {
maildirs = append(maildirs, sisyphus.Maildir(val))
}
// Create missing Maildirs // Create missing Maildirs
err := sisyphus.LoadMaildirs(maildirs) err := sisyphus.LoadMaildirs(maildirs)
if err != nil { if err != nil {
@ -119,7 +153,7 @@ func main() {
// Learn at startup and regular intervals // Learn at startup and regular intervals
go func() { go func() {
for { for {
duration, err := time.ParseDuration(*learnafter) duration, err := time.ParseDuration(os.Getenv("SISYPHUS_DURATION"))
if err != nil { if err != nil {
log.Fatal("Cannot parse duration for learning intervals.") log.Fatal("Cannot parse duration for learning intervals.")
} }
@ -165,8 +199,8 @@ func main() {
} }
}() }()
for _, val := range maildirPaths { for _, val := range maildirs {
err = watcher.Add(val + "/new") err = watcher.Add(string(val) + "/new")
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"err": err, "err": err,

Loading…
Cancel
Save