create .restic and config files if not existing, with default values
This commit is contained in:
parent
48c1aea220
commit
6c32ff3af3
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,23 +30,23 @@ func IsDev() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func PasswordFile() string {
|
func PasswordFile() string {
|
||||||
return configDir + "password.txt"
|
return filepath.Join(configDir, "password.txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
func LogFile() string {
|
func LogFile() string {
|
||||||
return configDir + "log.txt"
|
return filepath.Join(configDir, "log.txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExcludeFile() string {
|
func ExcludeFile() string {
|
||||||
return configDir + "excludes.txt"
|
return filepath.Join(configDir, "excludes.txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigFile() string {
|
func ConfigFile() string {
|
||||||
return configDir + "config.json"
|
return filepath.Join(configDir, "config.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cnf *Config) MountDir() string {
|
func (cnf *Config) MountDir() string {
|
||||||
return configDir + "mnt"
|
return filepath.Join(configDir, "mnt")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cnf *Config) CreateMountDirIfDoesntExist() error {
|
func (cnf *Config) CreateMountDirIfDoesntExist() error {
|
||||||
|
@ -55,13 +56,25 @@ func (cnf *Config) CreateMountDirIfDoesntExist() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func defaultConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
BackupTimeInHours: DefaultBackupTimeInHours,
|
||||||
|
Backup: home,
|
||||||
|
Repository: os.TempDir(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ReadConfig() (*Config, error) {
|
func ReadConfig() (*Config, error) {
|
||||||
confData, err := os.ReadFile(ConfigFile())
|
confData, err := os.ReadFile(ConfigFile())
|
||||||
|
conf := &Config{}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("No config.json found: %w", err)
|
log.Warn().Err(err).Msg("No config.json found, writing default")
|
||||||
|
conf = defaultConfig()
|
||||||
|
confData, _ := json.Marshal(conf)
|
||||||
|
os.WriteFile(ConfigFile(), confData, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
conf := &Config{}
|
|
||||||
err = json.Unmarshal(confData, conf)
|
err = json.Unmarshal(confData, conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("config.json malformed JSON: %w", err)
|
return nil, fmt.Errorf("config.json malformed JSON: %w", err)
|
||||||
|
@ -75,5 +88,12 @@ func ReadConfig() (*Config, error) {
|
||||||
conf.BackupTimeInHours = DefaultBackupTimeInHours
|
conf.BackupTimeInHours = DefaultBackupTimeInHours
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(ExcludeFile()); os.IsNotExist(err) {
|
||||||
|
os.WriteFile(ExcludeFile(), []byte(""), os.ModePerm)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(PasswordFile()); os.IsNotExist(err) {
|
||||||
|
os.WriteFile(PasswordFile(), []byte(""), os.ModePerm)
|
||||||
|
}
|
||||||
|
|
||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue