You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.0 KiB
92 lines
2.0 KiB
package main |
|
|
|
import ( |
|
"log" |
|
"os" |
|
"ssender/internal/config" |
|
"ssender/internal/db" |
|
"ssender/internal/rss" |
|
"ssender/internal/send" |
|
|
|
"github.com/jessevdk/go-flags" |
|
) |
|
|
|
type Options struct { |
|
FileParse string `short:"f" long:"fileparse" description:"File for parse (rss xml)" required:"true"` |
|
ConfigPath string `short:"c" long:"configpath" description:"Config file path"` |
|
InitDB bool `short:"i" long:"initdb" description:"Run initialize from current file"` |
|
} |
|
|
|
var ( |
|
ConfigPath = "/etc/ssender/config.yml" |
|
) |
|
|
|
func main() { |
|
log.Println("Run processing") |
|
// Parse flags |
|
var options Options |
|
var parser = flags.NewParser(&options, flags.Default) |
|
if _, err := parser.Parse(); err != nil { |
|
switch flagsErr := err.(type) { |
|
case flags.ErrorType: |
|
if flagsErr == flags.ErrHelp { |
|
os.Exit(0) |
|
} |
|
os.Exit(1) |
|
default: |
|
os.Exit(1) |
|
} |
|
} |
|
log.Println("Flags processed") |
|
|
|
if options.ConfigPath == "" { |
|
log.Println("Config path not found in options! Use default from /etc/ssender/config.yml") |
|
} else { |
|
log.Printf("Config from: %s\n", options.ConfigPath) |
|
ConfigPath = options.ConfigPath |
|
} |
|
|
|
// Get config |
|
cfg, err := config.NewConfig(ConfigPath) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
log.Println("Config processed") |
|
|
|
// Parse rss file |
|
log.Printf("Parse file %s \n", options.FileParse) |
|
rss, err := rss.NewRSS(options.FileParse) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
|
|
if options.InitDB { |
|
err := db.InitDb(*rss, cfg.Dbpath) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
} |
|
|
|
// Find new items |
|
sendItems, err := db.FindItems(*rss, cfg.Dbpath) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
log.Printf("Found %d new items to send\n", len(sendItems.ItemList)) |
|
|
|
if len(sendItems.ItemList) > 0 { |
|
// Run send data depended on configuration options |
|
log.Println("Run send process") |
|
err := send.RunSend(sendItems, *cfg) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
// Run update db |
|
err = db.UpdateDb(cfg.Dbpath, sendItems) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
} |
|
|
|
log.Println("End processing") |
|
}
|
|
|