Add username support, clean up command line parsing, help and output

This commit is contained in:
Justin Hawkins 2017-02-21 12:24:14 +10:30
parent 4e925136ba
commit 699ca9fcfc

47
dau.go
View File

@ -16,10 +16,13 @@ import (
"io/ioutil" "io/ioutil"
) )
var current_version = "0.2" var current_version = "0.3"
var last_check = time.Now() var last_check = time.Now()
var new_last_check = time.Now() var new_last_check = time.Now()
var webhook_url string var webhook_url string
var username string
type webhook_response struct { type webhook_response struct {
Test string Test string
@ -31,8 +34,9 @@ func keepLines(s string, n int) string {
} }
func main() { func main() {
webhook, path, watch := parse_options() webhook_opt, path, watch, username_opt := parse_options()
webhook_url = webhook webhook_url = webhook_opt
username = username_opt
check_updates() check_updates()
@ -69,10 +73,10 @@ func check_updates() {
err = json.Unmarshal(body, &latest) err = json.Unmarshal(body, &latest)
if (err != nil) { if (err != nil) {
log.Fatal("could not parse JSON", err) log.Fatal("could not parse JSON: ", err)
} }
if (current_version != latest.Tag_name) { if (current_version < latest.Tag_name) {
fmt.Println("A new version is available:", latest.Tag_name) fmt.Println("A new version is available:", latest.Tag_name)
fmt.Println("----------- Release Info -----------") fmt.Println("----------- Release Info -----------")
fmt.Println(latest.Body) fmt.Println(latest.Body)
@ -83,17 +87,34 @@ func check_updates() {
} }
func parse_options() (webhook_url string, path string, watch int) { func parse_options() (webhook_url string, path string, watch int, username string) {
// Declare the flags to be used // Declare the flags to be used
// helpFlag := getopt.Bool('h', "display help") // helpFlag := getopt.Bool('h', "display help")
webhookFlag := getopt.StringLong("webhook", 'w', "", "webhook URL") webhookFlag := getopt.StringLong("webhook", 'w', "", "discord webhook URL")
pathFlag := getopt.StringLong("directory", 'd', "", "directory") pathFlag := getopt.StringLong("directory", 'd', "", "directory to scan, optional, defaults to current directory")
watchFlag := getopt.Int16Long("watch", 's', 10, "time between scans") watchFlag := getopt.Int16Long ("watch", 's', 10, "time between scans")
usernameFlag := getopt.StringLong("username", 'u', "", "username for the bot upload")
helpFlag := getopt.BoolLong ("help", 'h', "help")
getopt.SetParameters("")
getopt.Parse() getopt.Parse()
return *webhookFlag, *pathFlag, int(*watchFlag) if (*helpFlag) {
getopt.PrintUsage(os.Stderr)
os.Exit(1)
}
if ! getopt.IsSet("directory") {
*pathFlag = "./"
log.Println("Defaulting to current directory")
}
if ! getopt.IsSet("webhook") {
log.Fatal("ERROR: You must specify a --webhook URL")
}
return *webhookFlag, *pathFlag, int(*watchFlag), *usernameFlag
} }
func check_file(path string, f os.FileInfo, err error) error { func check_file(path string, f os.FileInfo, err error) error {
@ -129,6 +150,10 @@ func process_file(file string) {
// "username": "Some username", // "username": "Some username",
} }
if (username != "") {
extraParams["username"] = username
}
type DiscordAPIResponseAttachment struct { type DiscordAPIResponseAttachment struct {
Url string Url string
Proxy_url string Proxy_url string
@ -170,7 +195,7 @@ func process_file(file string) {
err = json.Unmarshal(res_body, &res) err = json.Unmarshal(res_body, &res)
if (err != nil) { if (err != nil) {
log.Fatal("could not parse JSON", err) log.Print("could not parse JSON: ", err)
fmt.Println("Response was:", res_body) fmt.Println("Response was:", res_body)
return return
} }