79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package restic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/rs/zerolog/log"
|
|
"os"
|
|
)
|
|
|
|
type Config struct {
|
|
Repository string `json:"repository"`
|
|
Backup string `json:"backup"`
|
|
BackupTimeInHours int `json:"backupTimeInHours"`
|
|
}
|
|
|
|
const (
|
|
DefaultBackupTimeInHours int = 24
|
|
ShortTimeFormat string = "2006-01-02T15:04:05"
|
|
)
|
|
|
|
var home, _ = os.UserHomeDir()
|
|
var executable, _ = os.Executable()
|
|
var isDev = os.Getenv("RESTICTRAY_DEV")
|
|
|
|
func IsDev() bool {
|
|
return isDev != ""
|
|
}
|
|
|
|
func PasswordFile() string {
|
|
return home + "/.restic/password.txt"
|
|
}
|
|
|
|
func LogFile() string {
|
|
return home + "/.restic/log.txt"
|
|
}
|
|
|
|
func ExcludeFile() string {
|
|
return home + "/.restic/excludes.txt"
|
|
}
|
|
|
|
func ConfigFile() string {
|
|
return home + "/.restic/config.json"
|
|
}
|
|
|
|
func (cnf *Config) MountDir() string {
|
|
return home + "/.restic/mnt"
|
|
}
|
|
|
|
func (cnf *Config) CreateMountDirIfDoesntExist() error {
|
|
if _, err := os.Stat(cnf.MountDir()); os.IsNotExist(err) {
|
|
return os.Mkdir(cnf.MountDir(), os.ModePerm)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ReadConfig() (*Config, error) {
|
|
confData, err := os.ReadFile(ConfigFile())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("No config.json found: %w", err)
|
|
}
|
|
|
|
conf := &Config{}
|
|
err = json.Unmarshal(confData, conf)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("config.json malformed JSON: %w", err)
|
|
}
|
|
if conf.Repository == "" || conf.Backup == "" {
|
|
err := errors.New("config.json is missing required keys 'Backup' or 'Repository'")
|
|
return nil, err
|
|
}
|
|
if conf.BackupTimeInHours == 0 {
|
|
log.Warn().Msg("backupTimeInHours missing in config, reverting to 24hrs")
|
|
conf.BackupTimeInHours = DefaultBackupTimeInHours
|
|
}
|
|
|
|
return conf, nil
|
|
}
|