Skip to content
This repository has been archived by the owner on Oct 5, 2020. It is now read-only.

Commit

Permalink
Introduce file struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bethge committed Jan 3, 2018
1 parent 8df185d commit 054c017
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 65 deletions.
6 changes: 3 additions & 3 deletions pkg/api/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type FetchResult struct {
Branch *gh.Branch
Service *settings.Service
Templates map[string]string
Templates []gh.File
}

func (app *App) Fetch(project, branchName string) (*FetchResult, error) {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (app *App) Fetch(project, branchName string) (*FetchResult, error) {

service.Location.Ref = branch.SHA

templateStrings, err := app.Clients.GitHub.GetFiles(&service.Location)
files, err := app.Clients.GitHub.GetFiles(&service.Location)
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -69,6 +69,6 @@ func (app *App) Fetch(project, branchName string) (*FetchResult, error) {
return &FetchResult{
Branch: branch,
Service: service,
Templates: templateStrings,
Templates: files,
}, nil
}
15 changes: 8 additions & 7 deletions pkg/api/render.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"github.com/rebuy-de/kubernetes-deployment/pkg/gh"
"regexp"
"strings"

Expand Down Expand Up @@ -31,31 +32,31 @@ func (app *App) Render(fetched *FetchResult) ([]runtime.Object, error) {
return app.decode(rendered)
}

func (app *App) decode(rendered map[string]string) ([]runtime.Object, error) {
func (app *App) decode(files []gh.File) ([]runtime.Object, error) {
var objects []runtime.Object
decode := scheme.Codecs.UniversalDeserializer().Decode

for name, data := range rendered {
if !strings.HasSuffix(name, ".yaml") && !strings.HasSuffix(name, ".yml") {
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".yaml") && !strings.HasSuffix(file.Name(), ".yml") {
log.WithFields(log.Fields{
"Name": name,
"Name": file.Name(),
}).Debug("Ignoring file with wrong extension.")
continue
}

splitted := regexp.MustCompile("[\n\r]---").Split(data, -1)
splitted := regexp.MustCompile("[\n\r]---").Split(file.Content, -1)

for _, part := range splitted {
if strings.TrimSpace(part) == "" {
log.WithFields(log.Fields{
"Name": name,
"Name": file.Name(),
}).Debug("Ignoring empty file.")
continue
}

obj, _, err := decode([]byte(part), nil, nil)
if err != nil {
return nil, errors.Wrapf(err, "unable to decode file '%s'", name)
return nil, errors.Wrapf(err, "unable to decode file '%s'", file.Name())
}

obj, err = app.Interceptors.PostManifestRender(obj)
Expand Down
15 changes: 11 additions & 4 deletions pkg/api/render_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package api

import (
"github.com/rebuy-de/kubernetes-deployment/pkg/gh"
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors"
"github.com/rebuy-de/kubernetes-deployment/pkg/testutil"
"testing"
"io/ioutil"
"path"
"testing"
)

func readFile(t *testing.T, path string) string {
Expand All @@ -18,9 +19,15 @@ func readFile(t *testing.T, path string) string {

func TestDecode(t *testing.T) {
dir := "test-fixtures"
tcs := map[string]string{
"manifest-deployment.yaml": readFile(t, path.Join(dir, "manifest-deployment.yaml")),
"manifest-podpreset.yaml": readFile(t, path.Join(dir, "manifest-podpreset.yaml")),
tcs := []gh.File{
{
Path: "manifest-deployment.yaml",
Content: readFile(t, path.Join(dir, "manifest-deployment.yaml")),
},
{
Path: "manifest-podpreset.yaml",
Content: readFile(t, path.Join(dir, "manifest-podpreset.yaml")),
},
}
golden := "decoded-golden.yaml"

Expand Down
33 changes: 17 additions & 16 deletions pkg/gh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (

type Interface interface {
GetBranch(location *Location) (*Branch, error)
GetFile(location *Location) (string, error)
GetFiles(location *Location) (map[string]string, error)
GetFile(location *Location) (File, error)
GetFiles(location *Location) ([]File, error)
GetStatuses(location *Location) ([]github.RepoStatus, error)
}

Expand Down Expand Up @@ -111,11 +111,11 @@ func (gh *API) GetBranch(location *Location) (*Branch, error) {
}

type FileFuture struct {
file chan string
file chan File
err chan error
}

func (ff *FileFuture) Get() (string, error) {
func (ff *FileFuture) Get() (File, error) {
select {
case file := <-ff.file:
close(ff.file)
Expand All @@ -124,13 +124,13 @@ func (ff *FileFuture) Get() (string, error) {
case err := <-ff.err:
close(ff.file)
close(ff.err)
return "", err
return File{}, err
}
}

func (gh *API) GetFileAsync(location *Location) *FileFuture {
ff := &FileFuture{
file: make(chan string, 1),
file: make(chan File, 1),
err: make(chan error, 1),
}

Expand All @@ -145,7 +145,7 @@ func (gh *API) GetFileAsync(location *Location) *FileFuture {
return ff
}

func (gh *API) GetFile(location *Location) (string, error) {
func (gh *API) GetFile(location *Location) (File, error) {
log.WithFields(
log.Fields(structs.Map(location)),
).Debug("downloading file from GitHub")
Expand All @@ -159,19 +159,19 @@ func (gh *API) GetFile(location *Location) (string, error) {
)

if err != nil {
return "", errors.Wrapf(err,
return File{}, errors.Wrapf(err,
"unable to fetch file '%v' from GitHub", location)
}

if file == nil {
return "", errors.Errorf(
return File{}, errors.Errorf(
"unable to fetch file '%v' from GitHub; probably it's a directory",
location)
}

content, err := file.GetContent()
if err != nil {
return "", errors.Wrapf(err,
return File{}, errors.Wrapf(err,
"unable to decode file '%v'", location)
}

Expand All @@ -186,10 +186,10 @@ func (gh *API) GetFile(location *Location) (string, error) {

gh.statsd.Gauge("github.rate.remaining", resp.Rate.Remaining)

return content, nil
return File{location.Path, content}, nil
}

func (gh *API) GetFiles(location *Location) (map[string]string, error) {
func (gh *API) GetFiles(location *Location) ([]File, error) {
log.WithFields(
log.Fields(structs.Map(location)),
).Debug("downloading directory from GitHub")
Expand Down Expand Up @@ -237,17 +237,18 @@ func (gh *API) GetFiles(location *Location) (map[string]string, error) {
})
}

result := make(map[string]string)
for name, future := range futures {
result[name], err = future.Get()
var files []File
for _, future := range futures {
file, err := future.Get()
if err != nil {
return nil, errors.Wrapf(err,
"unable to decode file '%v'",
location)
}
files = append(files, file)
}

return result, nil
return files, nil
}

func (gh *API) GetStatuses(location *Location) ([]github.RepoStatus, error) {
Expand Down
22 changes: 13 additions & 9 deletions pkg/gh/fake/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,28 @@ func (d *GitHub) GetBranch(l *gh.Location) (*gh.Branch, error) {
return &branch, nil
}

func (d *GitHub) GetFile(l *gh.Location) (string, error) {
content := (*d)[l.Owner][l.Repo][l.Ref].Files[l.Path]
return content, nil
func (d *GitHub) GetFile(l *gh.Location) (gh.File, error) {
for _, file := range (*d)[l.Owner][l.Repo][l.Ref].Files {
if file.Path == l.Path {
return file, nil
}
}
return gh.File{}, nil
}

func (d *GitHub) GetFiles(l *gh.Location) (map[string]string, error) {
files := make(map[string]string)
func (d *GitHub) GetFiles(l *gh.Location) ([]gh.File, error) {
var files []gh.File

for p, content := range (*d)[l.Owner][l.Repo][l.Ref].Files {
dir, file := path.Split("/" + p)
for _, file := range (*d)[l.Owner][l.Repo][l.Ref].Files {
dir, _ := path.Split("/" + file.Path)
if path.Clean("/"+dir+"/") == path.Clean("/"+l.Path+"/") {
files[path.Clean(file)] = content
files = append(files, file)
}
}

return files, nil
}

func (d *GitHub) GetStatuses(location *gh.Location) ([]github.RepoStatus, error) {
return nil, fmt.Errorf("not implmented, yet")
return nil, fmt.Errorf("not implemented, yet")
}
22 changes: 11 additions & 11 deletions pkg/gh/fake/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ var (
"master": Branch{
Meta: ExampleBranch,
Files: Files{
"deployments.yaml": YAML([]string{"foo", "bar"}),
"README.md": "blubber",
"sub/foo.txt": "bar",
"sub/bim.txt": "baz",
{Path: "deployments.yaml", Content: YAML([]string{"foo", "bar"})},
{Path: "README.md", Content: "blubber"},
{Path: "sub/foo.txt", Content: "bar"},
{Path: "sub/bim.txt", Content: "baz"},
},
},
},
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestGetFile(t *testing.T) {

expected := "- foo\n- bar\n"

if file != expected {
if file.Content != expected {
t.Errorf("File contents don't match:")
t.Errorf(" Expected: %#v", expected)
t.Errorf(" Obtained: %#v", file)
Expand All @@ -88,9 +88,9 @@ func TestGetFiles(t *testing.T) {
t.Fatal(err)
}

expected := map[string]string{
"deployments.yaml": "- foo\n- bar\n",
"README.md": "blubber",
expected := []gh.File{
{Path: "deployments.yaml", Content: "- foo\n- bar\n"},
{Path: "README.md", Content: "blubber"},
}

if !reflect.DeepEqual(files, expected) {
Expand All @@ -106,9 +106,9 @@ func TestGetSubdirectoryFiles(t *testing.T) {
t.Fatal(err)
}

expected := map[string]string{
"foo.txt": "bar",
"bim.txt": "baz",
expected := []gh.File{
{Path: "sub/foo.txt", Content: "bar"},
{Path: "sub/bim.txt", Content: "baz"},
}

if !reflect.DeepEqual(files, expected) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gh/fake/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ type Branch struct {
Files Files
}

type Files map[string]string
type Files []gh.File
9 changes: 5 additions & 4 deletions pkg/gh/fake/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"os"
"path/filepath"

yaml "gopkg.in/yaml.v2"
"github.com/rebuy-de/kubernetes-deployment/pkg/gh"
"gopkg.in/yaml.v2"
)

func YAML(obj interface{}) string {
Expand All @@ -18,7 +19,7 @@ func YAML(obj interface{}) string {
}

func ScanFiles(root string) Files {
files := make(Files)
var files Files

err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
if err != nil {
Expand All @@ -29,13 +30,13 @@ func ScanFiles(root string) Files {
return nil
}

name, err := filepath.Rel(root, path)
relPath, err := filepath.Rel(root, path)
if err != nil {
return err
}

raw, err := ioutil.ReadFile(path)
files[name] = string(raw)
files = append(files, gh.File{Path: relPath, Content: string(raw)})
return err
})
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/gh/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gh

import "path"

type File struct {
Path string
Content string
}

func (f *File) Name() string {
_, name := path.Split(f.Path)
return name
}
2 changes: 1 addition & 1 deletion pkg/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os/exec"
"strings"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down
6 changes: 3 additions & 3 deletions pkg/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/rebuy-de/kubernetes-deployment/pkg/gh"

log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)

type Contexts map[string]Service
Expand Down Expand Up @@ -55,11 +55,11 @@ func ReadFromGitHub(filename string, client gh.Interface) (*Settings, error) {
return nil, errors.Wrapf(err, "parse GitHub location '%s'; use './' prefix to use a directory named 'github.com'", filename)
}

data, err := client.GetFile(location)
file, err := client.GetFile(location)
if err != nil {
return nil, errors.Wrapf(err, "could not download file '%s'", location)
}
return FromBytes([]byte(data))
return FromBytes([]byte(file.Content))
}

func (s *Settings) CurrentContext() Service {
Expand Down
Loading

0 comments on commit 054c017

Please sign in to comment.