🎨 Use os.readfile instead of ioutil

This commit is contained in:
Florian Beisel 2024-01-21 14:58:27 +01:00
parent c51a17993e
commit a871f165b7
Signed by: florian
GPG Key ID: 79ECA2E54996FF4D
1 changed files with 17 additions and 4 deletions

View File

@ -1,10 +1,23 @@
// Copyright 2024 Florian Beisel
//
// 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 package config
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"flag" "flag"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"strings" "strings"
@ -45,7 +58,7 @@ func LoadConfig() error {
loadConfigFromEnvOrSecrets(GlobalConfig) loadConfigFromEnvOrSecrets(GlobalConfig)
// Check if the configuration has been loaded successfully // Check if the configuration has been loaded successfully
if GlobalConfig.JwtKey == "" || GlobalConfig.DatabaseFile == "" { if GlobalConfig.DatabaseFile == "" {
// Add more checks as necessary for other required config fields // Add more checks as necessary for other required config fields
return errors.New("failed to load configuration from any source") return errors.New("failed to load configuration from any source")
} }
@ -54,7 +67,7 @@ func LoadConfig() error {
} }
func loadConfigFromFile(filePath string, config *AppConfig) error { func loadConfigFromFile(filePath string, config *AppConfig) error {
fileData, err := ioutil.ReadFile(filePath) fileData, err := os.ReadFile(filePath)
if err != nil { if err != nil {
return err return err
} }
@ -81,7 +94,7 @@ func loadConfigFromEnvOrSecrets(config *AppConfig) {
// Handling Docker secrets (file-based secrets) // Handling Docker secrets (file-based secrets)
secretFileEnvVar := envVar + "_FILE" secretFileEnvVar := envVar + "_FILE"
if secretFilePath, exists := os.LookupEnv(secretFileEnvVar); exists { if secretFilePath, exists := os.LookupEnv(secretFileEnvVar); exists {
if secretValue, err := ioutil.ReadFile(secretFilePath); err == nil { if secretValue, err := os.ReadFile(secretFilePath); err == nil {
val.Field(i).SetString(string(secretValue)) val.Field(i).SetString(string(secretValue))
} }
} }