Skip to content

Commit

Permalink
imporve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yassinebenaid committed Jul 4, 2023
1 parent 7dd5648 commit 8a73f2a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
8 changes: 2 additions & 6 deletions expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package goenv
import (
"os"
"regexp"
"strings"
)

func expand(env map[string]string) {
Expand All @@ -19,10 +18,7 @@ func evaluate(env map[string]string, key string, value string) string {
return value
}

// we trim the quotes in case of : "...${key}..."
value = strings.Trim(value, "\"")

// we expand each variable at once
// we replace variables with their values
return os.Expand(value, func(s string) string {
// in case of key1="somthing ${key1} something"
// we return to avoid infinit loop
Expand All @@ -49,7 +45,7 @@ func evaluate(env map[string]string, key string, value string) string {
}

func expectExpanding(value string) bool {
rx := regexp.MustCompile("^(\")?[^\"]*[$]{[^}]+}[^\"]*(\")?$")
rx := regexp.MustCompile("[$]({)?[A-z_0-9]+}?")

return rx.MatchString(value)
}
10 changes: 7 additions & 3 deletions goenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ func Read(files ...string) (map[string]string, error) {
}

// read envirement variables from s and return them as map
func Unmarshal(s string) map[string]string {
envs := buildKeyValuePairs(s)
func Unmarshal(s string) (map[string]string, error) {
envs, err := buildKeyValuePairs(s)

if err != nil {
return nil, err
}

expand(envs)

return envs
return envs, nil
}
15 changes: 7 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

// parse one or more files , if files is empty we default to .env
// parse one or more files , if files not provided is empty we default to .env
func parseOrDefault(files ...string) (map[string]string, error) {

// if no files provided we default to .env
Expand Down Expand Up @@ -69,7 +69,7 @@ func parseFile(file string) (map[string]string, error) {
return nil, err
}

return buildKeyValuePairs(content), nil
return buildKeyValuePairs(content)

}

Expand All @@ -92,8 +92,7 @@ func getFileContent(file string) (string, error) {
return string(content), nil
}

// build the map[key]value structure based on the given string
func buildKeyValuePairs(s string) map[string]string {
func buildKeyValuePairs(s string) (map[string]string, error) {
lines := strings.Split(s, "\n")
pairs := make(map[string]string)

Expand All @@ -102,28 +101,28 @@ func buildKeyValuePairs(s string) map[string]string {
line = strings.TrimSpace(line)

if !isValidPair(line) {
continue
return nil, errors.New("invalid key value pairs : [" + line + "]")
}

key, value := splitKeyValue(line)
pairs[key] = strings.TrimSpace(strings.Trim(value, "\""))
}

return pairs
return pairs, nil
}

// split l into a key value pairs, excluding any comments or invalid pairs
func splitKeyValue(l string) (key string, value string) {

pair := strings.SplitN(l, "=", 2)

key = strings.TrimSpace(pair[0])

// exclude comments
value = strings.TrimSpace(strings.Split(pair[1], "#")[0])

return key, value
}

// check if it looks like key=value
func isValidPair(l string) bool {
rx := regexp.MustCompile(`^(\s)*[A-z_0-9]+(\s)*=(\s)*[^\n]+(\s*)(#[^\n]*)*$`)

Expand Down
4 changes: 2 additions & 2 deletions throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
)

const MAX_ALLOWED_CALLS = 1000
const max_allowed_reads = 1000

var stack = make(map[string]int)

Expand All @@ -15,7 +15,7 @@ func throttle(key1 string, key2 string) {

calls := stack[key1+key2]

if calls > MAX_ALLOWED_CALLS {
if calls > max_allowed_reads {
log.Panic("recursion detected : trying to read " + key2 + " by " + key1)
}

Expand Down

0 comments on commit 8a73f2a

Please sign in to comment.