summaryrefslogtreecommitdiff
path: root/main.go
blob: c3fe1a5975a01f574cf389000440769f2844661f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"runtime/pprof"
)

var base_link string
var base_aplus_link string
var external_tools_code int
var token string
var client http.Client
var verbose bool

func usage() {
	fmt.Println("Usage: aplus --code attendance_code --course course_code [--list] [--listfav] [--timetable] [--help]")
	fmt.Println("  -c, --code\tfive character attendance code")
	fmt.Println("  -C, --course\tcanvas course ID")
	fmt.Println("  -L, --list\tlists canvas courses with name and ID")
	fmt.Println("  -F, --listfav\tlists favourited canvas courses with name and ID")
	fmt.Println("  -t, --timetable\tlists the class sessions in a course")
	fmt.Println("  -v, --verbose\tprints with verbose output")
	fmt.Println("  -h, --help\tdisplays this help message")
	fmt.Println("  -p, --profile\twrites cpu profile to a file (for debugging/optimizing)")
}

func main() {
	initialize()

	var code string
	var course int
	var listall bool
	var listfav bool
	var timetable_ bool
	var help bool
	var cpuprofile string

	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(&timetable_, "t", false, "lists the class sessions in a course")
	flag.BoolVar(&timetable_, "timetable", false, "lists the class sessions in a course")
	flag.BoolVar(&verbose, "verbose", false, "verbose output")
	flag.BoolVar(&verbose, "v", false, "verbose output")
	flag.BoolVar(&help, "help", false, "view usage message")
	flag.BoolVar(&help, "h", false, "view usage message")
	flag.StringVar(&cpuprofile, "p", "", "CPU profiling report location")
	flag.StringVar(&cpuprofile, "profile", "", "CPU profiling report location")

	flag.Parse()

	if cpuprofile != "" {
		cpuf, err := os.Create(cpuprofile)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		defer cpuf.Close()

		err = pprof.StartCPUProfile(cpuf)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		defer pprof.StopCPUProfile()
	}

	if code == "" && course == -1 && !listall && !listfav && !help {
		usage()
		os.Exit(1)
	}

	if listall {
		_ = list_all_courses(false)
		os.Exit(0)
	}

	if listfav {
		list_favorite_courses()
		os.Exit(0)
	}

	if course != -1 && timetable_ {
		timetable(course)
		os.Exit(0)
	} else if course == -1 && timetable_ {
		fmt.Println("Course code required for timetable")
		usage()
		os.Exit(1)
	}

	if help {
		usage()
		os.Exit(0)
	}

	if len(code) == 5 && course != -1 {
		fmt.Printf("%d %s\n", course, code)
		submit_code(course, code)
	} else if code == "" && course != -1 {
		fmt.Println("Attendance code not provided")
		usage()
		os.Exit(1)
	} else if len(code) != 5 {
		fmt.Println("Attendance code must be 5 characters long")
		usage()
		os.Exit(1)
	} else if len(code) == 5 && course == -1 {
		fmt.Println("Checking which course to send the code to")
		submit_code_sans_course(code)
	}
}