fiz config initialization
This commit is contained in:
parent
ff802ba435
commit
9954de7453
19
config.go
19
config.go
|
@ -2,10 +2,15 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/adhocore/jsonc"
|
"github.com/adhocore/jsonc"
|
||||||
|
// "github.com/adhocore/jsonc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -21,7 +26,7 @@ func makeDefaultConfig() Config {
|
||||||
return Config{
|
return Config{
|
||||||
ImageFilters: ImageFilters,
|
ImageFilters: ImageFilters,
|
||||||
ImagesDir: "bgs",
|
ImagesDir: "bgs",
|
||||||
Duration: 1,
|
Duration: 60,
|
||||||
Cache: ".gopaper",
|
Cache: ".gopaper",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,21 +41,21 @@ func (config *Config) load() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
configDir := homeDir + "/.config/gopaper/"
|
configDir := homeDir + "/.config/gopaper/"
|
||||||
configRaw, err := os.ReadFile(configDir)
|
configRaw, readError := os.ReadFile(configDir + "config.jsonc")
|
||||||
if err != nil {
|
if readError != nil && errors.Is(readError, fs.ErrNotExist) {
|
||||||
if !os.IsExist(err) {
|
|
||||||
defaultConfig := makeDefaultConfig()
|
defaultConfig := makeDefaultConfig()
|
||||||
defaultConfigJSON, err := json.MarshalIndent(defaultConfig, "", "")
|
defaultConfigJSON, err := json.MarshalIndent(defaultConfig, "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
elog.Println("could not encode default config!")
|
elog.Println("could not encode default config!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//TODO: make parent directories if they don't exist
|
|
||||||
err = os.Mkdir(homeDir+"/.config", 755)
|
err = os.Mkdir(homeDir+"/.config", 755)
|
||||||
if err != nil && !os.IsExist(err) {
|
if err != nil && !os.IsExist(err) {
|
||||||
elog.Println("could not create .config directory", err)
|
elog.Println("could not create .config directory", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Mkdir(configDir, 755)
|
err = os.Mkdir(configDir, 755)
|
||||||
if err != nil && !os.IsExist(err) {
|
if err != nil && !os.IsExist(err) {
|
||||||
elog.Println("could not create .config/gopaper directory")
|
elog.Println("could not create .config/gopaper directory")
|
||||||
|
@ -66,15 +71,15 @@ func (config *Config) load() error {
|
||||||
file.Write(defaultConfigJSON)
|
file.Write(defaultConfigJSON)
|
||||||
*config = defaultConfig
|
*config = defaultConfig
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
|
||||||
|
if readError == nil {
|
||||||
configRaw = j.Strip(configRaw)
|
configRaw = j.Strip(configRaw)
|
||||||
err = json.Unmarshal(configRaw, &config)
|
err = json.Unmarshal(configRaw, &config)
|
||||||
|
log.Printf("config: %v", config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
elog.Println("Couldn't unmarshal config!")
|
elog.Println("Couldn't unmarshal config!")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Cache = homeDir + "/" + config.Cache + "/"
|
config.Cache = homeDir + "/" + config.Cache + "/"
|
||||||
|
|
7
main.go
7
main.go
|
@ -1,13 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/natefinch/lumberjack"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/natefinch/lumberjack"
|
||||||
)
|
)
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
|
@ -83,7 +86,7 @@ func cleanExit(code int) {
|
||||||
|
|
||||||
func killPrevProc() error {
|
func killPrevProc() error {
|
||||||
//check if pidfile already exists
|
//check if pidfile already exists
|
||||||
if _, err := os.Stat(pidfilePath); os.IsNotExist(err) {
|
if _, err := os.Stat(pidfilePath); errors.Is(err, fs.ErrNotExist) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
//if it does, kill the process and delete the pidfile
|
//if it does, kill the process and delete the pidfile
|
||||||
|
|
Loading…
Reference in New Issue