Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #833 from laverya/template-noproxy-github-assets
Browse files Browse the repository at this point in the history
render templates in noproxy github assets
  • Loading branch information
laverya authored Feb 19, 2019
2 parents 57fe3a3 + 735207e commit 871abc3
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 0 deletions.
10 changes: 10 additions & 0 deletions integration/init/github-template-funcs/expected/.ship/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"v1": {
"config": {
"option": "abc123"
},
"releaseName": "ship",
"upstream": "https://github.com/replicatedhq/test-charts/tree/fd6c6c83ff38d272311b0ed00d6e579ba8b3579e/template-functions-noproxy",
"contentSHA": "a28253e6e1fa4a1768c62f11be8fbc97eec2abe1d8ed77c19faaee4cd0a386d7"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#This file tests a part of the Config suite of template functions in Ship

Config option: abc123
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#This file tests a part of the Integration suite of template functions in Ship

Release semver:

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#This file tests a part of the Static suite of template functions in Ship

TwoPlusTwo: 4
UPPERCASE: UPPERCASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#This file tests a part of the Config suite of template functions in Ship

Config option: abc123
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#This file tests a part of the Integration suite of template functions in Ship

Release semver:

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#This file tests a part of the Static suite of template functions in Ship

TwoPlusTwo: 4
UPPERCASE: UPPERCASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#This file tests a part of the Config suite of template functions in Ship

Config option: abc123
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#This file tests a part of the Integration suite of template functions in Ship

Release semver:

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#This file tests a part of the Static suite of template functions in Ship

TwoPlusTwo: 4
UPPERCASE: UPPERCASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "installing nothing"
echo "semver: "
3 changes: 3 additions & 0 deletions integration/init/github-template-funcs/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
upstream: "https://github.com/replicatedhq/test-charts/tree/fd6c6c83ff38d272311b0ed00d6e579ba8b3579e/template-functions-noproxy"
args: ["--prefer-git"]
skip_cleanup: false
5 changes: 5 additions & 0 deletions pkg/lifecycle/render/github/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/replicatedhq/ship/pkg/specs/gogetter"
"github.com/replicatedhq/ship/pkg/state"
"github.com/replicatedhq/ship/pkg/templates"

"github.com/spf13/afero"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -232,6 +233,10 @@ func (r *LocalRenderer) resolveNoProxyGithubAssets(asset api.GitHubAsset, builde
return errors.Wrap(err, "remove tmp github asset")
}

if err := templates.BuildDir(dest, &r.Fs, builder); err != nil {
return errors.Wrapf(err, "render templates in github asset %s", dest)
}

return nil
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/templates/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package templates

import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/afero"
)

func BuildDir(buildPath string, fs *afero.Afero, builder *Builder) error {
isDir, err := fs.IsDir(buildPath)
if err != nil {
return errors.Wrapf(err, "check if dir %s", buildPath)
}
if !isDir {
return buildFile(buildPath, fs, builder)
}

files, err := fs.ReadDir(buildPath)
if err != nil {
return errors.Wrapf(err, "read dir %s", buildPath)
}
for _, file := range files {
childPath := filepath.Join(buildPath, file.Name())
if file.IsDir() {
err = BuildDir(childPath, fs, builder)
if err != nil {
return errors.Wrapf(err, "build dir %s", childPath)
}
} else {
err = buildFile(childPath, fs, builder)
if err != nil {
return errors.Wrapf(err, "build file %s", childPath)
}
}
}

return nil
}

func buildFile(buildPath string, fs *afero.Afero, builder *Builder) error {
fileContents, err := fs.ReadFile(buildPath)
if err != nil {
return errors.Wrapf(err, "read file %s", buildPath)
}

newContents, err := builder.String(string(fileContents))
if err != nil {
return errors.Wrapf(err, "template file %s", buildPath)
}

err = fs.WriteFile(buildPath, []byte(newContents), os.FileMode(777))
if err != nil {
return errors.Wrapf(err, "write file %s", buildPath)
}
return nil
}
144 changes: 144 additions & 0 deletions pkg/templates/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package templates

import (
"os"
"testing"

"github.com/replicatedhq/ship/pkg/testing/logger"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

func TestBuildDir(t *testing.T) {
type file struct {
contents string
path string
}

tests := []struct {
name string
buildPath string
inputFiles []file
outputFiles []file
}{
{
name: "no templates",
buildPath: "dir",
inputFiles: []file{
{
contents: "notATemplate",
path: "dir/file.txt",
},
},
outputFiles: []file{
{
contents: "notATemplate",
path: "dir/file.txt",
},
},
},
{
name: "template not in dir",
buildPath: "dir",
inputFiles: []file{
{
contents: "notATemplate",
path: "dir/file.txt",
},
{
contents: `{{repl ConfigOption "option_1"}}`,
path: "notDir/template.txt",
},
},
outputFiles: []file{
{
contents: "notATemplate",
path: "dir/file.txt",
},
{
contents: `{{repl ConfigOption "option_1"}}`,
path: "notDir/template.txt",
},
},
},
{
name: "template in dir",
buildPath: "dir",
inputFiles: []file{
{
contents: "notATemplate",
path: "dir/file.txt",
},
{
contents: `{{repl ConfigOption "option_1"}}`,
path: "dir/template.txt",
},
},
outputFiles: []file{
{
contents: "notATemplate",
path: "dir/file.txt",
},
{
contents: "Option 1",
path: "dir/template.txt",
},
},
},
{
name: "template in subdir",
buildPath: "anotherdir",
inputFiles: []file{
{
contents: "notATemplate",
path: "anotherdir/file.txt",
},
{
contents: `{{repl ConfigOption "option_2"}}`,
path: "anotherdir/subdir/template.txt",
},
},
outputFiles: []file{
{
contents: "notATemplate",
path: "anotherdir/file.txt",
},
{
contents: "Option 2",
path: "anotherdir/subdir/template.txt",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := require.New(t)

fs := afero.Afero{Fs: afero.NewMemMapFs()}

for _, file := range tt.inputFiles {
req.NoError(fs.WriteFile(file.path, []byte(file.contents), os.FileMode(777)))
}

builderBuilder := &BuilderBuilder{
Logger: &logger.TestLogger{T: t},
Viper: viper.New(),
}

builder := builderBuilder.NewBuilder(
builderBuilder.NewStaticContext(),
testContext{},
)

err := BuildDir(tt.buildPath, &fs, &builder)
req.NoError(err)

for _, file := range tt.outputFiles {
actualContents, err := fs.ReadFile(file.path)
req.NoError(err)
req.Equal(file.contents, string(actualContents))
}
})
}
}

0 comments on commit 871abc3

Please sign in to comment.