Refactor away some globals. This is probably still not very idiomatic.

This commit is contained in:
Justin Hawkins 2017-02-23 12:55:10 +10:30
parent 65b9241492
commit 4825dc56e6

52
dau.go
View File

@ -17,32 +17,36 @@ import (
"github.com/pborman/getopt" "github.com/pborman/getopt"
) )
const currentVersion = "0.4" const currentVersion = "0.5"
var lastCheck = time.Now() var lastCheck = time.Now()
var newLastCheck = time.Now() var newLastCheck = time.Now()
var webhookURL string // Config for the application
var username string type Config struct {
webhookURL string
path string
watch int
username string
}
func main() { func main() {
webhookOpt, path, watch, usernameOpt := parseOptions()
webhookURL = webhookOpt
username = usernameOpt
checkPath(path) config := parseOptions()
checkPath(config.path)
checkUpdates() checkUpdates()
log.Print("Waiting for images to appear in ", path) log.Print("Waiting for images to appear in ", config.path)
// wander the path, forever // wander the path, forever
for { for {
err := filepath.Walk(path, checkFile) err := filepath.Walk(config.path,
func(path string, f os.FileInfo, err error) error { return checkFile(path, f, err, config) })
if err != nil { if err != nil {
log.Fatal("could not watch path", err) log.Fatal("could not watch path", err)
} }
lastCheck = newLastCheck lastCheck = newLastCheck
time.Sleep(time.Duration(watch) * time.Second) time.Sleep(time.Duration(config.watch) * time.Second)
} }
} }
@ -93,8 +97,9 @@ func checkUpdates() {
} }
func parseOptions() (webhookURL string, path string, watch int, username string) { func parseOptions() Config {
var newConfig Config
// Declare the flags to be used // Declare the flags to be used
webhookFlag := getopt.StringLong("webhook", 'w', "", "discord webhook URL") webhookFlag := getopt.StringLong("webhook", 'w', "", "discord webhook URL")
pathFlag := getopt.StringLong("directory", 'd', "", "directory to scan, optional, defaults to current directory") pathFlag := getopt.StringLong("directory", 'd', "", "directory to scan, optional, defaults to current directory")
@ -126,16 +131,21 @@ func parseOptions() (webhookURL string, path string, watch int, username string)
log.Fatal("ERROR: You must specify a --webhook URL") log.Fatal("ERROR: You must specify a --webhook URL")
} }
return *webhookFlag, *pathFlag, int(*watchFlag), *usernameFlag newConfig.path = *pathFlag
newConfig.webhookURL = *webhookFlag
newConfig.watch = int(*watchFlag)
newConfig.username = *usernameFlag
return newConfig
} }
func checkFile(path string, f os.FileInfo, err error) error { func checkFile(path string, f os.FileInfo, err error, config Config) error {
if f.ModTime().After(lastCheck) && f.Mode().IsRegular() { if f.ModTime().After(lastCheck) && f.Mode().IsRegular() {
if fileEligible(path) { if fileEligible(config, path) {
// process file // process file
processFile(path) processFile(config, path)
} }
if newLastCheck.Before(f.ModTime()) { if newLastCheck.Before(f.ModTime()) {
@ -146,7 +156,7 @@ func checkFile(path string, f os.FileInfo, err error) error {
return nil return nil
} }
func fileEligible(file string) bool { func fileEligible(config Config, file string) bool {
extension := strings.ToLower(filepath.Ext(file)) extension := strings.ToLower(filepath.Ext(file))
if extension == ".png" || extension == ".jpg" || extension == ".gif" { if extension == ".png" || extension == ".jpg" || extension == ".gif" {
return true return true
@ -154,13 +164,13 @@ func fileEligible(file string) bool {
return false return false
} }
func processFile(file string) { func processFile(config Config, file string) {
log.Print("Uploading ", file) log.Print("Uploading ", file)
extraParams := map[string]string{} extraParams := map[string]string{}
if username != "" { if config.username != "" {
extraParams["username"] = username extraParams["username"] = config.username
} }
type DiscordAPIResponseAttachment struct { type DiscordAPIResponseAttachment struct {
@ -177,7 +187,7 @@ func processFile(file string) {
ID int64 `json:",string"` ID int64 `json:",string"`
} }
request, err := newfileUploadRequest(webhookURL, extraParams, "file", file) request, err := newfileUploadRequest(config.webhookURL, extraParams, "file", file)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }