From c5ffba24af44d23723f318576302dc8077e32db0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 6 Nov 2023 11:54:25 +0000 Subject: [PATCH 1/8] chore(deps): bump github.com/spf13/cobra from 1.7.0 to 1.8.0 Bumps [github.com/spf13/cobra](https://github.com/spf13/cobra) from 1.7.0 to 1.8.0. - [Release notes](https://github.com/spf13/cobra/releases) - [Commits](https://github.com/spf13/cobra/compare/v1.7.0...v1.8.0) --- updated-dependencies: - dependency-name: github.com/spf13/cobra dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index e5a9dd7..693eea5 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4f10baa..65146c4 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= From a8257ce87d124f6957595eb6634e1618521db4af Mon Sep 17 00:00:00 2001 From: "Florian Rusch (cluetec GmbH)" <110589194+florianrusch-cluetec@users.noreply.github.com> Date: Thu, 9 Nov 2023 13:58:33 +0100 Subject: [PATCH 2/8] feat: Implement filesystem to filesystem backup (#21) --- cmd/backup.go | 40 +++++++++++--- cmd/root.go | 8 +-- config.yaml | 4 +- internal/destination/destination.go | 21 ++++++-- internal/destination/filesystem/config.go | 2 +- internal/destination/filesystem/writer.go | 65 +++++++++++++++++++++++ internal/source/filesystem/config.go | 2 +- internal/source/filesystem/reader.go | 55 +++++++++++++++++++ internal/source/source.go | 21 ++++++-- internal_open_discussion.md | 12 ++--- 10 files changed, 202 insertions(+), 28 deletions(-) create mode 100644 internal/destination/filesystem/writer.go create mode 100644 internal/source/filesystem/reader.go diff --git a/cmd/backup.go b/cmd/backup.go index f1edd22..191ed63 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -22,8 +22,8 @@ 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 @@ -31,11 +31,11 @@ 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()) @@ -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 }, } diff --git a/cmd/root.go b/cmd/root.go index 2f97527..bb0c4a1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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. diff --git a/config.yaml b/config.yaml index 6d5f15f..c7b23da 100644 --- a/config.yaml +++ b/config.yaml @@ -1,9 +1,9 @@ source: type: filesystem - path: source + path: /tmp/source.txt destination: type: filesystem - path: destination + path: /tmp/destination.txt logLevel: debug diff --git a/internal/destination/destination.go b/internal/destination/destination.go index 784802a..74f9060 100644 --- a/internal/destination/destination.go +++ b/internal/destination/destination.go @@ -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 } diff --git a/internal/destination/filesystem/config.go b/internal/destination/filesystem/config.go index a495943..8df1550 100644 --- a/internal/destination/filesystem/config.go +++ b/internal/destination/filesystem/config.go @@ -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) diff --git a/internal/destination/filesystem/writer.go b/internal/destination/filesystem/writer.go new file mode 100644 index 0000000..17607a1 --- /dev/null +++ b/internal/destination/filesystem/writer.go @@ -0,0 +1,65 @@ +/* + * 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" + "log/slog" + "os" +) + +type Writer struct { + file *os.File +} + +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 +} diff --git a/internal/source/filesystem/config.go b/internal/source/filesystem/config.go index 3013f79..063ddaa 100644 --- a/internal/source/filesystem/config.go +++ b/internal/source/filesystem/config.go @@ -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) diff --git a/internal/source/filesystem/reader.go b/internal/source/filesystem/reader.go new file mode 100644 index 0000000..bfce89d --- /dev/null +++ b/internal/source/filesystem/reader.go @@ -0,0 +1,55 @@ +/* + * 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 ( + "log/slog" + "os" +) + +type Reader struct { + file *os.File +} + +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 +} diff --git a/internal/source/source.go b/internal/source/source.go index ff56043..633cecf 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -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 } diff --git a/internal_open_discussion.md b/internal_open_discussion.md index 46b4fc5..3cc60dd 100644 --- a/internal_open_discussion.md +++ b/internal_open_discussion.md @@ -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: From cb0d32a2b109f157eab391556b865b30d2cd1c1b Mon Sep 17 00:00:00 2001 From: "Florian Rusch (cluetec GmbH)" <110589194+florianrusch-cluetec@users.noreply.github.com> Date: Fri, 10 Nov 2023 06:28:45 +0100 Subject: [PATCH 3/8] fix: throw error if resource type is not known (#24) --- internal/destination/destination.go | 6 ++- internal/destination/destination_test.go | 54 ++++++++++++++++++++++++ internal/source/source.go | 5 ++- internal/source/source_test.go | 54 ++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 internal/destination/destination_test.go create mode 100644 internal/source/source_test.go diff --git a/internal/destination/destination.go b/internal/destination/destination.go index 74f9060..b40c87a 100644 --- a/internal/destination/destination.go +++ b/internal/destination/destination.go @@ -17,6 +17,7 @@ package destination import ( + "errors" "github.com/cluetec/lifeboat/internal/config" "github.com/cluetec/lifeboat/internal/destination/filesystem" "io" @@ -29,6 +30,7 @@ type Destination struct { func New(c config.DestinationConfig) (*Destination, error) { d := Destination{} + if c.Type == filesystem.Type { filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig) if err != nil { @@ -43,7 +45,9 @@ func New(c config.DestinationConfig) (*Destination, error) { slog.Error("error while initializing writer interface for filesystem destination", "error", err) return nil, err } + + return &d, nil } - return &d, nil + return nil, errors.New("destination type not known") } diff --git a/internal/destination/destination_test.go b/internal/destination/destination_test.go new file mode 100644 index 0000000..efe3b1c --- /dev/null +++ b/internal/destination/destination_test.go @@ -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) + } + }) + } +} diff --git a/internal/source/source.go b/internal/source/source.go index 633cecf..381c9da 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -17,6 +17,7 @@ package source import ( + "errors" "github.com/cluetec/lifeboat/internal/config" "github.com/cluetec/lifeboat/internal/source/filesystem" "io" @@ -29,6 +30,7 @@ type Source struct { func New(c config.SourceConfig) (*Source, error) { s := Source{} + if c.Type == filesystem.Type { filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig) if err != nil { @@ -43,7 +45,8 @@ func New(c config.SourceConfig) (*Source, error) { slog.Error("error while initializing reader interface for filesystem source", "error", err) return nil, err } + return &s, nil } - return &s, nil + return nil, errors.New("source type not known") } diff --git a/internal/source/source_test.go b/internal/source/source_test.go new file mode 100644 index 0000000..cf8632e --- /dev/null +++ b/internal/source/source_test.go @@ -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 source + +import ( + "github.com/cluetec/lifeboat/internal/config" + "reflect" + "testing" +) + +func TestNew(t *testing.T) { + type args struct { + c config.SourceConfig + } + tests := []struct { + name string + args args + want *Source + wantErr bool + }{ + { + name: "Throw error if type is not known", + args: args{c: config.SourceConfig{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) + } + }) + } +} From 76afcf1b4fb18244835f0c550e222fca017b902e Mon Sep 17 00:00:00 2001 From: "Florian Rusch (cluetec GmbH)" <110589194+florianrusch-cluetec@users.noreply.github.com> Date: Fri, 10 Nov 2023 12:27:13 +0100 Subject: [PATCH 4/8] refactor: enhance internal handling of multiple systems (#25) --- internal/config/config.go | 6 +++-- internal/destination/destination.go | 28 ++++++++++------------- internal/destination/filesystem/config.go | 11 +++++---- internal/destination/filesystem/writer.go | 17 ++++++++++---- internal/source/filesystem/config.go | 11 +++++---- internal/source/filesystem/reader.go | 16 +++++++++---- internal/source/source.go | 27 ++++++++++------------ 7 files changed, 63 insertions(+), 53 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index ded72f6..bc2c5b8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 { diff --git a/internal/destination/destination.go b/internal/destination/destination.go index b40c87a..91c9faa 100644 --- a/internal/destination/destination.go +++ b/internal/destination/destination.go @@ -30,24 +30,20 @@ type Destination struct { func New(c config.DestinationConfig) (*Destination, error) { d := Destination{} + var err error - if c.Type == filesystem.Type { - filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig) - if err != nil { - slog.Error("error while initializing filesystem destination config", "error", err) - 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 - } + 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 + } - return &d, nil + if d.Writer == nil { + return nil, errors.New("destination type not known") } - return nil, errors.New("destination type not known") + return &d, nil } diff --git a/internal/destination/filesystem/config.go b/internal/destination/filesystem/config.go index 8df1550..e6c5519 100644 --- a/internal/destination/filesystem/config.go +++ b/internal/destination/filesystem/config.go @@ -17,25 +17,26 @@ package filesystem import ( + globalConfig "github.com/cluetec/lifeboat/internal/config" "github.com/mitchellh/mapstructure" "log/slog" ) const Type = "filesystem" -type Config struct { +type config struct { Path string } -func NewConfig(c map[string]any) (*Config, error) { - var filesystemConfig Config +func newConfig(rc *globalConfig.ResourceConfig) (*config, error) { + var c config - err := mapstructure.Decode(c, &filesystemConfig) + err := mapstructure.Decode(rc, &c) if err != nil { slog.Error("unable to decode config into filesystem destination config", "error", err) return nil, err } - return &filesystemConfig, nil + return &c, nil } diff --git a/internal/destination/filesystem/writer.go b/internal/destination/filesystem/writer.go index 17607a1..0cfb4d7 100644 --- a/internal/destination/filesystem/writer.go +++ b/internal/destination/filesystem/writer.go @@ -18,6 +18,7 @@ package filesystem import ( "errors" + globalConfig "github.com/cluetec/lifeboat/internal/config" "log/slog" "os" ) @@ -26,11 +27,17 @@ type Writer struct { file *os.File } -func NewWriter(c *Config) (*Writer, error) { - w := Writer{} +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) + _, err = os.Stat(c.Path) if err == nil { return nil, errors.New("destination file already exists") } else if !errors.Is(err, os.ErrNotExist) { @@ -44,11 +51,11 @@ func NewWriter(c *Config) (*Writer, error) { return nil, err } - w.file = f - return &w, nil + 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) } diff --git a/internal/source/filesystem/config.go b/internal/source/filesystem/config.go index 063ddaa..9354240 100644 --- a/internal/source/filesystem/config.go +++ b/internal/source/filesystem/config.go @@ -17,25 +17,26 @@ package filesystem import ( + globalConfig "github.com/cluetec/lifeboat/internal/config" "github.com/mitchellh/mapstructure" "log/slog" ) const Type = "filesystem" -type Config struct { +type config struct { Path string } -func NewConfig(c map[string]any) (*Config, error) { - var filesystemConfig Config +func newConfig(rc *globalConfig.ResourceConfig) (*config, error) { + var c config - err := mapstructure.Decode(c, &filesystemConfig) + err := mapstructure.Decode(rc, &c) if err != nil { slog.Error("unable to decode config into filesystem source config", "error", err) return nil, err } - return &filesystemConfig, nil + return &c, nil } diff --git a/internal/source/filesystem/reader.go b/internal/source/filesystem/reader.go index bfce89d..7020475 100644 --- a/internal/source/filesystem/reader.go +++ b/internal/source/filesystem/reader.go @@ -17,6 +17,7 @@ package filesystem import ( + globalConfig "github.com/cluetec/lifeboat/internal/config" "log/slog" "os" ) @@ -25,20 +26,25 @@ type Reader struct { file *os.File } -func NewReader(c *Config) (*Reader, error) { - r := Reader{} +func NewReader(rc *globalConfig.ResourceConfig) (*Reader, error) { + c, err := newConfig(rc) + if err != nil { + slog.Error("error while initializing filesystem source config", "error", err) + return nil, err + } + + slog.Debug("filesystem source config loaded", "config", rc) f, err := os.Open(c.Path) if err != nil { return nil, err } - r.file = f - - return &r, nil + return &Reader{file: f}, nil } func (r *Reader) Read(b []byte) (int, error) { + slog.Debug("filesystem source read got called") return r.file.Read(b) } diff --git a/internal/source/source.go b/internal/source/source.go index 381c9da..318050d 100644 --- a/internal/source/source.go +++ b/internal/source/source.go @@ -30,23 +30,20 @@ type Source struct { func New(c config.SourceConfig) (*Source, error) { s := Source{} + var err error - if c.Type == filesystem.Type { - filesystemConfig, err := filesystem.NewConfig(c.ResourceConfig) - if err != nil { - slog.Error("error while initializing filesystem source config", "error", err) - return nil, err - } - - slog.Debug("filesystem source config loaded", "config", filesystemConfig) + switch { + case c.Type == filesystem.Type: + s.Reader, err = filesystem.NewReader(&c.ResourceConfig) + } + if err != nil { + slog.Error("error while initializing reader interface for source system", "sourceType", c.Type, "error", err) + return nil, err + } - 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 + if s.Reader == nil { + return nil, errors.New("source type not known") } - return nil, errors.New("source type not known") + return &s, nil } From f651f753fb1f53ab71411de93b8aecbd58b88880 Mon Sep 17 00:00:00 2001 From: "Florian Rusch (cluetec GmbH)" Date: Mon, 13 Nov 2023 06:58:45 +0100 Subject: [PATCH 5/8] fix: fix typo in greetings message for new contributions --- .github/workflows/greetings.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/greetings.yaml b/.github/workflows/greetings.yaml index 9a43ade..1b34837 100644 --- a/.github/workflows/greetings.yaml +++ b/.github/workflows/greetings.yaml @@ -39,5 +39,5 @@ jobs: - uses: actions/first-interaction@1dbfe1ba5525b8257e1f259b09745bee346d62d8 # v1.2.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)." From 8703f04669938834cd14b3e31dd0d8463cc2867c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 23 Nov 2023 07:34:38 +0100 Subject: [PATCH 6/8] chore(deps): bump step-security/harden-runner from 2.6.0 to 2.6.1 (#31) --- .github/workflows/greetings.yaml | 2 +- .github/workflows/labeler.yaml | 2 +- .github/workflows/markdown-checks.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/greetings.yaml b/.github/workflows/greetings.yaml index 1b34837..c08110c 100644 --- a/.github/workflows/greetings.yaml +++ b/.github/workflows/greetings.yaml @@ -29,7 +29,7 @@ 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 diff --git a/.github/workflows/labeler.yaml b/.github/workflows/labeler.yaml index c4caff5..9d34dcf 100644 --- a/.github/workflows/labeler.yaml +++ b/.github/workflows/labeler.yaml @@ -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 diff --git a/.github/workflows/markdown-checks.yaml b/.github/workflows/markdown-checks.yaml index cf288dd..92f443a 100644 --- a/.github/workflows/markdown-checks.yaml +++ b/.github/workflows/markdown-checks.yaml @@ -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 From 9235f5e5257f0890629e8ff0b6a9b403b3696675 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 30 Nov 2023 09:59:05 +0100 Subject: [PATCH 7/8] chore(deps): bump DavidAnson/markdownlint-cli2-action from 13 to 14 (#32) --- .github/workflows/markdown-checks.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/markdown-checks.yaml b/.github/workflows/markdown-checks.yaml index 92f443a..a722d26 100644 --- a/.github/workflows/markdown-checks.yaml +++ b/.github/workflows/markdown-checks.yaml @@ -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 From fc243c57c7cb466283860574d00313aa64813e5b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 5 Dec 2023 14:32:24 +0100 Subject: [PATCH 8/8] chore(deps): bump actions/first-interaction from 1.2.0 to 1.3.0 (#33) Bumps [actions/first-interaction](https://github.com/actions/first-interaction) from 1.2.0 to 1.3.0. - [Release notes](https://github.com/actions/first-interaction/releases) - [Commits](https://github.com/actions/first-interaction/compare/1dbfe1ba5525b8257e1f259b09745bee346d62d8...34f15e814fe48ac9312ccf29db4e74fa767cbab7) --- updated-dependencies: - dependency-name: actions/first-interaction dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/greetings.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/greetings.yaml b/.github/workflows/greetings.yaml index c08110c..ac0f9c5 100644 --- a/.github/workflows/greetings.yaml +++ b/.github/workflows/greetings.yaml @@ -36,7 +36,7 @@ jobs: 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: "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)."