-
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.
Given a representation of a file from a source repository, transform it into its destination in a target directory. This includes substituting variables in the file path, in any text file and not-substituting unknown variables. Signed-off-by: Aidan Delaney <adelaney21@bloomberg.net>
- Loading branch information
1 parent
04bc1f3
commit 87f9c12
Showing
9 changed files
with
442 additions
and
43 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
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,90 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
t "github.com/coveooss/gotemplate/v3/template" | ||
) | ||
|
||
type SourceFile struct { | ||
FilePath string | ||
FileContent string | ||
FileMode fs.FileMode | ||
} | ||
|
||
func (s SourceFile) Transform(inputDir string, outputDir string, vars map[string]string) error { | ||
outputFile, err := s.Replace(vars) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dstDir := filepath.Join(outputDir, filepath.Dir(outputFile.FilePath)) | ||
mkdirErr := os.MkdirAll(dstDir, 0744) | ||
if mkdirErr != nil { | ||
return fmt.Errorf("failed to create target directory %s", dstDir) | ||
} | ||
|
||
outputPath := filepath.Join(outputDir, outputFile.FilePath) | ||
if outputFile.FileContent == "" { | ||
inputPath := filepath.Join(inputDir, s.FilePath) | ||
mvErr := os.Rename(inputPath, outputPath) | ||
if mvErr != nil { | ||
return fmt.Errorf("failed to rename %s to %s", s.FilePath, outputFile.FilePath) | ||
} | ||
} else { | ||
os.WriteFile(outputPath, []byte(outputFile.FileContent), outputFile.FileMode|0600) | ||
} | ||
return nil | ||
} | ||
|
||
func replaceUnknownVars(vars map[string]string, content string) string { | ||
regex := regexp.MustCompile(`{{[ \t]*\.\w+`) | ||
transformed := content | ||
for _, token := range regex.FindAllString(content, -1) { | ||
candidate := strings.Split(token, ".")[1] | ||
if _, exists := vars[candidate]; !exists { | ||
// replace "{{\s*.candidate" with "{&{&\s*.candidate" | ||
replacement := strings.Replace(token, "{{", ReplacementDelimiter, 1) | ||
transformed = strings.ReplaceAll(transformed, token, replacement) | ||
} | ||
} | ||
return transformed | ||
} | ||
|
||
func (s SourceFile) Replace(vars map[string]string) (SourceFile, error) { | ||
opts := t.DefaultOptions(). | ||
Set(t.Overwrite, t.Sprig, t.StrictErrorCheck, t.AcceptNoValue). | ||
Unset(t.Razor) | ||
template, err := t.NewTemplate( | ||
"", | ||
vars, | ||
"", | ||
opts) | ||
if err != nil { | ||
return SourceFile{}, err | ||
} | ||
|
||
filePath := replaceUnknownVars(vars, s.FilePath) | ||
transformedFilePath, err := template.ProcessContent(filePath, "") | ||
if err != nil { | ||
return SourceFile{}, err | ||
} | ||
transformedFilePath = strings.ReplaceAll(transformedFilePath, ReplacementDelimiter, "{{") | ||
|
||
transformedFileContent := "" | ||
if s.FileContent != "" { | ||
fileContent := replaceUnknownVars(vars, s.FileContent) | ||
transformedFileContent, err = template.ProcessContent(fileContent, "") | ||
if err != nil { | ||
return SourceFile{}, err | ||
} | ||
transformedFileContent = strings.ReplaceAll(transformedFileContent, ReplacementDelimiter, "{{") | ||
} | ||
|
||
return SourceFile{FilePath: transformedFilePath, FileContent: transformedFileContent, FileMode: s.FileMode}, nil | ||
} |
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,90 @@ | ||
package internal_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
h "github.com/stretchr/testify/assert" | ||
|
||
"github.com/buildpacks/scafall/pkg/internal" | ||
) | ||
|
||
func testReplace(t *testing.T, when spec.G, it spec.S) { | ||
type TestCase struct { | ||
file internal.SourceFile | ||
vars map[string]string | ||
expectedName string | ||
} | ||
|
||
testCases := []TestCase{ | ||
{ | ||
internal.SourceFile{FilePath: "{{.Foo}}", FileContent: ""}, | ||
map[string]string{"Foo": "Bar"}, | ||
"Bar", | ||
}, | ||
{ | ||
internal.SourceFile{FilePath: "{{.Foo}}"}, | ||
map[string]string{"Bar": "Bar"}, | ||
"{{.Foo}}", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
current := testCase | ||
when("variable replacement is called", func() { | ||
it("correctly replaces tokens", func() { | ||
output, err := current.file.Replace(current.vars) | ||
h.Nil(t, err) | ||
h.Equal(t, output.FilePath, current.expectedName) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func testTransform(t *testing.T, when spec.G, it spec.S) { | ||
type TestCase struct { | ||
file internal.SourceFile | ||
vars map[string]string | ||
expectedName string | ||
expectedContent string | ||
} | ||
testCases := []TestCase{ | ||
{ | ||
internal.SourceFile{FilePath: "{{.Foo}}", FileContent: "{{.Foo}}"}, | ||
map[string]string{"Foo": "Bar"}, | ||
"Bar", | ||
"Bar", | ||
}, | ||
{ | ||
internal.SourceFile{FilePath: "{{.Foo}}"}, | ||
map[string]string{"Bar": "Bar"}, | ||
"{{.Foo}}", | ||
"", | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
testCase := testCase | ||
when("variable replacement is called", func() { | ||
var ( | ||
inputDir string | ||
outputDir string | ||
) | ||
it.Before(func() { | ||
inputDir = t.TempDir() | ||
outputDir = t.TempDir() | ||
err := os.WriteFile(filepath.Join(inputDir, testCase.file.FilePath), []byte(testCase.file.FileContent), 0400) | ||
h.Nil(t, err) | ||
}) | ||
|
||
it("correctly replaces tokens", func() { | ||
err := testCase.file.Transform(inputDir, outputDir, testCase.vars) | ||
h.Nil(t, err) | ||
|
||
contents, err := os.ReadFile(filepath.Join(outputDir, testCase.expectedName)) | ||
h.Nil(t, err) | ||
h.Equal(t, string(contents), testCase.expectedContent) | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.