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

Add Golint and fix all issues #189

Merged
merged 5 commits into from
Jan 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RUN go version
RUN apt-get update
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash
RUN apt-get install -y build-essential nodejs
RUN go get -u github.com/golang/lint/golint

ENV BP=$GOPATH/src/github.com/gobuffalo/buffalo

Expand All @@ -16,6 +17,8 @@ RUN go get -v -t ./...

RUN go test -race ./...

RUN golint -set_exit_status ./...

RUN go install ./buffalo

WORKDIR $GOPATH/src/
Expand Down
26 changes: 13 additions & 13 deletions buffalo/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ var outputBinName string
var zipBin bool

type builder struct {
cleanup []string
original_main []byte
workDir string
cleanup []string
originalMain []byte
workDir string
}

func (b *builder) clean(name ...string) string {
Expand Down Expand Up @@ -206,12 +206,12 @@ func (b *builder) buildRiceEmbedded() error {
}

func (b *builder) buildMain() error {
new_main := strings.Replace(string(b.original_main), "func main()", "func original_main()", 1)
newMain := strings.Replace(string(b.originalMain), "func main()", "func originalMain()", 1)
maingo, err := os.Create("main.go")
if err != nil {
return err
}
_, err = maingo.WriteString(new_main)
_, err = maingo.WriteString(newMain)
if err != nil {
return err
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func (b *builder) cleanupBuild() {
os.RemoveAll(b)
}
maingo, _ := os.Create("main.go")
maingo.Write(b.original_main)
maingo.Write(b.originalMain)
}

func (b *builder) run() error {
Expand Down Expand Up @@ -309,18 +309,18 @@ var buildCmd = &cobra.Command{
Aliases: []string{"b", "bill"},
Short: "Builds a Buffalo binary, including bundling of assets (go.rice & webpack)",
RunE: func(cc *cobra.Command, args []string) error {
original_main := &bytes.Buffer{}
originalMain := &bytes.Buffer{}
maingo, err := os.Open("main.go")
_, err = original_main.ReadFrom(maingo)
_, err = originalMain.ReadFrom(maingo)
if err != nil {
return err
}
maingo.Close()
pwd, _ := os.Getwd()
b := builder{
cleanup: []string{},
original_main: original_main.Bytes(),
workDir: pwd,
cleanup: []string{},
originalMain: originalMain.Bytes(),
workDir: pwd,
}
defer b.cleanupBuild()

Expand Down Expand Up @@ -362,15 +362,15 @@ var migrationBox *rice.Box
func main() {
args := os.Args
if len(args) == 1 {
original_main()
originalMain()
}
c := args[1]
switch c {
case "migrate":
migrate()
case "start", "run", "serve":
printVersion()
original_main()
originalMain()
case "version":
printVersion()
default:
Expand Down
1 change: 1 addition & 0 deletions examples/hello-world/actions/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import "github.com/gobuffalo/buffalo"

// HomeHandler renders index.html
func HomeHandler(c buffalo.Context) error {
return c.Render(200, r.HTML("index.html"))
}
4 changes: 2 additions & 2 deletions examples/hello-world/grifts/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"os"

"github.com/gobuffalo/buffalo/examples/hello-world/actions"
. "github.com/markbates/grift/grift"
mbgrift "github.com/markbates/grift/grift"
"github.com/olekukonko/tablewriter"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using a special name here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, just pushed a commit to fix it.

when I first started this PR . . . I misread something and thought there was a namespace collision. But there's not.

)

var _ = Add("routes", func(c *Context) error {
var _ = mbgrift.Add("routes", func(c *mbgrift.Context) error {
a := actions.App()
routes := a.Routes()

Expand Down
7 changes: 7 additions & 0 deletions examples/html-crud/actions/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func findUserMW(h buffalo.Handler) buffalo.Handler {
}
}

// UsersList renders an html page that shows users
func UsersList(c buffalo.Context) error {
users := &models.Users{}
tx := c.Get("tx").(*pop.Connection)
Expand All @@ -35,15 +36,18 @@ func UsersList(c buffalo.Context) error {
return c.Render(200, r.HTML("users/index.html"))
}

// UsersShow renders an html form for viewing a user
func UsersShow(c buffalo.Context) error {
return c.Render(200, r.HTML("users/show.html"))
}

// UsersNew renders a form for adding a new user
func UsersNew(c buffalo.Context) error {
c.Set("user", models.User{})
return c.Render(200, r.HTML("users/new.html"))
}

// UsersCreate creates a user
func UsersCreate(c buffalo.Context) error {
u := &models.User{}
err := c.Bind(u)
Expand All @@ -69,10 +73,12 @@ func UsersCreate(c buffalo.Context) error {
return c.Redirect(301, "/users/%d", u.ID)
}

// UsersEdit renders the editing page for a target user
func UsersEdit(c buffalo.Context) error {
return c.Render(200, r.HTML("users/edit.html"))
}

// UsersUpdate updates a target user
func UsersUpdate(c buffalo.Context) error {
tx := c.Get("tx").(*pop.Connection)
u := c.Get("user").(*models.User)
Expand Down Expand Up @@ -103,6 +109,7 @@ func UsersUpdate(c buffalo.Context) error {
return c.Redirect(301, "/users/%d", u.ID)
}

// UsersDelete removes a target user
func UsersDelete(c buffalo.Context) error {
tx := c.Get("tx").(*pop.Connection)
u := c.Get("user").(*models.User)
Expand Down
4 changes: 2 additions & 2 deletions examples/html-crud/grifts/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package grifts

import (
"github.com/gobuffalo/buffalo/examples/html-crud/models"
. "github.com/markbates/grift/grift"
"github.com/markbates/grift/grift"
"github.com/markbates/pop"
)

var _ = Add("db:seed", func(c *Context) error {
var _ = grift.Add("db:seed", func(c *grift.Context) error {
return models.DB.Transaction(func(tx *pop.Connection) error {
users := models.Users{
{FirstName: "Mark", LastName: "Bates", Email: "mark@example.com"},
Expand Down
4 changes: 2 additions & 2 deletions examples/html-crud/grifts/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"os"

"github.com/gobuffalo/buffalo/examples/html-crud/actions"
. "github.com/markbates/grift/grift"
"github.com/markbates/grift/grift"
"github.com/olekukonko/tablewriter"
)

var _ = Add("routes", func(c *Context) error {
var _ = grift.Add("routes", func(c *grift.Context) error {
a := actions.App()
routes := a.Routes()

Expand Down
1 change: 1 addition & 0 deletions examples/html-crud/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/markbates/pop"
)

// DB is a sql database connection
var DB *pop.Connection

func init() {
Expand Down
7 changes: 7 additions & 0 deletions examples/html-crud/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/markbates/pop"
)

// User model stores information about a user account
type User struct {
ID int `json:"id" db:"id" schema:"-"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
Expand All @@ -25,10 +26,13 @@ func (u User) String() string {
return string(b)
}

// FullName returns the first name and last name, separated by a space
func (u User) FullName() string {
return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}

// ValidateNew validates the User fields and checks whether a user has
// already claimed that email address
func (u *User) ValidateNew(tx *pop.Connection) (*validate.Errors, error) {
verrs, err := u.validateCommon(tx)
verrs.Append(validate.Validate(
Expand All @@ -47,13 +51,16 @@ func (u *User) ValidateNew(tx *pop.Connection) (*validate.Errors, error) {
return verrs, err
}

// ValidateUpdate validates the User fields and confirms that the email has
// not been claimed by a different user
func (u *User) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
verrs, err := u.validateCommon(tx)
verrs.Append(validate.Validate(
&validators.FuncValidator{
Fn: func() bool {
var b bool
if u.Email != "" {
// Differs from ValidateNew here - only check whether *other* users claimed email
b, err = tx.Where("email = ? and id != ?", u.Email, u.ID).Exists(u)
}
return !b
Expand Down
8 changes: 8 additions & 0 deletions examples/html-resource/actions/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
)

// UsersResource allows CRUD with HTTP against the User model
type UsersResource struct {
buffalo.BaseResource
}
Expand All @@ -29,6 +30,7 @@ func findUserMW(n string) buffalo.MiddlewareFunc {
}
}

// List shows all users in an HTML page
func (ur *UsersResource) List(c buffalo.Context) error {
users := &models.Users{}
tx := c.Get("tx").(*pop.Connection)
Expand All @@ -41,15 +43,18 @@ func (ur *UsersResource) List(c buffalo.Context) error {
return c.Render(200, r.HTML("users/index.html"))
}

// Show renders a target user in an HTML page
func (ur *UsersResource) Show(c buffalo.Context) error {
return c.Render(200, r.HTML("users/show.html"))
}

// New renders a form for adding a new user
func (ur *UsersResource) New(c buffalo.Context) error {
c.Set("user", models.User{})
return c.Render(200, r.HTML("users/new.html"))
}

// Create is a JSON API endpoint that adds a new user
func (ur *UsersResource) Create(c buffalo.Context) error {
u := &models.User{}
err := c.Bind(u)
Expand All @@ -75,10 +80,12 @@ func (ur *UsersResource) Create(c buffalo.Context) error {
return c.Redirect(301, "/users/%d", u.ID)
}

// Edit renders an html form for editing a user
func (ur *UsersResource) Edit(c buffalo.Context) error {
return c.Render(200, r.HTML("users/edit.html"))
}

// Update is a JSON API endpoint that updates a user
func (ur *UsersResource) Update(c buffalo.Context) error {
tx := c.Get("tx").(*pop.Connection)
u := c.Get("user").(*models.User)
Expand Down Expand Up @@ -109,6 +116,7 @@ func (ur *UsersResource) Update(c buffalo.Context) error {
return c.Redirect(301, "/users/%d", u.ID)
}

// Destroy is an API endpoint that deletes a user
func (ur *UsersResource) Destroy(c buffalo.Context) error {
tx := c.Get("tx").(*pop.Connection)
u := c.Get("user").(*models.User)
Expand Down
4 changes: 2 additions & 2 deletions examples/html-resource/grifts/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package grifts

import (
"github.com/gobuffalo/buffalo/examples/html-resource/models"
. "github.com/markbates/grift/grift"
"github.com/markbates/grift/grift"
"github.com/markbates/pop"
)

var _ = Add("db:seed", func(c *Context) error {
var _ = grift.Add("db:seed", func(c *grift.Context) error {
return models.DB.Transaction(func(tx *pop.Connection) error {
users := models.Users{
{FirstName: "Mark", LastName: "Bates", Email: "mark@example.com"},
Expand Down
4 changes: 2 additions & 2 deletions examples/html-resource/grifts/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"os"

"github.com/gobuffalo/buffalo/examples/html-resource/actions"
. "github.com/markbates/grift/grift"
"github.com/markbates/grift/grift"
"github.com/olekukonko/tablewriter"
)

var _ = Add("routes", func(c *Context) error {
var _ = grift.Add("routes", func(c *grift.Context) error {
a := actions.App()
routes := a.Routes()

Expand Down
1 change: 1 addition & 0 deletions examples/html-resource/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/markbates/pop"
)

// DB is a sql database connection
var DB *pop.Connection

func init() {
Expand Down
7 changes: 7 additions & 0 deletions examples/html-resource/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/markbates/pop"
)

// User model stores information about a user account
type User struct {
ID int `json:"id" db:"id" schema:"-"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
Expand All @@ -25,10 +26,13 @@ func (u User) String() string {
return string(b)
}

// FullName returns the first name and last name, separated by a space
func (u User) FullName() string {
return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}

// ValidateNew validates the User fields and checks whether a user has
// claimed that email address
func (u *User) ValidateNew(tx *pop.Connection) (*validate.Errors, error) {
verrs, err := u.validateCommon(tx)
verrs.Append(validate.Validate(
Expand All @@ -47,13 +51,16 @@ func (u *User) ValidateNew(tx *pop.Connection) (*validate.Errors, error) {
return verrs, err
}

// ValidateUpdate validates the User fields and confirms that the email has
// not been claimed
func (u *User) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
verrs, err := u.validateCommon(tx)
verrs.Append(validate.Validate(
&validators.FuncValidator{
Fn: func() bool {
var b bool
if u.Email != "" {
// Differs from ValidateNew here - only check whether *other* users claimed email
b, err = tx.Where("email = ? and id != ?", u.Email, u.ID).Exists(u)
}
return !b
Expand Down
Loading