use app identifier instead of ctx

changing this because ctx is commonly used with the context package.
This commit is contained in:
Harvey Tindall
2020-08-16 13:36:54 +01:00
parent fffb3471d6
commit fd766e7b1a
9 changed files with 452 additions and 445 deletions
+24 -23
View File
@@ -2,33 +2,34 @@ package main
import (
"encoding/json"
"github.com/fsnotify/fsnotify"
"io/ioutil"
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
func (ctx *appContext) StartPWR() {
ctx.info.Println("Starting password reset daemon")
path := ctx.config.Section("password_resets").Key("watch_directory").String()
func (app *appContext) StartPWR() {
app.info.Println("Starting password reset daemon")
path := app.config.Section("password_resets").Key("watch_directory").String()
if _, err := os.Stat(path); os.IsNotExist(err) {
ctx.err.Printf("Failed to start password reset daemon: Directory \"%s\" doesn't exist", path)
app.err.Printf("Failed to start password reset daemon: Directory \"%s\" doesn't exist", path)
return
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
ctx.err.Printf("Couldn't initialise password reset daemon")
app.err.Printf("Couldn't initialise password reset daemon")
return
}
defer watcher.Close()
done := make(chan bool)
go pwrMonitor(ctx, watcher)
go pwrMonitor(app, watcher)
err = watcher.Add(path)
if err != nil {
ctx.err.Printf("Failed to start password reset daemon: %s", err)
app.err.Printf("Failed to start password reset daemon: %s", err)
}
<-done
}
@@ -39,7 +40,7 @@ type Pwr struct {
Expiry time.Time `json:"ExpirationDate"`
}
func pwrMonitor(ctx *appContext, watcher *fsnotify.Watcher) {
func pwrMonitor(app *appContext, watcher *fsnotify.Watcher) {
for {
select {
case event, ok := <-watcher.Events:
@@ -56,29 +57,29 @@ func pwrMonitor(ctx *appContext, watcher *fsnotify.Watcher) {
if len(pwr.Pin) == 0 || err != nil {
return
}
ctx.info.Printf("New password reset for user \"%s\"", pwr.Username)
app.info.Printf("New password reset for user \"%s\"", pwr.Username)
if ct := time.Now(); pwr.Expiry.After(ct) {
user, status, err := ctx.jf.userByName(pwr.Username, false)
user, status, err := app.jf.userByName(pwr.Username, false)
if !(status == 200 || status == 204) || err != nil {
ctx.err.Printf("Failed to get users from Jellyfin: Code %d", status)
ctx.debug.Printf("Error: %s", err)
app.err.Printf("Failed to get users from Jellyfin: Code %d", status)
app.debug.Printf("Error: %s", err)
return
}
ctx.storage.loadEmails()
address, ok := ctx.storage.emails[user["Id"].(string)].(string)
app.storage.loadEmails()
address, ok := app.storage.emails[user["Id"].(string)].(string)
if !ok {
ctx.err.Printf("Couldn't find email for user \"%s\". Make sure it's set", pwr.Username)
app.err.Printf("Couldn't find email for user \"%s\". Make sure it's set", pwr.Username)
return
}
if ctx.email.constructReset(pwr, ctx) != nil {
ctx.err.Printf("Failed to construct password reset email for %s", pwr.Username)
} else if ctx.email.send(address, ctx) != nil {
ctx.err.Printf("Failed to send password reset email to \"%s\"", address)
if app.email.constructReset(pwr, app) != nil {
app.err.Printf("Failed to construct password reset email for %s", pwr.Username)
} else if app.email.send(address, app) != nil {
app.err.Printf("Failed to send password reset email to \"%s\"", address)
} else {
ctx.info.Printf("Sent password reset email to \"%s\"", address)
app.info.Printf("Sent password reset email to \"%s\"", address)
}
} else {
ctx.err.Printf("Password reset for user \"%s\" has already expired (%s). Check your time settings.", pwr.Username, pwr.Expiry)
app.err.Printf("Password reset for user \"%s\" has already expired (%s). Check your time settings.", pwr.Username, pwr.Expiry)
}
}
@@ -86,7 +87,7 @@ func pwrMonitor(ctx *appContext, watcher *fsnotify.Watcher) {
if !ok {
return
}
ctx.err.Printf("Password reset daemon: %s", err)
app.err.Printf("Password reset daemon: %s", err)
}
}
}