-
Notifications
You must be signed in to change notification settings - Fork 993
Add standard newline/quoting behavior to dotenv store #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,311 @@ | ||
| package dotenv | ||
|
|
||
scjudd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // The dotenv parser is designed around the following rules: | ||
| // | ||
| // Comments: | ||
| // | ||
| // * Comments may be written by starting a line with the `#` character. | ||
| // End-of-line comments are not currently supported, as there is no way to | ||
| // encode a comment's position in a `sops.TreeItem`. | ||
| // | ||
| // Newline handling: | ||
| // | ||
| // * If a value is unquoted or single-quoted and contains the character | ||
| // sequence `\n` (`0x5c6e`), it IS NOT decoded to a line feed (`0x0a`). | ||
| // | ||
| // * If a value is double-quoted and contains the character sequence `\n` | ||
| // (`0x5c6e`), it IS decoded to a line feed (`0x0a`). | ||
| // | ||
| // Whitespace trimming: | ||
| // | ||
| // * For comments, the whitespace immediately after the `#` character and any | ||
| // trailing whitespace is trimmed. | ||
| // | ||
| // * If a value is unquoted and contains any leading or trailing whitespace, it | ||
| // is trimmed. | ||
| // | ||
| // * If a value is either single- or double-quoted and contains any leading or | ||
| // trailing whitespace, it is left untrimmed. | ||
| // | ||
| // Quotation handling: | ||
| // | ||
| // * If a value is surrounded by single- or double-quotes, the quotation marks | ||
| // are interpreted and not included in the value. | ||
| // | ||
| // * Any number of single-quote characters may appear in a double-quoted | ||
| // value, or within a single-quoted value if they are escaped (i.e., | ||
| // `'foo\'bar'`). | ||
| // | ||
| // * Any number of double-quote characters may appear in a single-quoted | ||
| // value, or within a double-quoted value if they are escaped (i.e., | ||
| // `"foo\"bar"`). | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "go.mozilla.org/sops/v3" | ||
| ) | ||
|
|
||
| var KeyRegexp = regexp.MustCompile(`^[A-Za-z_]+[A-Za-z0-9_]*$`) | ||
|
|
||
| func parse(data []byte) (items []sops.TreeItem, err error) { | ||
| reader := bytes.NewReader(data) | ||
|
|
||
| for { | ||
| var b byte | ||
| var item *sops.TreeItem | ||
|
|
||
| b, err = reader.ReadByte() | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| if isWhitespace(b) { | ||
| continue | ||
| } | ||
|
|
||
| if b == '#' { | ||
| item, err = parseComment(reader) | ||
| } else { | ||
| reader.UnreadByte() | ||
| item, err = parseKeyValue(reader) | ||
| } | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| if item == nil { | ||
| continue | ||
| } | ||
|
|
||
| items = append(items, *item) | ||
| } | ||
|
|
||
| if err == io.EOF { | ||
| err = nil | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func parseComment(reader io.ByteScanner) (item *sops.TreeItem, err error) { | ||
| var builder strings.Builder | ||
| var whitespace bytes.Buffer | ||
|
|
||
| for { | ||
| var b byte | ||
| b, err = reader.ReadByte() | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| if b == '\n' { | ||
| break | ||
| } | ||
|
|
||
| if isWhitespace(b) { | ||
| whitespace.WriteByte(b) | ||
| continue | ||
| } | ||
|
|
||
| if builder.Len() == 0 { | ||
| whitespace.Reset() | ||
| } | ||
|
|
||
| _, err = io.Copy(&builder, &whitespace) | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| builder.WriteByte(b) | ||
| } | ||
|
|
||
| if builder.Len() == 0 { | ||
| return | ||
| } | ||
|
|
||
| item = &sops.TreeItem{Key: sops.Comment{builder.String()}, Value: nil} | ||
| return | ||
| } | ||
|
|
||
| func parseKeyValue(reader io.ByteScanner) (item *sops.TreeItem, err error) { | ||
| var key, value string | ||
|
|
||
| key, err = parseKey(reader) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| value, err = parseValue(reader) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| item = &sops.TreeItem{Key: key, Value: value} | ||
| return | ||
| } | ||
|
|
||
| func parseKey(reader io.ByteScanner) (key string, err error) { | ||
| var builder strings.Builder | ||
|
|
||
| for { | ||
| var b byte | ||
| b, err = reader.ReadByte() | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| if b == '=' { | ||
| break | ||
| } | ||
|
|
||
| builder.WriteByte(b) | ||
| } | ||
|
|
||
| key = builder.String() | ||
|
|
||
| if !KeyRegexp.MatchString(key) { | ||
| err = fmt.Errorf("invalid dotenv key: %q", key) | ||
scjudd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func parseValue(reader io.ByteScanner) (value string, err error) { | ||
| var first byte | ||
| first, err = reader.ReadByte() | ||
|
|
||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| if first == '\'' { | ||
| return parseSingleQuoted(reader) | ||
| } | ||
|
|
||
| if first == '"' { | ||
| return parseDoubleQuoted(reader) | ||
| } | ||
|
|
||
| reader.UnreadByte() | ||
| return parseUnquoted(reader) | ||
| } | ||
|
|
||
| func parseSingleQuoted(reader io.ByteScanner) (value string, err error) { | ||
| var builder strings.Builder | ||
| escaping := false | ||
|
|
||
| for { | ||
| var b byte | ||
| b, err = reader.ReadByte() | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| if !escaping && b == '\'' { | ||
| break | ||
| } | ||
|
|
||
| if !escaping && b == '\\' { | ||
| escaping = true | ||
| continue | ||
| } | ||
|
|
||
| if escaping && b != '\'' { | ||
| builder.WriteByte('\\') | ||
| } | ||
|
|
||
| escaping = false | ||
| builder.WriteByte(b) | ||
| } | ||
|
|
||
| value = builder.String() | ||
| return | ||
| } | ||
|
|
||
| func parseDoubleQuoted(reader io.ByteScanner) (value string, err error) { | ||
| var builder strings.Builder | ||
| escaping := false | ||
|
|
||
| for { | ||
| var b byte | ||
| b, err = reader.ReadByte() | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| if !escaping && b == '"' { | ||
| break | ||
| } | ||
|
|
||
| if !escaping && b == '\\' { | ||
| escaping = true | ||
| continue | ||
| } | ||
|
|
||
| if escaping && b == 'n' { | ||
| b = '\n' | ||
| } else if escaping && b != '"' { | ||
| builder.WriteByte('\\') | ||
| } | ||
|
|
||
| escaping = false | ||
| builder.WriteByte(b) | ||
| } | ||
|
|
||
| value = builder.String() | ||
| return | ||
| } | ||
|
|
||
| func parseUnquoted(reader io.ByteScanner) (value string, err error) { | ||
| var builder strings.Builder | ||
| var whitespace bytes.Buffer | ||
|
|
||
| for { | ||
| var b byte | ||
| b, err = reader.ReadByte() | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| if b == '\n' { | ||
| break | ||
| } | ||
|
|
||
| if isWhitespace(b) { | ||
| whitespace.WriteByte(b) | ||
| continue | ||
| } | ||
|
|
||
| if builder.Len() == 0 { | ||
| whitespace.Reset() | ||
| } | ||
|
|
||
| _, err = io.Copy(&builder, &whitespace) | ||
|
|
||
| if err != nil { | ||
| break | ||
| } | ||
|
|
||
| builder.WriteByte(b) | ||
| } | ||
|
|
||
| value = builder.String() | ||
| return | ||
| } | ||
|
|
||
| func isWhitespace(b byte) bool { | ||
| return b == ' ' || b == '\t' || b == '\r' || b == '\n' | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.