Add Noten parsing and rework some existing code
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
||||
var (
|
||||
sessionCache string
|
||||
username string
|
||||
showGrades bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -29,6 +30,7 @@ func init() {
|
||||
defaultCacheDir := path.Join(homeDir, ".cache/go-lsf/sessions")
|
||||
flag.StringVar(&sessionCache, "session-cache", defaultCacheDir, "path where the session tokens are located")
|
||||
flag.StringVar(&username, "username", "", "username to login with in lsf")
|
||||
flag.BoolVar(&showGrades, "noten", false, "list grades")
|
||||
flag.Parse()
|
||||
|
||||
err = os.MkdirAll(sessionCache, os.ModePerm)
|
||||
@@ -51,7 +53,16 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("s: %+v\n", s)
|
||||
if showGrades {
|
||||
noten, err := s.Noten()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Noten:")
|
||||
for _, note := range noten {
|
||||
fmt.Println(note)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readUsername() (string, error) {
|
||||
@@ -69,7 +80,7 @@ func readPassword() (string, error) {
|
||||
fmt.Print("Password: ")
|
||||
b, err := terminal.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "could not read password")
|
||||
return "", err
|
||||
}
|
||||
fmt.Print("\n")
|
||||
password = string(b)
|
||||
@@ -101,20 +112,16 @@ func session(username string) (*lsf.Session, error) {
|
||||
return nil, errors.Wrapf(err, "could not read session file %s", sessionPath)
|
||||
}
|
||||
sid := strings.TrimSuffix(string(b), "\n")
|
||||
s := &lsf.Session{
|
||||
SID: sid,
|
||||
}
|
||||
valid, err := s.Valid()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not check session")
|
||||
}
|
||||
if !valid {
|
||||
fmt.Println("session is not valid")
|
||||
s, err := lsf.NewSessionBySID(sid)
|
||||
if err == lsf.ErrInvalSID {
|
||||
log.Println("session is not valid")
|
||||
err := os.Remove(sessionPath)
|
||||
if err != nil {
|
||||
log.Println(errors.Wrap(err, "could not remove invalid session file"))
|
||||
}
|
||||
return login(username)
|
||||
} else if err != nil {
|
||||
return nil, errors.Wrap(err, "could not get session by existing sid")
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
@@ -122,7 +129,7 @@ func session(username string) (*lsf.Session, error) {
|
||||
func login(username string) (*lsf.Session, error) {
|
||||
password, err := readPassword()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not login")
|
||||
return nil, errors.Wrap(err, "could not read password")
|
||||
}
|
||||
s, err := lsf.Login(username, password)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user