-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable dynamic configuration (#9)
- Loading branch information
Florian Rusch (cluetec GmbH)
authored
Nov 3, 2023
1 parent
c4ec1e0
commit 7f7180c
Showing
11 changed files
with
312 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
source: | ||
type: filesystem | ||
path: source | ||
|
||
destination: | ||
type: filesystem | ||
path: destination | ||
|
||
logLevel: debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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 config | ||
|
||
import ( | ||
"github.com/spf13/viper" | ||
"log/slog" | ||
"strings" | ||
) | ||
|
||
func initViper() error { | ||
viper.AutomaticEnv() | ||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
viper.AddConfigPath(".") | ||
viper.SetConfigFile("./config.yaml") | ||
|
||
if err := viper.ReadInConfig(); err != nil { | ||
slog.Error("error while reading in the configs: %w", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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" | ||
"github.com/cluetec/lifeboat/internal/destination/filesystem" | ||
"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) | ||
} | ||
|
||
slog.Debug("filesystem destination config loaded", "config", filesystemConfig) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 ( | ||
"github.com/mitchellh/mapstructure" | ||
"log/slog" | ||
) | ||
|
||
const Type = "filesystem" | ||
|
||
type Config struct { | ||
Path string | ||
} | ||
|
||
func New(c map[string]any) (*Config, error) { | ||
var filesystemConfig Config | ||
|
||
err := mapstructure.Decode(c, &filesystemConfig) | ||
|
||
if err != nil { | ||
slog.Error("unable to decode config into filesystem destination config", "error", err) | ||
return nil, err | ||
} | ||
|
||
return &filesystemConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 logging | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func InitSlog(logLevel slog.Level) { | ||
lvl := new(slog.LevelVar) | ||
lvl.Set(logLevel) | ||
|
||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: lvl, | ||
})) | ||
|
||
slog.SetDefault(logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 ( | ||
"github.com/mitchellh/mapstructure" | ||
"log/slog" | ||
) | ||
|
||
const Type = "filesystem" | ||
|
||
type Config struct { | ||
Path string | ||
} | ||
|
||
func New(c map[string]any) (*Config, error) { | ||
var filesystemConfig Config | ||
|
||
err := mapstructure.Decode(c, &filesystemConfig) | ||
|
||
if err != nil { | ||
slog.Error("unable to decode config into filesystem source config", "error", err) | ||
return nil, err | ||
} | ||
|
||
return &filesystemConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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" | ||
"github.com/cluetec/lifeboat/internal/source/filesystem" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func Prepare(c config.SourceConfig) { | ||
if c.Type == filesystem.Type { | ||
filesystemConfig, err := filesystem.New(c.ResourceConfig) | ||
if err != nil { | ||
slog.Error("error while initializing filesystem source config", "error", err) | ||
os.Exit(1) | ||
} | ||
|
||
slog.Debug("filesystem source config loaded", "config", filesystemConfig) | ||
} | ||
} |