-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
122 additions
and
110 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 was deleted.
Oops, something went wrong.
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
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,60 @@ | ||
package githubactions | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
valueDelimiter = `ghadelimiter_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}` | ||
valueDelimiterR = regexp.MustCompile(valueDelimiter) | ||
|
||
keyDelimiter = `(\w+)<<` + valueDelimiter | ||
keyDelimiterR = regexp.MustCompile(keyDelimiter) | ||
) | ||
|
||
func ParseEnvFile(r io.Reader) (map[string]string, error) { | ||
var ( | ||
values = make(map[string]string) | ||
scanner = bufio.NewScanner(r) | ||
) | ||
for scanner.Scan() { | ||
line0 := scanner.Text() | ||
if strings.HasPrefix(line0, "# ") || strings.TrimSpace(line0) == "" { | ||
continue | ||
} else if matches := keyDelimiterR.FindStringSubmatch(line0); len(matches) == 2 { | ||
fmt.Println(line0, matches) | ||
if scanner.Scan() { | ||
line1 := scanner.Text() | ||
if scanner.Scan() { | ||
line2 := scanner.Text() | ||
if valueDelimiterR.MatchString(line2) { | ||
values[matches[1]] = strings.SplitN(line1, " #", 2)[0] | ||
continue | ||
} | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("invalid multiline environment file entry") | ||
} else if matches := strings.SplitN(line0, "=", 2); len(matches) == 2 { | ||
values[matches[0]] = trimValue(matches[1]) | ||
} else { | ||
return nil, fmt.Errorf("parse environment file line: %s", line0) | ||
} | ||
} | ||
|
||
return values, scanner.Err() | ||
} | ||
|
||
func trimValue(value string) string { | ||
if strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) { | ||
return strings.Trim(value, `"`) | ||
} else if strings.HasPrefix(value, `'`) && strings.HasSuffix(value, `'`) { | ||
return strings.Trim(value, `'`) | ||
} | ||
|
||
return value | ||
} |
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,45 @@ | ||
package githubactions_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/frantjc/forge/githubactions" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func TestParseEnvFile(t *testing.T) { | ||
var ( | ||
env = `# comment | ||
HELLO=there | ||
GENERAL<<ghadelimiter_` + uuid.NewString() + ` | ||
kenobi # comment | ||
ghadelimiter_` + uuid.NewString() + ` | ||
YOU="are a" | ||
BOLD<<ghadelimiter_` + uuid.NewString() + ` | ||
one | ||
ghadelimiter_` + uuid.NewString() + ` | ||
` | ||
expected = map[string]string{ | ||
"HELLO": "there", | ||
"GENERAL": "kenobi", | ||
"YOU": "are a", | ||
"BOLD": "one", | ||
} | ||
actual, err = githubactions.ParseEnvFile(bytes.NewBufferString(env)) | ||
) | ||
if err != nil { | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
|
||
for k, v := range actual { | ||
if expected[k] != v { | ||
t.Error("actual", v, "for key", k, "does not match expected", expected[k]) | ||
t.FailNow() | ||
} | ||
} | ||
} |
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
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
Binary file not shown.
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