main.go (3272B)
1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "net/http" 7 "os" 8 "runtime/pprof" 9 ) 10 11 var base_link string 12 var base_aplus_link string 13 var external_tools_code int 14 var token string 15 var client http.Client 16 var verbose bool 17 18 func usage() { 19 fmt.Println("Usage: aplus --code attendance_code --course course_code [--list] [--listfav] [--timetable] [--help]") 20 fmt.Println(" -c, --code\tfive character attendance code") 21 fmt.Println(" -C, --course\tcanvas course ID") 22 fmt.Println(" -L, --list\tlists canvas courses with name and ID") 23 fmt.Println(" -F, --listfav\tlists favourited canvas courses with name and ID") 24 fmt.Println(" -t, --timetable\tlists the class sessions in a course") 25 fmt.Println(" -v, --verbose\tprints with verbose output") 26 fmt.Println(" -h, --help\tdisplays this help message") 27 fmt.Println(" -p, --profile\twrites cpu profile to a file (for debugging/optimizing)") 28 } 29 30 func main() { 31 initialize() 32 33 var code string 34 var course int 35 var listall bool 36 var listfav bool 37 var timetable_ bool 38 var help bool 39 var cpuprofile string 40 41 flag.StringVar(&code, "code", "", "5 character attendance code") 42 flag.StringVar(&code, "c", "", "5 character attendance code") 43 flag.IntVar(&course, "course", -1, "canvas course code") 44 flag.IntVar(&course, "C", -1, "canvas course code") 45 flag.BoolVar(&listall, "list", false, "list all canvas courses") 46 flag.BoolVar(&listall, "L", false, "list all canvas courses") 47 flag.BoolVar(&listfav, "listfav", false, "list favorited canvas courses") 48 flag.BoolVar(&listfav, "F", false, "list favorited canvas courses") 49 flag.BoolVar(&timetable_, "t", false, "lists the class sessions in a course") 50 flag.BoolVar(&timetable_, "timetable", false, "lists the class sessions in a course") 51 flag.BoolVar(&verbose, "verbose", false, "verbose output") 52 flag.BoolVar(&verbose, "v", false, "verbose output") 53 flag.BoolVar(&help, "help", false, "view usage message") 54 flag.BoolVar(&help, "h", false, "view usage message") 55 flag.StringVar(&cpuprofile, "p", "", "CPU profiling report location") 56 flag.StringVar(&cpuprofile, "profile", "", "CPU profiling report location") 57 58 flag.Parse() 59 60 if cpuprofile != "" { 61 cpuf, err := os.Create(cpuprofile) 62 if err != nil { 63 fmt.Println(err) 64 os.Exit(1) 65 } 66 defer cpuf.Close() 67 68 err = pprof.StartCPUProfile(cpuf) 69 if err != nil { 70 fmt.Println(err) 71 os.Exit(1) 72 } 73 defer pprof.StopCPUProfile() 74 } 75 76 if code == "" && course == -1 && !listall && !listfav && !help { 77 usage() 78 os.Exit(1) 79 } 80 81 if listall { 82 _ = list_all_courses(false) 83 os.Exit(0) 84 } 85 86 if listfav { 87 list_favorite_courses() 88 os.Exit(0) 89 } 90 91 if course != -1 && timetable_ { 92 timetable(course) 93 os.Exit(0) 94 } else if course == -1 && timetable_ { 95 fmt.Println("Course code required for timetable") 96 usage() 97 os.Exit(1) 98 } 99 100 if help { 101 usage() 102 os.Exit(0) 103 } 104 105 if len(code) == 5 && course != -1 { 106 fmt.Printf("%d %s\n", course, code) 107 submit_code(course, code) 108 } else if code == "" && course != -1 { 109 fmt.Println("Attendance code not provided") 110 usage() 111 os.Exit(1) 112 } else if len(code) != 5 { 113 fmt.Println("Attendance code must be 5 characters long") 114 usage() 115 os.Exit(1) 116 } else if len(code) == 5 && course == -1 { 117 fmt.Println("Checking which course to send the code to") 118 submit_code_sans_course(code) 119 } 120 }