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

Commit

Permalink
pulls in the buffalo-docker plugin (#1709)
Browse files Browse the repository at this point in the history
* pulls in the buffalo-docker plugin

* remove syncx
  • Loading branch information
markbates authored Jun 23, 2019
1 parent 7c454ed commit 6d6fdb9
Show file tree
Hide file tree
Showing 18 changed files with 399 additions and 10 deletions.
2 changes: 1 addition & 1 deletion buffalo/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
"strings"

"github.com/gobuffalo/buffalo-docker/genny/docker"
"github.com/gobuffalo/buffalo/genny/docker"
pop "github.com/gobuffalo/buffalo-pop/genny/newapp"
"github.com/gobuffalo/buffalo/genny/assets/standard"
"github.com/gobuffalo/buffalo/genny/assets/webpack"
Expand Down
8 changes: 8 additions & 0 deletions genny/docker/docker-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions genny/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package docker

import (
"text/template"

"github.com/gobuffalo/genny"
"github.com/gobuffalo/gogen"
"github.com/gobuffalo/packr/v2"
)

func New(opts *Options) (*genny.Generator, error) {
g := genny.New()

if err := opts.Validate(); err != nil {
return g, err
}

data := map[string]interface{}{
"opts": opts,
}

g.Box(packr.New("github.com/gobuffalo/buffalo/common", "../docker/templates/common"))

switch opts.Style {
case "multi":
g.Box(packr.New("github.com/gobuffalo/buffalo/multi", "../docker/templates/multi"))
case "standard":
g.Box(packr.New("github.com/gobuffalo/buffalo/standard", "../docker/templates/standard"))
}

helpers := template.FuncMap{}
t := gogen.TemplateTransformer(data, helpers)
g.Transformer(t)
g.Transformer(genny.Dot())

return g, nil
}
60 changes: 60 additions & 0 deletions genny/docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package docker

import (
"testing"

"github.com/gobuffalo/genny/gentest"
"github.com/stretchr/testify/require"
)

func Test_New(t *testing.T) {
r := require.New(t)

g, err := New(&Options{})
r.NoError(err)

run := gentest.NewRunner()
run.With(g)

r.NoError(run.Run())

res := run.Results()

r.Len(res.Commands, 0)

r.Len(res.Files, 2)

f := res.Files[0]
r.Equal(".dockerignore", f.Name())

f = res.Files[1]
r.Equal("Dockerfile", f.Name())
r.Contains(f.String(), "multi-stage")
}

func Test_New_Standard(t *testing.T) {
r := require.New(t)

g, err := New(&Options{
Style: "standard",
})
r.NoError(err)

run := gentest.NewRunner()
run.With(g)

r.NoError(run.Run())

res := run.Results()

r.Len(res.Commands, 0)

r.Len(res.Files, 2)

f := res.Files[0]
r.Equal(".dockerignore", f.Name())

f = res.Files[1]
r.Equal("Dockerfile", f.Name())
r.NotContains(f.String(), "multi-stage")
}
35 changes: 35 additions & 0 deletions genny/docker/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package docker

import (
"fmt"

"github.com/gobuffalo/buffalo/runtime"
"github.com/gobuffalo/meta"
)

type Options struct {
App meta.App `json:"app"`
Version string `json:"version"`
Style string `json:"style"`
}

// Validate that options are usuable
func (opts *Options) Validate() error {
if opts.App.IsZero() {
opts.App = meta.New(".")
}
if len(opts.Version) == 0 {
opts.Version = runtime.Version
}
if len(opts.Style) == 0 {
opts.Style = "multi"
}

switch opts.Style {
case "multi", "standard":
default:
return fmt.Errorf("unknown style option %s", opts.Style)
}

return nil
}
22 changes: 22 additions & 0 deletions genny/docker/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package docker

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_Options_Validate(t *testing.T) {
r := require.New(t)

opts := &Options{
Style: "foo",
}
err := opts.Validate()
r.Error(err)

opts.Style = "multi"
err = opts.Validate()
r.NoError(err)

}
3 changes: 3 additions & 0 deletions genny/docker/templates/common/-dot-dockerignore.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
*.log
bin/
48 changes: 48 additions & 0 deletions genny/docker/templates/multi/Dockerfile.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This is a multi-stage Dockerfile and requires >= Docker 17.05
# https://docs.docker.com/engine/userguide/eng-image/multistage-build/
FROM gobuffalo/buffalo:{{.opts.Version}} as builder

RUN mkdir -p $GOPATH/src/{{.opts.App.PackagePkg}}
WORKDIR $GOPATH/src/{{.opts.App.PackagePkg}}

{{if .opts.App.WithWebpack -}}
# this will cache the npm install step, unless package.json changes
ADD package.json .
{{if .opts.App.WithYarn -}}
ADD yarn.lock .
RUN yarn install --no-progress
{{else -}}
RUN npm install --no-progress
{{end -}}
{{end -}}

ADD . .
{{if .opts.App.WithDep -}}
RUN dep ensure
{{else -}}
{{if .opts.App.WithModules -}}
ENV GO111MODULES=on
{{end -}}
RUN go get ./...
{{end -}}
RUN buffalo build --static -o /bin/app

FROM alpine
RUN apk add --no-cache bash
RUN apk add --no-cache ca-certificates

WORKDIR /bin/

COPY --from=builder /bin/app .

# Uncomment to run the binary in "production" mode:
# ENV GO_ENV=production

# Bind the app to 0.0.0.0 so it can be seen from outside the container
ENV ADDR=0.0.0.0

EXPOSE 3000

# Uncomment to run the migrations before running the binary:
# CMD /bin/app migrate; /bin/app
CMD exec /bin/app
37 changes: 37 additions & 0 deletions genny/docker/templates/standard/Dockerfile.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM gobuffalo/buffalo:{{.opts.Version}}

RUN mkdir -p $GOPATH/src/{{.opts.App.PackagePkg}}
WORKDIR $GOPATH/src/{{.opts.App.PackagePkg}}

{{if .opts.App.AsWeb -}}
{{if .opts.App.WithWebpack -}}
# this will cache the npm install step, unless package.json changes
ADD package.json .
{{if .opts.App.WithYarn -}}
ADD yarn.lock .
RUN yarn install --no-progress
{{else -}}
RUN npm install --no-progress
{{end -}}
{{end -}}
{{end -}}

ADD . .
{{if .opts.App.WithDep -}}
RUN dep ensure
{{else -}}
RUN go get $(go list ./... | grep -v /vendor/)
{{end -}}
RUN buffalo build --static -o /bin/app

# Uncomment to run the binary in "production" mode:
# ENV GO_ENV=production

# Bind the app to 0.0.0.0 so it can be seen from outside the container
ENV ADDR=0.0.0.0

EXPOSE 3000

# Uncomment to run the migrations before running the binary:
# CMD /bin/app migrate; /bin/app
CMD exec /bin/app
2 changes: 1 addition & 1 deletion genny/newapp/core/core.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package core

import (
"github.com/gobuffalo/buffalo-docker/genny/docker"
"github.com/gobuffalo/buffalo/genny/docker"
pop "github.com/gobuffalo/buffalo-pop/genny/newapp"
"github.com/gobuffalo/buffalo/genny/ci"
"github.com/gobuffalo/buffalo/genny/plugins/install"
Expand Down
2 changes: 1 addition & 1 deletion genny/newapp/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"path/filepath"
"testing"

"github.com/gobuffalo/buffalo-docker/genny/docker"
"github.com/gobuffalo/buffalo/genny/docker"
"github.com/gobuffalo/buffalo/runtime"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/genny/gentest"
Expand Down
2 changes: 1 addition & 1 deletion genny/newapp/core/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"strings"

"github.com/gobuffalo/buffalo-docker/genny/docker"
"github.com/gobuffalo/buffalo/genny/docker"
"github.com/gobuffalo/buffalo-pop/genny/newapp"
"github.com/gobuffalo/buffalo/genny/ci"
"github.com/gobuffalo/buffalo/genny/refresh"
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.7.0
github.com/gobuffalo/attrs v0.1.0
github.com/gobuffalo/buffalo-docker v1.1.0
github.com/gobuffalo/buffalo-pop v1.14.0
github.com/gobuffalo/clara v0.6.0
github.com/gobuffalo/envy v1.7.0
Expand All @@ -28,7 +27,6 @@ require (
github.com/gobuffalo/pop v4.11.1+incompatible
github.com/gobuffalo/release v1.7.0
github.com/gobuffalo/shoulders v1.1.0 // indirect
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754
github.com/gobuffalo/tags v2.1.0+incompatible
github.com/gobuffalo/x v0.0.0-20190614162758-d80e318e1bb4 // indirect
github.com/gorilla/mux v1.7.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ github.com/gobuffalo/buffalo v0.14.6/go.mod h1:71Un+T2JGgwXLjBqYFdGSooz/OUjw15BJ
github.com/gobuffalo/buffalo-docker v1.0.5/go.mod h1:NZ3+21WIdqOUlOlM2onCt7cwojYp4Qwlsngoolw8zlE=
github.com/gobuffalo/buffalo-docker v1.0.6/go.mod h1:UlqKHJD8CQvyIb+pFq+m/JQW2w2mXuhxsaKaTj1X1XI=
github.com/gobuffalo/buffalo-docker v1.0.7/go.mod h1:BdB8AhcmjwR6Lo3rDPMzyh/+eNjYnZ1TAO0eZeLkhig=
github.com/gobuffalo/buffalo-docker v1.1.0 h1:2lkaY8aLxyRz1VSJCz/Fw1wMDpm6swh2d3RfF9Ai8vg=
github.com/gobuffalo/buffalo-docker v1.1.0/go.mod h1:O06o+u8rgQCHe8a79sD+yibbBbjWF5OoxVW1tq2+eUw=
github.com/gobuffalo/buffalo-plugins v1.0.2/go.mod h1:pOp/uF7X3IShFHyobahTkTLZaeUXwb0GrUTb9ngJWTs=
github.com/gobuffalo/buffalo-plugins v1.0.4/go.mod h1:pWS1vjtQ6uD17MVFWf7i3zfThrEKWlI5+PYLw/NaDB4=
github.com/gobuffalo/buffalo-plugins v1.4.3/go.mod h1:uCzTY0woez4nDMdQjkcOYKanngeUVRO2HZi7ezmAjWY=
Expand Down
21 changes: 21 additions & 0 deletions packrd/packed-packr.go

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions render/string_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package render

import (
"sort"
"sync"
)

// stringMap wraps sync.Map and uses the following types:
// key: string
// value: string
type stringMap struct {
data sync.Map
}

// Delete the key from the map
func (m *stringMap) Delete(key string) {
m.data.Delete(key)
}

// Load the key from the map.
// Returns string or bool.
// A false return indicates either the key was not found
// or the value is not of type string
func (m *stringMap) Load(key string) (string, bool) {
i, ok := m.data.Load(key)
if !ok {
return ``, false
}
s, ok := i.(string)
return s, ok
}

// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *stringMap) LoadOrStore(key string, value string) (string, bool) {
i, _ := m.data.LoadOrStore(key, value)
s, ok := i.(string)
return s, ok
}

// Range over the string values in the map
func (m *stringMap) Range(f func(key string, value string) bool) {
m.data.Range(func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return false
}
value, ok := v.(string)
if !ok {
return false
}
return f(key, value)
})
}

// Store a string in the map
func (m *stringMap) Store(key string, value string) {
m.data.Store(key, value)
}

// Keys returns a list of keys in the map
func (m *stringMap) Keys() []string {
var keys []string
m.Range(func(key string, value string) bool {
keys = append(keys, key)
return true
})
sort.Strings(keys)
return keys
}
Loading

0 comments on commit 6d6fdb9

Please sign in to comment.