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 #551 from laverya/add-filepath-trim-option-to-gith…
Browse files Browse the repository at this point in the history
…ub-asset

Add filepath trim option to GitHub asset
  • Loading branch information
laverya authored Sep 6, 2018
2 parents cc6f0fb + c185e4d commit 2200e0d
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 10 deletions.
17 changes: 13 additions & 4 deletions hack/docs/mutations.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@
"path": "properties.assets.properties.v1.items.properties.helm.properties.github.properties",
"delete": [
"dest",
"mode"
"mode",
"strip_path"
]
},
{
Expand Down Expand Up @@ -352,15 +353,17 @@
"ref": "8fcaebe55af67fe6789fa678faaa76fa867fbc",
"path": "k8s-yamls/",
"dest": "./k8s/",
"source": "private"
"source": "private",
"strip_path": ""
},
{
"repo": "github.com/replicatedhq/ship",
"ref": "master",
"path": "hack/docs/",
"dest": "./docs/",
"dest": "./docs{{repl Add 1 1}}/",
"source": "public",
"mode": 644
"mode": 644,
"strip_path": "{{repl ParseBool \"true\"}}"
}
]
},
Expand Down Expand Up @@ -409,6 +412,12 @@
"description": "One of `public` or `private`, if `private`, access to the repo can be validated on release creation"
}
},
{
"path": "properties.assets.properties.v1.items.properties.github.properties.strip_path",
"merge": {
"description": "If true, the github directory will not be included in the filepath of the generated files. For instance, when outputting all files within 'source/' in the repository to the 'dest/' directory, the file 'source/a/file.txt' would be placed at 'dest/source/a/file.txt' when this is false and 'dest/a/file.txt' when this is true."
}
},
{
"path": "properties.assets.properties.v1.items.properties.github.properties.when",
"merge": {
Expand Down
12 changes: 9 additions & 3 deletions hack/docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,17 @@
"ref": "8fcaebe55af67fe6789fa678faaa76fa867fbc",
"path": "k8s-yamls/",
"dest": "./k8s/",
"source": "private"
"source": "private",
"strip_path": ""
},
{
"repo": "github.com/replicatedhq/ship",
"ref": "master",
"path": "hack/docs/",
"dest": "./docs/",
"dest": "./docs{{repl Add 1 1}}/",
"source": "public",
"mode": 644
"mode": 644,
"strip_path": "{{repl ParseBool \"true\"}}"
}
],
"type": "object",
Expand Down Expand Up @@ -348,6 +350,10 @@
"description": "One of `public` or `private`, if `private`, access to the repo can be validated on release creation",
"type": "string"
},
"strip_path": {
"description": "If true, the github directory will not be included in the filepath of the generated files. For instance, when outputting all files within 'source/' in the repository to the 'dest/' directory, the file 'source/a/file.txt' would be placed at 'dest/source/a/file.txt' when this is false and 'dest/a/file.txt' when this is true.",
"type": "string"
},
"when": {
"description": "This asset will be included when 'when' is omitted or true",
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ assets:
dest: ./github-noslash
source: public
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
- github:
repo: replicatedhq/test-charts
path: template-functions
dest: ./github-stripped
strip_path: "{{repl ParseBool \"true\"}}"
source: public
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
config:
v1:
- name: option_group
Expand Down
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: 1.0.1-SNAPSHOT

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
Expand Up @@ -20,6 +20,13 @@ assets:
dest: ./github-noslash
source: public
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
- github:
repo: replicatedhq/test-charts
path: template-functions
dest: ./github-stripped
strip_path: "{{repl ParseBool \"true\"}}"
source: public
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
config:
v1:
- name: option_group
Expand Down
1 change: 1 addition & 0 deletions pkg/api/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type GitHubAsset struct {
Ref string `json:"ref" yaml:"ref" hcl:"ref"`
Path string `json:"path" yaml:"path" hcl:"path"`
Source string `json:"source" yaml:"source" hcl:"source"`
StripPath string `json:"strip_path" yaml:"strip_path" hcl:"strip_path"`
}

// WebAsset is an asset whose contents are specified by the HTML at the corresponding URL
Expand Down
2 changes: 1 addition & 1 deletion pkg/lifecycle/render/amazoneks/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ func TestBuildAsset(t *testing.T) {
req.Error(err)
}

req.Equal(got, tt.want)
req.Equal(tt.want, got)
})
}
}
32 changes: 31 additions & 1 deletion pkg/lifecycle/render/github/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"os"
"path/filepath"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -100,7 +101,10 @@ func (r *LocalRenderer) Execute(
return errors.Wrapf(err, "building %s", file.Path)
}

filePath := filepath.Join(asset.Dest, file.Path)
filePath, err := getDestPath(file.Path, asset, builder)
if err != nil {
return errors.Wrapf(err, "determining destination for %s", file.Path)
}

basePath := filepath.Dir(filePath)
debug.Log("event", "mkdirall.attempt", "root", rootFs.RootPath, "dest", filePath, "basePath", basePath)
Expand All @@ -123,3 +127,29 @@ func (r *LocalRenderer) Execute(
return nil
}
}

func getDestPath(githubPath string, asset api.GitHubAsset, builder *templates.Builder) (string, error) {
stripPath, err := builder.Bool(asset.StripPath, false)
if err != nil {
return "", errors.Wrapf(err, "parse boolean from %q", asset.StripPath)
}

destDir, err := builder.String(asset.Dest)
if err != nil {
return "", errors.Wrapf(err, "get destination directory from %q", asset.Dest)
}

if stripPath {
// remove asset.Path's directory from the beginning of githubPath
sourcePathDir := filepath.ToSlash(filepath.Dir(asset.Path)) + "/"
githubPath = strings.TrimPrefix(githubPath, sourcePathDir)

// handle cases where the source path was a dir but a trailing slash was not included
if !strings.HasSuffix(asset.Path, "/") {
sourcePathBase := filepath.Base(asset.Path) + "/"
githubPath = strings.TrimPrefix(githubPath, sourcePathBase)
}
}

return filepath.Join(destDir, githubPath), nil
}
Loading

0 comments on commit 2200e0d

Please sign in to comment.