Try to load the config YAML from CWD first

This commit is contained in:
M. Mert Yildiran 2023-02-05 22:36:40 +03:00
parent eee641082e
commit e62ebea8d8
No known key found for this signature in database
GPG Key ID: DA5D6DCBB758A461
2 changed files with 22 additions and 6 deletions

3
.gitignore vendored
View File

@ -61,3 +61,6 @@ bin
# Scripts # Scripts
scripts/ scripts/
# CWD config YAML
kubeshark.yaml

View File

@ -5,11 +5,13 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"github.com/creasty/defaults" "github.com/creasty/defaults"
"github.com/kubeshark/kubeshark/misc"
"github.com/kubeshark/kubeshark/misc/version" "github.com/kubeshark/kubeshark/misc/version"
"github.com/kubeshark/kubeshark/utils" "github.com/kubeshark/kubeshark/utils"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -100,14 +102,25 @@ func WriteConfig(config *ConfigStruct) error {
} }
func loadConfigFile(configFilePath string, config *ConfigStruct) error { func loadConfigFile(configFilePath string, config *ConfigStruct) error {
reader, openErr := os.Open(configFilePath) cwd, err := os.Getwd()
if openErr != nil { if err != nil {
return openErr return err
} }
buf, readErr := io.ReadAll(reader) cwdConfig := filepath.Join(cwd, fmt.Sprintf("%s.yaml", misc.Program))
if readErr != nil { reader, err := os.Open(cwdConfig)
return readErr if err != nil {
reader, err = os.Open(configFilePath)
if err != nil {
return err
}
} else {
configFilePath = cwdConfig
}
buf, err := io.ReadAll(reader)
if err != nil {
return err
} }
if err := yaml.Unmarshal(buf, config); err != nil { if err := yaml.Unmarshal(buf, config); err != nil {