Skip to content

Commit

Permalink
add changes
Browse files Browse the repository at this point in the history
  • Loading branch information
florianrusch committed Nov 9, 2023
1 parent af2691d commit dca4fbe
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 35 deletions.
40 changes: 33 additions & 7 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ import (
"github.com/cluetec/lifeboat/internal/logging"
"github.com/cluetec/lifeboat/internal/source"
"github.com/spf13/cobra"
"io"
"log/slog"
"os"
)

// backupCmd represents the backup command
var backupCmd = &cobra.Command{
Use: "backup",
Short: "Execute the backup.",
Long: "Execute the backup. Used config can be overridden by providing arguments.",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.New()
if err != nil {
slog.Error("error while initializing config", "error", err)
os.Exit(1)
return err
}

logging.InitSlog(c.GetLogLevel())
Expand All @@ -45,11 +45,37 @@ var backupCmd = &cobra.Command{

slog.Debug("start of backup command")

source.Prepare(c.Source)
destination.Prepare(c.Destination)
s, err := source.New(c.Source)
if err != nil {
slog.Error("error while initializing source", "error", err)
return err
}
defer func() {
err := s.Reader.Close()
if err != nil {
slog.Error("error while closing source reader", "error", err)
}
}()

slog.Info("TODO: Do backup")
d, err := destination.New(c.Destination)
if err != nil {
slog.Error("error while initializing destination", "error", err)
return err
}
defer func() {
err := d.Writer.Close()
if err != nil {
slog.Error("error while closing destination writer", "error", err)
}
}()

n, err := io.Copy(d.Writer, s.Reader)
if err != nil {
slog.Error("error while doing the backup", "error", err)
return err
}

slog.Debug("end of backup command")
slog.Info("Backup successful", "writtenBytes", n)
return nil
},
}
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "lb",
Short: "Perform backups from any source to any destination.",
Long: "Lifeboat is a general purpose backup tool which supports backups for arbitrary sources and destinations.",
Use: "lb",
Short: "Perform backups from any source to any destination.",
Long: "Lifeboat is a general purpose backup tool which supports backups for arbitrary sources and destinations.",
SilenceUsage: true,
SilenceErrors: true,
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source:
type: filesystem
path: source
path: /tmp/source.txt

destination:
type: filesystem
path: destination
path: /tmp/destination.txt

logLevel: debug
21 changes: 17 additions & 4 deletions internal/destination/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,31 @@ package destination
import (
"github.com/cluetec/lifeboat/internal/config"
"github.com/cluetec/lifeboat/internal/destination/filesystem"
"io"
"log/slog"
"os"
)

func Prepare(c config.DestinationConfig) {
type Destination struct {
Writer io.WriteCloser
}

func New(c config.DestinationConfig) (*Destination, error) {
d := Destination{}
if c.Type == filesystem.Type {
filesystemConfig, err := filesystem.New(c.ResourceConfig)
filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig)
if err != nil {
slog.Error("error while initializing filesystem destination config", "error", err)
os.Exit(1)
return nil, err
}

slog.Debug("filesystem destination config loaded", "config", filesystemConfig)

d.Writer, err = filesystem.NewWriter(filesystemConfig)
if err != nil {
slog.Error("error while initializing writer interface for filesystem destination", "error", err)
return nil, err
}
}

return &d, nil
}
2 changes: 1 addition & 1 deletion internal/destination/filesystem/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Config struct {
Path string
}

func New(c map[string]any) (*Config, error) {
func NewConfig(c map[string]any) (*Config, error) {
var filesystemConfig Config

err := mapstructure.Decode(c, &filesystemConfig)
Expand Down
48 changes: 44 additions & 4 deletions internal/destination/filesystem/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,50 @@

package filesystem

type Storeman struct {
Config Config
import (
"errors"
"log/slog"
"os"
)

type Writer struct {
file *os.File
}

func (r *Storeman) StoreBackup() []byte {
return []byte{}
func NewWriter(c *Config) (*Writer, error) {
w := Writer{}

// Check if destination file already exists
_, err := os.Stat(c.Path)
if err == nil {
return nil, errors.New("destination file already exists")
} else if !errors.Is(err, os.ErrNotExist) {
slog.Error("error while checking if destination file already exists", "error", err)
return nil, err
}

// Create file
f, err := os.Create(c.Path)
if err != nil {
return nil, err
}

w.file = f
return &w, nil
}

func (w *Writer) Write(b []byte) (int, error) {
return w.file.Write(b)
}

func (w *Writer) Close() error {
slog.Debug("closing filesystem writer")

if w.file != nil {
if err := w.file.Close(); err != nil {
return err
}
w.file = nil
}
return nil
}
2 changes: 1 addition & 1 deletion internal/source/filesystem/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Config struct {
Path string
}

func New(c map[string]any) (*Config, error) {
func NewConfig(c map[string]any) (*Config, error) {
var filesystemConfig Config

err := mapstructure.Decode(c, &filesystemConfig)
Expand Down
36 changes: 33 additions & 3 deletions internal/source/filesystem/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,40 @@

package filesystem

import (
"log/slog"
"os"
)

type Reader struct {
Config *Config
file *os.File
}

func (r *Reader) ProcessBackup() []byte {
return []byte{}
func NewReader(c *Config) (*Reader, error) {
r := Reader{}

f, err := os.Open(c.Path)
if err != nil {
return nil, err
}

r.file = f

return &r, nil
}

func (r *Reader) Read(b []byte) (int, error) {
return r.file.Read(b)
}

func (r *Reader) Close() error {
slog.Debug("closing filesystem reader")

if r.file != nil {
if err := r.file.Close(); err != nil {
return err
}
r.file = nil
}
return nil
}
21 changes: 17 additions & 4 deletions internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,31 @@ package source
import (
"github.com/cluetec/lifeboat/internal/config"
"github.com/cluetec/lifeboat/internal/source/filesystem"
"io"
"log/slog"
"os"
)

func Prepare(c config.SourceConfig) {
type Source struct {
Reader io.ReadCloser
}

func New(c config.SourceConfig) (*Source, error) {
s := Source{}
if c.Type == filesystem.Type {
filesystemConfig, err := filesystem.New(c.ResourceConfig)
filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig)
if err != nil {
slog.Error("error while initializing filesystem source config", "error", err)
os.Exit(1)
return nil, err
}

slog.Debug("filesystem source config loaded", "config", filesystemConfig)

s.Reader, err = filesystem.NewReader(filesystemConfig)
if err != nil {
slog.Error("error while initializing reader interface for filesystem source", "error", err)
return nil, err
}
}

return &s, nil
}
12 changes: 6 additions & 6 deletions internal_open_discussion.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## Planned features

- [ ] Step: 1
- [x] Step: 1
- [x] Init cobra & viper setup
- [ ] Dynamic config loading with viper (`type`)
- [ ] Step: 2
- [ ] Init slog setup
- [ ] Implement Filesystem to Filesystem backup
- [ ] Think about the interfaces
- [x] Dynamic config loading with viper (`type`)
- [x] Step: 2
- [x] Init slog setup
- [x] Implement Filesystem to Filesystem backup
- [x] Think about the interfaces
- [ ] Implement Vault to Filesystem backup

For later:
Expand Down

0 comments on commit dca4fbe

Please sign in to comment.