-
Notifications
You must be signed in to change notification settings - Fork 234
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
Gabriel Simmer
authored
Jul 30, 2020
1 parent
8db507d
commit e08af42
Showing
3 changed files
with
40 additions
and
19 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
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,34 @@ | ||
package process | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// MaybeIncludeFile replaces intsances of <<include(file)>> with the contents | ||
// of "file", escaping instances of "<<" within the file before returning, when | ||
// the <<include()>> parameter is the string passed. | ||
func MaybeIncludeFile(s string, orbDirectory string) (string, error) { | ||
// View: https://regexr.com/582gb | ||
includeRegex, err := regexp.Compile(`(?U)^<<\s*include\((.*\/*[^\/]+)\)\s*?>>$`) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
includeMatches := includeRegex.FindStringSubmatch(s) | ||
if len(includeMatches) > 0 { | ||
filepath := filepath.Join(orbDirectory, includeMatches[1]) | ||
file, err := ioutil.ReadFile(filepath) | ||
if err != nil { | ||
return "", fmt.Errorf("could not open %s for inclusion", filepath) | ||
} | ||
escaped := strings.ReplaceAll(string(file), "<<", "\\<<") | ||
|
||
return escaped, nil | ||
} | ||
|
||
return s, nil | ||
} |
e08af42
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this broke me :(
I was moving my scripts to files and going to do the env vars later, but this now escapes actual params.