Skip to content

Commit

Permalink
Refactor profile file parsing with envparse
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosestito committed Oct 20, 2022
1 parent f6e8d35 commit 7edbc98
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions internal/sapsystem/sapsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -233,22 +232,19 @@ 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")
}

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
}

Expand All @@ -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)
}
Expand Down

0 comments on commit 7edbc98

Please sign in to comment.