From 138121330e97cf6a31a4abd91b8bec206d7d7f80 Mon Sep 17 00:00:00 2001 From: vin Date: Mon, 20 Nov 2023 10:06:35 -0500 Subject: [PATCH] use golang flag instead of external getopt library --- aplus.go | 2 +- go.mod | 4 +--- go.sum | 2 -- main.go | 67 +++++++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/aplus.go b/aplus.go index 51a5a10..6d308c4 100644 --- a/aplus.go +++ b/aplus.go @@ -63,7 +63,7 @@ func submit_code(course_code int, attendance_code string) { func launch_aplus(course_code int) string { err := init_aplus_toolid(course_code) if err != nil { - fmt.Println("Failed to get A+ base URL and tool ID") + fmt.Println("Failed to get A+ base URL and tool ID. Try again with a different course code.") os.Exit(1) } diff --git a/go.mod b/go.mod index faf8f50..6659170 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,5 @@ module codeberg.org/flpolyaplus/aplus -go 1.21.3 +go 1.21.1 require golang.org/x/net v0.18.0 - -require github.com/pborman/getopt v1.1.0 // indirect diff --git a/go.sum b/go.sum index 7143679..ee9cb73 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ -github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0= -github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk= golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= diff --git a/main.go b/main.go index 9386b46..93defd3 100644 --- a/main.go +++ b/main.go @@ -1,43 +1,74 @@ package main import ( + "flag" "fmt" "net/http" "os" - - "github.com/pborman/getopt" ) var base_link string +var base_aplus_link string +var external_tools_code int var token string var client http.Client +func usage() { + fmt.Println("Usage: aplus [--code attendance_code --course course_code] [--list] [--listfav] [--help]") +} + func main() { initialize() - code := getopt.StringLong("code", 'c', "", "Code to enter") - course := getopt.IntLong("course", 'C', -1, "Canvas course number") - list_all := getopt.BoolLong("list-all", 'L', "List all courses") - list_favorites := getopt.BoolLong("list-favorites", 'F', "List favorite courses") - help := getopt.BoolLong("help", 'h', "Help") + var code string + var course int + var listall bool + var listfav bool + var help bool - getopt.Parse() + flag.StringVar(&code, "code", "", "5 character attendance code") + flag.StringVar(&code, "c", "", "5 character attendance code") + flag.IntVar(&course, "course", -1, "canvas course code") + flag.IntVar(&course, "C", -1, "canvas course code") + flag.BoolVar(&listall, "list", false, "list all canvas courses") + flag.BoolVar(&listall, "L", false, "list all canvas courses") + flag.BoolVar(&listfav, "listfav", false, "list favorited canvas courses") + flag.BoolVar(&listfav, "F", false, "list favorited canvas courses") + flag.BoolVar(&help, "help", false, "view usage message") + flag.BoolVar(&help, "h", false, "view usage message") - if *help || (*list_all == false && *list_favorites == false && *code == "" && *course == -1) { - getopt.Usage() + flag.Parse() + + if (code == "" && course == -1 && !listall && !listfav) { + usage() + os.Exit(1) + } + + if (listall) { + list_all_courses() os.Exit(0) } - if *list_all { - list_all_courses() - } - - if *list_favorites { + if (listfav) { list_favorite_courses() + os.Exit(0) } - if *code != "" && *course != -1 { - fmt.Printf("%d %s", *course, *code) - submit_code(*course, *code) + if (help) { + usage() + os.Exit(0) + } + + if (len(code) != 5) { + fmt.Println("Attendance code must be 5 characters long") + os.Exit(1) + } + + if code != "" && course != -1 { + fmt.Printf("%d %s\n", course, code) + submit_code(course, code) + } else { + usage() + os.Exit(1) } }