Skip to content

Commit

Permalink
Merge branch 'main' into florianrusch-cluetec-patch-1
Browse files Browse the repository at this point in the history
# Conflicts:
#	config.yaml
#	internal/destination/filesystem/config.go
#	internal/source/filesystem/config.go
  • Loading branch information
florianrusch committed Jan 4, 2024
2 parents 27d7383 + fc243c5 commit 3241f2d
Show file tree
Hide file tree
Showing 18 changed files with 356 additions and 57 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/greetings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ jobs:
pull-requests: write # for actions/first-interaction to comment on first PR
steps:
- name: Harden Runner
uses: step-security/harden-runner@1b05615854632b887b69ae1be8cbefe72d3ae423 # v2.6.0
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
with:
allowed-endpoints: >
api.github.com:443
disable-sudo: true
disable-telemetry: true
egress-policy: block
- uses: actions/first-interaction@1dbfe1ba5525b8257e1f259b09745bee346d62d8 # v1.2.0
- uses: actions/first-interaction@34f15e814fe48ac9312ccf29db4e74fa767cbab7 # v1.3.0
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: "Thanks for your contribution 🔥 We will take a look asap 🚀\n\nIn the meantime we recommend each new contribute to have a look at the [contribution guide](https://github.com/cluetec/lifeboat/CONTRIBUTING.md)."
pr-message: "Thanks for your contribution 🔥 We will take a look asap 🚀\n\nIn the meantime we recommend each new contribute to have a look at the [contribution guide](https://github.com/cluetec/lifeboat/CONTRIBUTING.md)."
issue-message: "Thank you for your contribution 🔥 We will look into this as soon as possible 🚀\n\n In the meantime, we recommend every new contributor to take a look at the [Contributor Guide](https://github.com/cluetec/lifeboat/CONTRIBUTING.md)."
pr-message: "Thank you for your contribution 🔥 We will look into this as soon as possible 🚀\n\n In the meantime, we recommend every new contributor to take a look at the [Contributor Guide](https://github.com/cluetec/lifeboat/CONTRIBUTING.md)."
2 changes: 1 addition & 1 deletion .github/workflows/labeler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@1b05615854632b887b69ae1be8cbefe72d3ae423 # v2.6.0
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
with:
allowed-endpoints: >
api.github.com:443
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/markdown-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@1b05615854632b887b69ae1be8cbefe72d3ae423 # v2.6.0
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
with:
allowed-endpoints: >
github.com:443
Expand All @@ -40,7 +40,7 @@ jobs:
egress-policy: block
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: markdownlint-cli2-action
uses: DavidAnson/markdownlint-cli2-action@v13
uses: DavidAnson/markdownlint-cli2-action@v14

link-check:
runs-on: ubuntu-latest
Expand Down
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,11 +1,11 @@
source:
type: filesystem
filesystem:
path: source
path: /tmp/source.txt

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

logLevel: debug
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
)

Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down Expand Up @@ -165,8 +165,8 @@ github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.17.0 h1:I5txKw7MJasPL/BrfkbA0Jyo/oELqVmux4pR/UxOMfI=
Expand Down
6 changes: 4 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import (
"strings"
)

type ResourceConfig map[string]any

type SourceConfig struct {
Type string
ResourceConfig map[string]any `mapstructure:",remain"`
ResourceConfig ResourceConfig `mapstructure:",remain"`
}

type DestinationConfig struct {
Type string
ResourceConfig map[string]any `mapstructure:",remain"`
ResourceConfig ResourceConfig `mapstructure:",remain"`
}

type Config struct {
Expand Down
31 changes: 22 additions & 9 deletions internal/destination/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@
package destination

import (
"errors"
"github.com/cluetec/lifeboat/internal/config"
"github.com/cluetec/lifeboat/internal/destination/filesystem"
"io"
"log/slog"
"os"
)

func Prepare(c config.DestinationConfig) {
if c.Type == filesystem.Type {
filesystemConfig, err := filesystem.New(c.ResourceConfig)
if err != nil {
slog.Error("error while initializing filesystem destination config", "error", err)
os.Exit(1)
}
type Destination struct {
Writer io.WriteCloser
}

func New(c config.DestinationConfig) (*Destination, error) {
d := Destination{}
var err error

switch {
case c.Type == filesystem.Type:
d.Writer, err = filesystem.NewWriter(&c.ResourceConfig)
}
if err != nil {
slog.Error("error while initializing writer interface for destination system", "destinationType", c.Type, "error", err)
return nil, err
}

slog.Debug("filesystem destination config loaded", "config", filesystemConfig)
if d.Writer == nil {
return nil, errors.New("destination type not known")
}

return &d, nil
}
54 changes: 54 additions & 0 deletions internal/destination/destination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 cluetec GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package destination

import (
"github.com/cluetec/lifeboat/internal/config"
"reflect"
"testing"
)

func TestNew(t *testing.T) {
type args struct {
c config.DestinationConfig
}
tests := []struct {
name string
args args
want *Destination
wantErr bool
}{
{
name: "Throw error if type is not known",
args: args{c: config.DestinationConfig{Type: "not-known-type"}},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.c)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() got = %v, want %v", got, tt.want)
}
})
}
}
15 changes: 8 additions & 7 deletions internal/destination/filesystem/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,30 @@
package filesystem

import (
globalConfig "github.com/cluetec/lifeboat/internal/config"
"github.com/mitchellh/mapstructure"
"log/slog"
)

const Type = "filesystem"

type MetaConfig struct {
Filesystem Config
type metaConfig struct {
Filesystem config
}

type Config struct {
type config struct {
Path string
}

func New(c map[string]any) (*Config, error) {
var config MetaConfig
func newConfig(rc *globalConfig.ResourceConfig) (*config, error) {
var c metaConfig

err := mapstructure.Decode(c, &config)
err := mapstructure.Decode(rc, &c)

if err != nil {
slog.Error("unable to decode config into filesystem destination config", "error", err)
return nil, err
}

return &config.Filesystem, nil
return &c.Filesystem, nil
}
72 changes: 72 additions & 0 deletions internal/destination/filesystem/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2023 cluetec GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package filesystem

import (
"errors"
globalConfig "github.com/cluetec/lifeboat/internal/config"
"log/slog"
"os"
)

type Writer struct {
file *os.File
}

func NewWriter(rc *globalConfig.ResourceConfig) (*Writer, error) {
c, err := newConfig(rc)
if err != nil {
slog.Error("error while initializing filesystem destination config", "error", err)
return nil, err
}

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

// 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
}

return &Writer{file: f}, nil
}

func (w *Writer) Write(b []byte) (int, error) {
slog.Debug("filesystem destination write got called")
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
}
Loading

0 comments on commit 3241f2d

Please sign in to comment.