Add logging and new flags for attendance program.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os" // added for logging to file
|
||||
"time"
|
||||
|
||||
"github.com/chromedp/chromedp"
|
||||
@@ -14,21 +15,68 @@ const (
|
||||
)
|
||||
|
||||
var waitTime int
|
||||
var isVerbose bool
|
||||
var maxTime int
|
||||
|
||||
func main() {
|
||||
//* Parse the flags
|
||||
wait, headless, err := parseFlags()
|
||||
wait, verbose, headless, logFile, timeout, replacePass, err := parseFlags()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse flags: %v", err)
|
||||
}
|
||||
waitTime = wait
|
||||
isVerbose = verbose
|
||||
maxTime = timeout
|
||||
if logFile != "" {
|
||||
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open log file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
log.SetOutput(file)
|
||||
}
|
||||
|
||||
fmt.Println("Waiting time:", waitTime)
|
||||
if isVerbose {
|
||||
fmt.Println("Waiting time:", waitTime)
|
||||
fmt.Println("Verbose mode:", isVerbose)
|
||||
fmt.Println("Headless mode:", headless)
|
||||
if logFile == "" {
|
||||
fmt.Println("Logging to file: disabled")
|
||||
|
||||
} else {
|
||||
fmt.Println("Logging to file:", logFile)
|
||||
}
|
||||
|
||||
if maxTime == -1 {
|
||||
fmt.Println("Max time: disabled")
|
||||
} else {
|
||||
fmt.Println("Max time:", maxTime)
|
||||
}
|
||||
}
|
||||
|
||||
//* Welcome message
|
||||
welcomeMessage()
|
||||
|
||||
if logFile != "" {
|
||||
fmt.Println("Logging to file:", logFile)
|
||||
fmt.Println("(No text will be shown because logging is enabled, dont worry, the program is still running)")
|
||||
log.Print("Starting new program... (logging to file)\n\n")
|
||||
isVerbose = true
|
||||
}
|
||||
|
||||
creds, err := getCreds()
|
||||
|
||||
if replacePass {
|
||||
creds, err = promptAndSaveCreds(credFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get credentials: %v", err)
|
||||
}
|
||||
if isVerbose {
|
||||
log.Println("Credentials replaced")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// If the credentials file doesn't exist, prompt for credentials
|
||||
if err.Error() == "credentials file not found: stat "+credFile+": no such file or directory" {
|
||||
@@ -59,6 +107,9 @@ func main() {
|
||||
log.Fatalf("Failed to navigate: %v", err)
|
||||
}
|
||||
|
||||
if isVerbose {
|
||||
log.Println("Navigated to the website")
|
||||
}
|
||||
//* Check if there's a login button
|
||||
var isVisible bool
|
||||
if err := chromedp.Run(ctx, chromedp.Tasks{
|
||||
@@ -68,15 +119,25 @@ func main() {
|
||||
log.Fatalf("Failed to evaluate: %v", err)
|
||||
}
|
||||
|
||||
if isVerbose {
|
||||
log.Println("Login button visible:", isVisible)
|
||||
}
|
||||
|
||||
if isVisible {
|
||||
log.Println("Logging in...")
|
||||
if err := login(ctx, creds); err != nil {
|
||||
log.Fatalf("Failed to login: %v", err)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
log.Println("Waiting for activation...")
|
||||
log.Println("Logged in!")
|
||||
if isVerbose {
|
||||
log.Println("Waiting for activation...")
|
||||
}
|
||||
if err := marcarPresenca(ctx); err != nil {
|
||||
log.Fatalf("Failed to mark presence: %v", err)
|
||||
log.Println("Failed to mark presence: %v", err)
|
||||
} else {
|
||||
log.Println("Presence marked!")
|
||||
}
|
||||
}
|
||||
log.Print("Program finished successfully! (logging to file) \n\n")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user