mirror of https://github.com/qwc/backive.git
Some small progress...
This commit is contained in:
parent
17ec990dc9
commit
716a3c9d4a
|
@ -8,7 +8,7 @@ before:
|
|||
- go generate ./...
|
||||
builds:
|
||||
- main: ./cmd/backive/main.go
|
||||
id: backive
|
||||
id: "backive"
|
||||
binary: backive
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
|
@ -19,7 +19,7 @@ builds:
|
|||
# - windows
|
||||
# - darwin
|
||||
- main: ./cmd/backive_udev/main.go
|
||||
id: backive_udev
|
||||
id: "backive_udev"
|
||||
binary: backive_udev
|
||||
goarch:
|
||||
- amd64
|
||||
|
@ -30,7 +30,7 @@ builds:
|
|||
# - windows
|
||||
# - darwin
|
||||
- main: ./cmd/backive_ui/main.go
|
||||
id: backive_ui
|
||||
id: "backive_ui"
|
||||
binary: backive_ui
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package backive
|
||||
package main
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
@ -12,6 +13,24 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var logfile os.File
|
||||
|
||||
func setupLogging() {
|
||||
logdir := "/var/log/backive"
|
||||
logname := "/var/log/backive/backive.log"
|
||||
if _, err := os.Stat(logdir); err == nil {
|
||||
//ignore
|
||||
} else if os.IsNotExist(err) {
|
||||
os.MkdirAll(logdir, 0755)
|
||||
}
|
||||
logfile, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating logfile!")
|
||||
panic("no logfile no info")
|
||||
}
|
||||
log.SetOutput(logfile)
|
||||
}
|
||||
|
||||
// Global variables for backive
|
||||
var (
|
||||
database Database
|
||||
|
@ -41,11 +60,16 @@ func (d Database) Save() {
|
|||
|
||||
// LoadDb loads the database
|
||||
func (d Database) Load() {
|
||||
data, err := os.ReadFile(d.path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if _, err := os.Stat(d.path); err == nil {
|
||||
data, rferr := os.ReadFile(d.path)
|
||||
if rferr != nil {
|
||||
panic(rferr)
|
||||
}
|
||||
json.Unmarshal(data, &d.data)
|
||||
} else if os.IsNotExist(err) {
|
||||
// no data
|
||||
|
||||
}
|
||||
json.Unmarshal(data, &d.data)
|
||||
}
|
||||
|
||||
// Device represents a device, with a name easy to remember and the UUID to identify it, optionally an owner.
|
||||
|
@ -88,7 +112,7 @@ type Configuration struct {
|
|||
Settings Settings `mapstructure:"settings"`
|
||||
Devices Devices `mapstructure:"devices"`
|
||||
Backups Backups `mapstructure:"backups"`
|
||||
vconfig *viper.Viper
|
||||
vconfig viper.Viper
|
||||
}
|
||||
|
||||
// Settings struct holds the global configuration items
|
||||
|
@ -107,26 +131,30 @@ type Backups map[string]Backup
|
|||
// CreateViper creates a viper instance for usage later
|
||||
func (c Configuration) CreateViper() {
|
||||
vconfig := viper.New()
|
||||
// vconfig.Debug()
|
||||
vconfig.SetConfigName("backive")
|
||||
vconfig.SetConfigFile("backive.yml")
|
||||
//vconfig.SetConfigFile("backive.yaml")
|
||||
vconfig.SetConfigType("yaml")
|
||||
vconfig.AddConfigPath("/etc/backive/") // system config
|
||||
vconfig.AddConfigPath("$HOME/.backive/")
|
||||
vconfig.AddConfigPath("/etc/backive") // system config
|
||||
//vconfig.AddConfigPath("$HOME/.backive/")
|
||||
vconfig.AddConfigPath(".")
|
||||
c.vconfig = vconfig
|
||||
c.vconfig = *vconfig
|
||||
}
|
||||
|
||||
// Load loads the configuration from the disk
|
||||
func (c Configuration) Load() {
|
||||
c.CreateViper()
|
||||
if err := c.vconfig.ReadInConfig(); err != nil {
|
||||
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"))
|
||||
panic(fmt.Errorf("Fatal: No config file could be found: %w", err))
|
||||
}
|
||||
panic(fmt.Errorf("Fatal error config file: %w ", err))
|
||||
}
|
||||
|
||||
//Unmarshal all into Configuration type
|
||||
err := c.vconfig.Unmarshal(c)
|
||||
err := vc.Unmarshal(c)
|
||||
if err != nil {
|
||||
fmt.Printf("Error occured when loading config: %v\n", err)
|
||||
panic("No configuration available!")
|
||||
|
@ -173,24 +201,25 @@ func (eh EventHandler) RegisterCallback(cb func(map[string]string)) {
|
|||
func (eh EventHandler) process() {
|
||||
client, err := eh.ls.Accept()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
data := make([]byte, 2048)
|
||||
for {
|
||||
buf := make([]byte, 512)
|
||||
nr, err := client.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
data = append(data, buf[0:nr]...)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
}
|
||||
log.Println(data)
|
||||
env := map[string]string{}
|
||||
errjson := json.Unmarshal(data, &env)
|
||||
if errjson != nil {
|
||||
panic(errjson)
|
||||
log.Fatal(errjson)
|
||||
}
|
||||
for _, v := range eh.callbacks {
|
||||
v(env)
|
||||
|
@ -296,8 +325,9 @@ func (r Runs) LastRun(b Backup) (time.Time, error) {
|
|||
}
|
||||
|
||||
func main() {
|
||||
setupLogging()
|
||||
// TODO: do proper signal handling!
|
||||
fmt.Println("backive starting up...")
|
||||
log.Println("backive starting up...")
|
||||
// find and load config
|
||||
database.Load()
|
||||
config.Load()
|
||||
|
@ -312,5 +342,5 @@ func main() {
|
|||
|
||||
// cleanup if anything is there to cleanup
|
||||
database.Save()
|
||||
fmt.Println("backive shuting down.")
|
||||
log.Println("backive shuting down.")
|
||||
}
|
||||
|
|
|
@ -11,7 +11,14 @@ import (
|
|||
|
||||
// main Simple main function for the udev callback executable, registered with the udev service.
|
||||
func main() {
|
||||
f, err := os.OpenFile("/tmp/backive/udev.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||
udev_logdir := "/var/log/backive"
|
||||
udev_logname := "/var/log/backive/udev.log"
|
||||
if _, err := os.Stat(udev_logdir); err == nil {
|
||||
//ignore
|
||||
} else if os.IsNotExist(err) {
|
||||
os.MkdirAll(udev_logdir, 0755)
|
||||
}
|
||||
f, err := os.OpenFile(udev_logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating logfile!")
|
||||
panic("no logfile no info")
|
||||
|
|
2
go.mod
2
go.mod
|
@ -16,7 +16,7 @@ require (
|
|||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.2.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20210930141918-969570ce7c6c // indirect
|
||||
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/ini.v1 v1.63.2 // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -432,8 +432,8 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210930141918-969570ce7c6c h1:ayiZ33F3u3LIXB03Y5VKNdaFO79a18Fr+SB30o/KFyw=
|
||||
golang.org/x/sys v0.0.0-20210930141918-969570ce7c6c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw=
|
||||
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
|
Loading…
Reference in New Issue