-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from gowizzard/development
fix: Add new private function for read all matches from file.
- Loading branch information
Showing
2 changed files
with
50 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2022 Jonas Kwiedor. All rights reserved. | ||
// Use of this source code is governed by the MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package dotenv | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"regexp" | ||
) | ||
|
||
// regex is to save the compiled expression. | ||
var regex = regexp.MustCompile(`(?m)^(?P<key>\w+?)=(?:["']|\b)(?P<value>.+?)(?:["']|\b)$`) | ||
|
||
// read is to read the environment variable file and use regex to find all sub matches. | ||
func read(path string) (map[string]string, error) { | ||
|
||
read, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(read) == 0 { | ||
return nil, errors.New("file is empty") | ||
} | ||
|
||
matches := regex.FindAllSubmatch(read, -1) | ||
if matches == nil { | ||
return nil, errors.New("no matches found") | ||
} | ||
|
||
var environ = make(map[string]string) | ||
for _, value := range matches { | ||
|
||
index := map[string]int{ | ||
"key": regex.SubexpIndex("key"), | ||
"value": regex.SubexpIndex("value"), | ||
} | ||
|
||
environ[string(value[index["key"]])] = string(value[index["value"]]) | ||
|
||
} | ||
|
||
return environ, nil | ||
|
||
} |