Satisfying GoLint

This commit is contained in:
Marcel Otte 2022-01-07 11:17:12 +01:00
parent 17960a9e2f
commit d9405890d5
8 changed files with 25 additions and 22 deletions

View File

@ -4,6 +4,7 @@ var config Configuration
var runs Runs
var database Database
// Init initializes backive with the two basic data structures required, the config, and the database
func Init(cfg Configuration, db Database) {
config = cfg
database = db

View File

@ -28,33 +28,35 @@ type Backup struct {
// Backups is nothing else than a name to Backup type mapping
type Backups map[string]*Backup
// findBackupsForDevice only finds the first backup which is configured for a given device.
// FindBackupsForDevice only finds the first backup which is configured for a given device.
func (bs *Backups) FindBackupsForDevice(d Device) ([]*Backup, bool) {
var backups []*Backup = []*Backup{}
var backups = []*Backup{}
for _, b := range *bs {
if d.Name == b.TargetDevice {
backups = append(backups, b)
}
}
var ret bool = len(backups) > 0
var ret = len(backups) > 0
return backups, ret
}
// CanRun Checks the configuration items required and checks the frequency setting with the run database if a Backup should run.
func (b *Backup) CanRun() error {
// target path MUST exist
if b.TargetPath == "" {
return fmt.Errorf("The setting targetPath MUST exist within a backup configuration.")
return fmt.Errorf("the setting targetPath MUST exist within a backup configuration")
}
// script must exist, having only script means this is handled in the script
if b.ScriptPath == "" {
return fmt.Errorf("The setting scriptPath must exist within a backup configuration.")
return fmt.Errorf("the setting scriptPath must exist within a backup configuration")
}
if !b.ShouldRun() {
return fmt.Errorf("Frequency (days inbetween) not reached.")
return fmt.Errorf("frequency (days inbetween) not reached")
}
return nil
}
// PrepareRun prepares a run for a backup, creates a logger for the execution of the backup script and gives the rights of the directory recursively to the user specified.
func (b *Backup) PrepareRun() error {
backupPath := path.Join(
config.Settings.SystemMountPoint,
@ -96,7 +98,7 @@ func (b *Backup) Run() error {
if !strings.ContainsAny(b.ScriptPath, "/") || strings.HasPrefix(b.ScriptPath, ".") {
//The scriptPath is a relative path, from the place of the config, so use the config as base
log.Printf("ERROR: Script path is relative, aborting.")
return fmt.Errorf("Script path is relative, aborting.")
return fmt.Errorf("script path is relative, aborting")
}
cmd := exec.Command("/usr/bin/sh", b.ScriptPath)
if b.ExeUser != "" {
@ -129,7 +131,7 @@ func (b *Backup) Run() error {
return nil
}
// quit with error that the device is not available.
return fmt.Errorf("The device is not mounted")
return fmt.Errorf("the device is not mounted")
}
// Runs contains the Data for the scheduler: mapping from backups to a list of timestamps of the last 10 backups
@ -199,5 +201,5 @@ func (r *Runs) LastRun(b *Backup) (time.Time, error) {
return t, nil
}
}
return time.Unix(0, 0), fmt.Errorf("Backup name not found and therefore has never run")
return time.Unix(0, 0), fmt.Errorf("backup name not found and therefore has never run")
}

View File

@ -11,8 +11,6 @@ import (
"github.com/qwc/backive"
)
var logfile os.File
func setupLogging() {
logname := "/var/log/backive/backive.log"
logdir, _ := path.Split(logname)

View File

@ -44,9 +44,9 @@ func (c *Configuration) Load() {
vc := c.Vconfig
if err := vc.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic(fmt.Errorf("Fatal: No config file could be found: %w", err))
panic(fmt.Errorf("fatal: No config file could be found: %w", err))
}
panic(fmt.Errorf("Fatal error config file: %w ", err))
panic(fmt.Errorf("fatal error config file: %w ", err))
}
log.Printf("Configuration file used: %s", vc.ConfigFileUsed())

View File

@ -12,7 +12,7 @@ type Database struct {
data map[string]string
}
var dbPath string = "/var/lib/backive/data.json"
var dbPath = "/var/lib/backive/data.json"
// Save saves the database
func (d *Database) Save() {
@ -29,7 +29,7 @@ func (d *Database) Save() {
}
}
// LoadDb loads the database
// Load loads the database
func (d *Database) Load() {
if _, err := os.Stat(dbPath); err == nil {
data, rferr := os.ReadFile(dbPath)
@ -37,8 +37,7 @@ func (d *Database) Load() {
panic(rferr)
}
json.Unmarshal(data, &d.data)
} else if os.IsNotExist(err) {
} /*else if os.IsNotExist(err) {
// no data
}
}*/
}

View File

@ -6,7 +6,7 @@ import (
"path"
)
var devsByUuid string = "/dev/disk/by-uuid"
var devsByUUID = "/dev/disk/by-uuid"
// Devices is nothing else than a name to Device type mapping
type Devices map[string]*Device
@ -29,7 +29,7 @@ func (d *Device) Mount() error {
log.Printf("Executing mount command for %s", d.Name)
cmd := exec.Command(
"mount",
path.Join(devsByUuid, d.UUID),
path.Join(devsByUUID, d.UUID),
path.Join(config.Settings.SystemMountPoint, d.Name),
)
cmd.Stdout = log.Writer()
@ -69,6 +69,7 @@ func (d *Device) Unmount() error {
return nil
}
// IsMounted returns the mount state of the device
func (d *Device) IsMounted() bool {
return d.isMounted
}

View File

@ -9,9 +9,10 @@ import (
"path"
)
// EventHandler holds the necessary elements to get an eventhandler setup and working.
type EventHandler struct {
ls net.Listener
done <-chan struct{}
ls net.Listener
//done <-chan struct{}
callbacks []func(map[string]string)
}

View File

@ -5,6 +5,7 @@ import (
"os"
)
// CreateDirectoryIfNotExists Checks for a directory string and creates the directory if it does not exist, must be a absolute path.
func CreateDirectoryIfNotExists(dir string) {
if _, err := os.Stat(dir); err == nil {
//ignore