From 7edbc988aebe4d3e7809d9520aa05db88b75ff7b Mon Sep 17 00:00:00 2001 From: Fabrizio Sestito Date: Thu, 20 Oct 2022 14:36:18 +0200 Subject: [PATCH] Refactor profile file parsing with envparse --- internal/sapsystem/sapsystem.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/internal/sapsystem/sapsystem.go b/internal/sapsystem/sapsystem.go index d919b509..d639e376 100644 --- a/internal/sapsystem/sapsystem.go +++ b/internal/sapsystem/sapsystem.go @@ -10,6 +10,7 @@ import ( "regexp" "strings" + envparse "github.com/hashicorp/go-envparse" "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/spf13/afero" @@ -53,9 +54,7 @@ type SAPSystem struct { DBAddress string } -// The value is interface{} as some of the entries in the SAP profiles files and commands -// are already using "/", so the result will be a map of strings/maps -type SAPProfile map[string]interface{} +type SAPProfile map[string]string type DatabaseData struct { Database string @@ -233,7 +232,7 @@ func getProfilePath(sysPath string) string { } // Get SAP profile file content -func getProfileData(fs afero.Fs, profilePath string) (map[string]interface{}, error) { +func getProfileData(fs afero.Fs, profilePath string) (map[string]string, error) { profile, err := fs.Open(profilePath) if err != nil { return nil, errors.Wrap(err, "could not open profile file") @@ -241,14 +240,11 @@ func getProfileData(fs afero.Fs, profilePath string) (map[string]interface{}, er defer profile.Close() - profileRaw, err := io.ReadAll(profile) - + configMap, err := envparse.Parse(profile) if err != nil { - return nil, errors.Wrap(err, "could not read profile file") + return nil, errors.Wrap(err, "could not parse profile file") } - configMap := utils.FindMatches(`([\w\/]+)\s=\s(.+)`, profileRaw) - return configMap, nil } @@ -258,7 +254,7 @@ func getDBAddress(system *SAPSystem) (string, error) { return "", fmt.Errorf("SAPDBHOST field not found in the SAP profile") } - addrList, err := net.LookupIP(sapdbhost.(string)) + addrList, err := net.LookupIP(sapdbhost) if err != nil { return "", fmt.Errorf("could not resolve \"%s\" hostname", sapdbhost) }