2021-07-17 14:38:59 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2024-10-09 20:46:23 +02:00
|
|
|
"bytes"
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2021-07-17 14:38:59 +02:00
|
|
|
"encoding/json"
|
2024-10-09 20:46:23 +02:00
|
|
|
"fmt"
|
2021-07-17 14:38:59 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-10-09 20:46:23 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
2023-09-22 10:25:04 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-07-17 14:38:59 +02:00
|
|
|
)
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
// WriteJsonWithRestrictedPermission writes JSON config object to a file. Enforces permission on the parent directory
|
|
|
|
func WriteJsonWithRestrictedPermission(file string, obj interface{}) error {
|
2023-02-28 17:01:38 +01:00
|
|
|
configDir, configFileName, err := prepareConfigFileDir(file)
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
err = EnforcePermission(file)
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
return writeJson(file, obj, configDir, configFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteJson writes JSON config object to a file creating parent directories if required
|
|
|
|
// The output JSON is pretty-formatted
|
|
|
|
func WriteJson(file string, obj interface{}) error {
|
|
|
|
configDir, configFileName, err := prepareConfigFileDir(file)
|
2022-02-20 19:03:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
return writeJson(file, obj, configDir, configFileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DirectWriteJson writes JSON config object to a file creating parent directories if required without creating a temporary file
|
|
|
|
func DirectWriteJson(ctx context.Context, file string, obj interface{}) error {
|
|
|
|
|
|
|
|
_, _, err := prepareConfigFileDir(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetFile, err := openOrCreateFile(file)
|
2022-02-20 19:03:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2024-09-16 22:42:37 +02:00
|
|
|
err = targetFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to close file %s: %v", file, err)
|
2022-02-20 19:03:16 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
// make it pretty
|
|
|
|
bs, err := json.MarshalIndent(obj, "", " ")
|
2022-02-20 19:03:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
err = targetFile.Truncate(0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = targetFile.Write(bs)
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
func writeJson(file string, obj interface{}, configDir string, configFileName string) error {
|
2023-09-22 10:25:04 +02:00
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
// make it pretty
|
|
|
|
bs, err := json.MarshalIndent(obj, "", " ")
|
2023-09-22 10:25:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
tempFile, err := os.CreateTemp(configDir, ".*"+configFileName)
|
2023-09-22 10:25:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
tempFileName := tempFile.Name()
|
|
|
|
// closing file ops as windows doesn't allow to move it
|
|
|
|
err = tempFile.Close()
|
2023-09-22 10:25:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
defer func() {
|
|
|
|
_, err = os.Stat(tempFileName)
|
|
|
|
if err == nil {
|
|
|
|
os.Remove(tempFileName)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = os.WriteFile(tempFileName, bs, 0600)
|
2023-09-22 10:25:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-16 22:42:37 +02:00
|
|
|
err = os.Rename(tempFileName, file)
|
2023-09-22 10:25:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openOrCreateFile(file string) (*os.File, error) {
|
|
|
|
s, err := os.Stat(file)
|
|
|
|
if err == nil {
|
|
|
|
return os.OpenFile(file, os.O_WRONLY, s.Mode())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetFile, err := os.Create(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//no:lint
|
|
|
|
err = targetFile.Chmod(0640)
|
|
|
|
if err != nil {
|
|
|
|
_ = targetFile.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return targetFile, nil
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
// ReadJson reads JSON config file and maps to a provided interface
|
|
|
|
func ReadJson(file string, res interface{}) (interface{}, error) {
|
|
|
|
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2022-10-22 15:06:54 +02:00
|
|
|
bs, err := io.ReadAll(f)
|
2021-07-17 14:38:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(bs, &res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-10-09 20:46:23 +02:00
|
|
|
// ReadJsonWithEnvSub reads JSON config file and maps to a provided interface with environment variable substitution
|
|
|
|
func ReadJsonWithEnvSub(file string, res interface{}) (interface{}, error) {
|
|
|
|
envVars := getEnvMap()
|
|
|
|
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
bs, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.New("").Parse(string(bs))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
// Execute the template, substituting environment variables
|
|
|
|
err = t.Execute(&output, envVars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error executing template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(output.Bytes(), &res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed parsing Json file after template was executed, err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getEnvMap Convert the output of os.Environ() to a map
|
|
|
|
func getEnvMap() map[string]string {
|
|
|
|
envMap := make(map[string]string)
|
|
|
|
|
|
|
|
for _, env := range os.Environ() {
|
|
|
|
parts := strings.SplitN(env, "=", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
envMap[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return envMap
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:38:59 +02:00
|
|
|
// CopyFileContents copies contents of the given src file to the dst file
|
|
|
|
func CopyFileContents(src, dst string) (err error) {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
cErr := out.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if _, err = io.Copy(out, in); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = out.Sync()
|
|
|
|
return
|
|
|
|
}
|
2023-02-28 17:01:38 +01:00
|
|
|
|
|
|
|
func prepareConfigFileDir(file string) (string, string, error) {
|
|
|
|
configDir, configFileName := filepath.Split(file)
|
|
|
|
if configDir == "" {
|
|
|
|
return filepath.Dir(file), configFileName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.MkdirAll(configDir, 0750)
|
2024-09-16 22:42:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2023-02-28 17:01:38 +01:00
|
|
|
return configDir, configFileName, err
|
|
|
|
}
|