2024-10-27 16:21:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-10-31 11:54:39 +00:00
|
|
|
"fmt"
|
2024-10-27 16:21:28 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2024-10-31 11:54:39 +00:00
|
|
|
|
|
|
|
"github.com/adhocore/jsonc"
|
2024-10-27 16:21:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2024-10-31 11:54:39 +00:00
|
|
|
ImageFilters ImageFilters
|
|
|
|
ImagesDir string
|
|
|
|
Duration int
|
|
|
|
Cache string
|
|
|
|
Default string
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeDefaultConfig() Config {
|
|
|
|
ImageFilters := makeDefaultImageFilters()
|
|
|
|
return Config{
|
|
|
|
ImageFilters: ImageFilters,
|
|
|
|
ImagesDir: "bgs",
|
|
|
|
Duration: 1,
|
|
|
|
Cache: ".gopaper",
|
|
|
|
}
|
2024-10-27 16:21:28 +00:00
|
|
|
}
|
|
|
|
|
2024-11-04 15:42:24 +00:00
|
|
|
func (config *Config) load() error {
|
2024-10-31 11:54:39 +00:00
|
|
|
j := jsonc.New()
|
|
|
|
|
2024-10-30 20:36:10 +00:00
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
2024-11-04 15:42:24 +00:00
|
|
|
log.Println("could not get home directory!")
|
|
|
|
return err
|
2024-10-30 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 11:54:39 +00:00
|
|
|
configPath := homeDir + "/.config/gopaper/config.jsonc"
|
|
|
|
configRaw, err := os.ReadFile(configPath)
|
2024-10-27 16:21:28 +00:00
|
|
|
if err != nil {
|
2024-10-31 11:54:39 +00:00
|
|
|
if !os.IsExist(err) {
|
|
|
|
defaultConfig := makeDefaultConfig()
|
|
|
|
defaultConfigJSON, err := json.MarshalIndent(defaultConfig, "", "")
|
|
|
|
if err != nil {
|
2024-11-04 15:42:24 +00:00
|
|
|
log.Println("could not encode default config!")
|
|
|
|
return err
|
2024-10-31 11:54:39 +00:00
|
|
|
}
|
|
|
|
file, err := os.Create(configPath)
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
2024-11-04 15:42:24 +00:00
|
|
|
log.Println("could not create config file!")
|
|
|
|
return err
|
2024-10-31 11:54:39 +00:00
|
|
|
}
|
|
|
|
file.Write(defaultConfigJSON)
|
|
|
|
*config = defaultConfig
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
configRaw = j.Strip(configRaw)
|
|
|
|
err = json.Unmarshal(configRaw, &config)
|
|
|
|
if err != nil {
|
2024-11-04 15:42:24 +00:00
|
|
|
log.Println("Couldn't unmarshal config!")
|
|
|
|
return err
|
2024-10-31 11:54:39 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 18:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.Cache = homeDir + "/" + config.Cache + "/"
|
2024-10-31 11:54:39 +00:00
|
|
|
//make directory if it doesn't exist
|
|
|
|
_, err = os.Stat(config.Cache)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fmt.Printf("Cache directory %v not found. Making one.\n", config.Cache)
|
|
|
|
os.Mkdir(config.Cache, 0777)
|
|
|
|
}
|
|
|
|
|
2024-10-30 18:22:03 +00:00
|
|
|
config.ImagesDir = homeDir + "/" + config.ImagesDir + "/"
|
2024-11-04 15:42:24 +00:00
|
|
|
return nil
|
2024-10-27 16:21:28 +00:00
|
|
|
}
|