Skip to content

Commit

Permalink
adjustments to pass 'golint'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Jul 19, 2019
1 parent 4f54b72 commit 626747c
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 16 deletions.
1 change: 1 addition & 0 deletions a_main-packr.go

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

8 changes: 8 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ func updateCmd(cmd *cobra.Command, args []string) {
logrus.Info("generated k8s/deployment.yaml")
}

// Makefile // TODO: remove and move to init
makefile := template.NewGenerator(template.MakefileOptions())
if err := makefile.GenerateFile(TemplateFilesystem); err != nil {
logrus.Error(fmt.Sprintf("failed to generate Makefile: %s", err.Error()))
} else {
logrus.Info("generated Makefile")
}

// README.md
readme := generate.NewReadme(TemplateFilesystem, service.Interface, tplContext)
if err := readme.Update(); err != nil {
Expand Down
31 changes: 21 additions & 10 deletions internal/template/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (f *File) prepare(fs packr.Box) error {

// Render the specified template file
func (f *File) Render(fs packr.Box, templateContext Context) (rendered []byte, err error) {
skipPartials := false
if err := f.prepare(fs); err != nil {
return nil, err
}
Expand All @@ -58,7 +59,7 @@ func (f *File) Render(fs packr.Box, templateContext Context) (rendered []byte, e
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("FindString: %s", f.templates[0]))
}
f.tpl, err = template.New(path.Base(f.templates[0])).Funcs(sprig.TxtFuncMap()).Funcs(map[string]interface{}{
f.tpl = template.New(path.Base(f.templates[0])).Funcs(sprig.TxtFuncMap()).Funcs(map[string]interface{}{
"ReadmeOptionCheckbox": func(option string) string {
if !config.IsSet(option) {
img := "![disabled](https://img.icons8.com/color/24/000000/close-window.png)"
Expand All @@ -73,20 +74,30 @@ func (f *File) Render(fs packr.Box, templateContext Context) (rendered []byte, e
return img

},
}).Parse(tmp)
})

// FIXME: hack to support Makefiles
if f.name == "makefile" {
f.tpl.Delims("<<", ">>")
skipPartials = true
}

f.tpl, err = f.tpl.Parse(tmp)
if err != nil {
return nil, errors.Wrap(err, "Parse")
}

// parse all loaded partial templates
for i := 1; i < len(f.templates); i++ {
tmp, err := fs.FindString(f.templates[i])
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("FindString: %s", f.templates[i]))
}
f.tpl, err = f.tpl.Parse(tmp)
if err != nil {
return nil, errors.Wrap(err, "Parse")
if !skipPartials {
for i := 1; i < len(f.templates); i++ {
tmp, err := fs.FindString(f.templates[i])
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("FindString: %s", f.templates[i]))
}
f.tpl, err = f.tpl.Parse(tmp)
if err != nil {
return nil, errors.Wrap(err, "Parse")
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions internal/template/godin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ var fileOptions = map[string]GenerateOptions{
TargetFile: "k8s/deployment.yaml",
Overwrite: true,
},
"makefile": {
Template: "makefile",
IsGoSource: false,
TargetFile: "Makefile",
Overwrite: false,
},
"errors": {
Template: "domain_errors",
IsGoSource: true,
Expand Down Expand Up @@ -104,6 +110,19 @@ func K8sServiceOptions() GenerateOptions {
return opts
}

func MakefileOptions() GenerateOptions {
ctx := Context{
Service: Service{
Name: config.GetString("service.name"),
Namespace: config.GetString("service.namespace"),
},
}
opts := fileOptions["makefile"]
opts.Context = ctx

return opts
}

func MiddlewareOptions() GenerateOptions {
ctx := Context{
Service: Service{
Expand Down
2 changes: 1 addition & 1 deletion templates/amqp_subscriber_handler.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Loop over all subscribers. The slice is faked, tho. The generator will replace t
subscriber only, so we can safely assume that there is only one element in the slice.
*/}}
{{ range .Service.Subscriber }}
// {{ .Handler }} is responsible of handling all incoming AMQP messages with routing key '{{ .Subscription.Topic }}'
// {{ .Handler }}Subscriber is responsible of handling all incoming AMQP messages with routing key '{{ .Subscription.Topic }}'
// It might seem overly complicated at first, but the design is on purpose. You WANT to have access to the Delivery,
// thus it would not make sense to use a middleware for Decoding it into a DAO or domain-level object as you would
// loose access to the Delivery.
Expand Down
2 changes: 1 addition & 1 deletion templates/grpc_encode_decode.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func EncodeError(err error) error {
// TODO: this is a nice spot for convenience mapping functions :)

// ----------------[ ENCODER / DECODER ]----------------
{{- range .Service.Methods }}
{{ range .Service.Methods }}
{{- template "grpc_request_decoder" . }}
{{- template "grpc_response_encoder" . }}
{{- template "grpc_request_encoder" . }}
Expand Down
9 changes: 6 additions & 3 deletions templates/implementation.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"context"

"github.com/go-godin/log"
_ "{{ .Service.Module }}/internal/service"
"{{ .Service.Module }}/internal/service"
"{{ .Service.Module }}/internal/service/domain"
)

type serviceImplementation struct {
// UseCase implements all business use-cases
type UseCase struct {
logger log.Logger
}

func NewServiceImplementation(logger log.Logger) *serviceImplementation {
// NewServiceImplementation constructs the use-case (service) layer of the microservice.
// It provides the whole business use-cases and works on the domain entities.
func NewServiceImplementation(logger log.Logger) *UseCase {
return &serviceImplementation{
logger: logger,
}
Expand Down
1 change: 1 addition & 0 deletions templates/logging_middleware.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type loggingMiddleware struct {
logger log.Logger
}

// LoggingMiddleware logs every request and response of the service.
func LoggingMiddleware(logger log.Logger) Middleware {
return func(next service.{{ title .Service.Name }}) service.{{ title .Service.Name }} {
return &loggingMiddleware{next, logger}
Expand Down
101 changes: 101 additions & 0 deletions templates/makefile.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
SERVICE_NAME=ticket

GOBASE=$(shell pwd)
GOBIN=$(GOBASE)/bin
GODIRS = $(shell go list -f '{{.Dir}}' ./...)
KUBE_CONTEXT=$(cat ~/.kube/config | grep "current-context:" | sed "s/current-context: //")

COMMIT=$(shell git rev-parse --short HEAD)

DOCKER_IMAGE="docker.coozzy.ch/ticket-ticket"
DOCKER_TAG="v-${COMMIT}-dev"

MODULE="bitbucket.org/jdbergmann/ticket/ticket"
PROTO_SRC="/home/lukas/devel/work/protobuf/ticket/ticket"
PROTO_DST="proto"

.PHONY: build
build:
@echo "--> building"
@go build -o ${GOBIN}/ticket ./cmd/ticket/main.go

.PHONY: run
run:
echo "--> starting locally"
@LOG_LEVEL=${LOG_LEVEL} AMQP_CONNECTION=${AMQP_CONNECTION} AMQP_EXCHANGE=${AMQP_EXCHANGE} go run cmd/ticket/main.go

.PHONY: grpcui
grpcui:
grpcui -proto ${PROTO_SRC}/ticket.proto -port 5000 -plaintext localhost:50051

.PHONY: clean
clean:
echo "--> cleaning up binaries"
go clean -tags netgo -i ./...
rm -rf $(GOBIN)/*

.PHONY: clean
skaffold:
@echo "--> starting skaffold, make sure minikube is running"
@skaffold dev

.PHONY: docker
docker:
@echo "--> building docker image"
docker build . -t ${DOCKER_IMAGE}:${DOCKER_TAG} -f ./Dockerfile

.PHONY: docker-run
docker-run:
@echo "--> starting container ${DOCKER_IMAGE}:${DOCKER_TAG}"
@docker run \
--rm \
--name ${ticket} \
--network host \
${DOCKER_IMAGE}:${DOCKER_TAG}

.PHONY: proto
proto:
@rm -rf ${PROTO_DST}
@echo "--> fetching proto from ${PROTO_SRC}"
@make -C ${PROTO_SRC} go
@mkdir ${PROTO_DST}
@echo "--< moving stubs into ${PROTO_DST}"
@cp ${PROTO_SRC}/go/* ${PROTO_DST}

.PHONY: check
check: format.check lint

.PHONY: format.check
format.check: tools.goimports
@echo "--> checking code formatting with 'goimports'"
@goimports -w -l .

.PHONY: lint
lint: tools.golint
@echo "--> checking code style with 'golint'"
@echo $(GODIRS) | xargs -n 1 golint

#---------------
#-- tools
#---------------
.PHONY: tools tools.goimports tools.golint tools.govet

tools: tools.goimports tools.golint tools.govet

tools.goimports:
@command -v goimports >/dev/null ; if [ $$? -ne 0 ]; then \
echo "--> installing goimports"; \
go get golang.org/x/tools/cmd/goimports; \
fi

tools.govet:
@go tool vet 2>/dev/null ; if [ $$? -eq 3 ]; then \
echo "--> installing govet"; \
go get golang.org/x/tools/cmd/vet; \
fi

tools.golint:
@command -v golint >/dev/null ; if [ $$? -ne 0 ]; then \
echo "--> installing golint"; \
go get -u golang.org/x/lint/golint; \
fi
1 change: 1 addition & 0 deletions templates/middleware.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import (
"{{ .Service.Module }}/internal/service"
)

// Middleware defines the service-specific middleware type
type Middleware func(service service.{{ title .Service.Name }}) service.{{ title .Service.Name }}
2 changes: 1 addition & 1 deletion templates/partials/service_method.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{- range .Comments }}
{{ . }}
{{- end }}
func (s *serviceImplementation) {{ .Name }}( {{ .ParamList }}) ({{ .ReturnList }}) {
func (uc *UseCase) {{ .Name }}( {{ .ParamList }}) ({{ .ReturnList }}) {
return {{ .ReturnImplementationMissing }}
}
{{ end }}

0 comments on commit 626747c

Please sign in to comment.