Rewrote the code to handle the app directory and config file

master
Ronald1985 2 years ago
parent ac9d6095cb
commit b9bafff720

@ -23,6 +23,7 @@ import (
const ( const (
DEFAULT_TIME_INBETWEEN_NOTIFICATIONS = 20 * time.Minute DEFAULT_TIME_INBETWEEN_NOTIFICATIONS = 20 * time.Minute
EYE_REMINDER_ICON_URL = "https://gitea.ronald1985.uk/ronald1985/Eye-Reminder/raw/branch/master/assets/Eye-Reminder-Icon.png" EYE_REMINDER_ICON_URL = "https://gitea.ronald1985.uk/ronald1985/Eye-Reminder/raw/branch/master/assets/Eye-Reminder-Icon.png"
DEFAULT_CONFIG_FILE = "Interval: 20"
) )
type Config struct { type Config struct {
@ -128,7 +129,7 @@ func systrayOnReady() {
lastTimeNotificationWasSent := time.Now().Add(time.Duration(-ProgramConfig.Interval) * time.Minute) lastTimeNotificationWasSent := time.Now().Add(time.Duration(-ProgramConfig.Interval) * time.Minute)
for { for {
if enabled && time.Since(lastTimeNotificationWasSent) > time.Duration(ProgramConfig.Interval) { if enabled && time.Since(lastTimeNotificationWasSent) > time.Duration(ProgramConfig.Interval)*time.Minute {
fmt.Println("Sending notification") fmt.Println("Sending notification")
SendNotification("Eye Reminder", "Look away for the screen for atleast 20 seconds") SendNotification("Eye Reminder", "Look away for the screen for atleast 20 seconds")
@ -166,66 +167,77 @@ func systrayOnReady() {
func main() { func main() {
ge.HandleSignals(false) ge.HandleSignals(false)
var possibleConfigFileLocations []string ProgramConfig.Interval = int(DEFAULT_TIME_INBETWEEN_NOTIFICATIONS)
possibleConfigFileLocations = append(possibleConfigFileLocations, os.Getenv("HOME")+"/.config/Eye-Reminder/config.yaml")
possibleConfigFileLocations = append(possibleConfigFileLocations, os.Getenv("HOME")+"/.config/Eye-Reminder/config.yml")
possibleConfigFileLocations = append(possibleConfigFileLocations, os.Getenv("XDG_CONFIG_HOME")+"/Eye-Reminder/config.yaml")
possibleConfigFileLocations = append(possibleConfigFileLocations, os.Getenv("XDG_CONFIG_HOME")+"/Eye-Reminder/config.yml")
possibleConfigFileLocations = append(possibleConfigFileLocations, os.Getenv("LOCALAPPDATA")+"/Eye-Reminder/config.yml")
possibleConfigFileLocations = append(possibleConfigFileLocations, os.Getenv("LOCALAPPDATA")+"/Eye-Reminder/config.yml")
configFileLocation := "not found" var possibleAppDirectoryLocations []string
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("HOME")+"/.config/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("HOME")+"/.config/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("XDG_CONFIG_HOME")+"/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("XDG_CONFIG_HOME")+"/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("LOCALAPPDATA")+"/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("LOCALAPPDATA")+"/Eye-Reminder/")
for _, file := range possibleConfigFileLocations { appDirectory := "not found"
if fileExists, err := CheckIfFileExists(file); fileExists { for _, folder := range possibleAppDirectoryLocations {
configFileLocation = file if _, err := os.Stat(folder); !os.IsNotExist(err) {
appDirectory = folder
break break
} else if err != nil {
fmt.Fprintln(os.Stderr, "Error checking for config file:", err)
} }
} }
var err error if appDirectory == "not found" {
appDirectory := "not found" if runtime.GOOS == "windows" {
if configFileLocation == "not found" { appDirectory = DEFAULT_WINDOWS_DIRECTORY
ProgramConfig.Interval = int(DEFAULT_TIME_INBETWEEN_NOTIFICATIONS) } else {
appDirectory = DEFAULT_UNIX_DIRECTORY
var possibleAppDirectoryLocations []string
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("HOME")+"/.config/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("HOME")+"/.config/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("XDG_CONFIG_HOME")+"/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("XDG_CONFIG_HOME")+"/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("LOCALAPPDATA")+"/Eye-Reminder/")
possibleAppDirectoryLocations = append(possibleAppDirectoryLocations, os.Getenv("LOCALAPPDATA")+"/Eye-Reminder/")
for _, folder := range possibleAppDirectoryLocations {
if _, err := os.Stat(folder); !os.IsNotExist(err) {
appDirectory = folder
break
}
} }
if appDirectory == "not found" { fmt.Println("Creating config directory:", appDirectory)
if runtime.GOOS == "windows" { err := os.Mkdir(appDirectory, 0755)
appDirectory = DEFAULT_WINDOWS_DIRECTORY fmt.Println("Created folder:", appDirectory)
} else { if err != nil {
appDirectory = DEFAULT_UNIX_DIRECTORY panic(err)
} }
}
fmt.Println("Creating config directory:", appDirectory) configFile := ""
err := os.Mkdir(appDirectory, 0755) if fileExists, err := CheckIfFileExists(appDirectory + "config.yaml"); fileExists {
fmt.Println("Created folder:", appDirectory) configFile = appDirectory + "config.yaml"
if err != nil { } else if !fileExists && err == nil {
panic(err) if fileExists, _ := CheckIfFileExists(appDirectory + "config.yml"); fileExists {
} configFile = appDirectory + "config.yml"
} }
}
} else { if configFile != "" {
appDirectory = configFileLocation[:strings.IndexByte(configFileLocation, '/')+1] fmt.Println("Found config file at", configFile)
ProgramConfig, err = ReadConfig(configFileLocation) }
if configFile == "" {
configFile = appDirectory + "config.yaml"
fmt.Println("Creating config file:", configFile)
f, err := os.Create(configFile)
if err != nil { if err != nil {
panic(err) print_fatal_error("Failed to create config file")
}
fmt.Println("Created config file:", configFile)
fmt.Println("Writing default config to", configFile)
_, err = f.WriteString(DEFAULT_CONFIG_FILE)
if err != nil {
f.Close()
print_fatal_error("Failed to write to config file: ", configFile)
} }
f.Close()
fmt.Println("Written default config to ", configFile)
}
var err error
ProgramConfig, err = ReadConfig(configFile)
if err != nil {
print_fatal_error("Failed to read config file")
} }
ProgramConfig.imageLocation = appDirectory + "Eye-Reminder-Icon.png" ProgramConfig.imageLocation = appDirectory + "Eye-Reminder-Icon.png"

Loading…
Cancel
Save