Skip to content

Commit

Permalink
Merge pull request #96 from dyweb/errors/categorization/init
Browse files Browse the repository at this point in the history
[errors] Error inspection

- implemented error inspection based on go 2 proposal
- deprecated requests package
- add banner
  • Loading branch information
at15 authored Dec 31, 2018
2 parents 3de3856 + dabe7e9 commit fd4886c
Show file tree
Hide file tree
Showing 95 changed files with 3,194 additions and 457 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ignore vendor folder because we install it using dep
build
legacy
doc
playground
vendor
7 changes: 4 additions & 3 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Contribution Guidelines

Format and test the code locally
TODO

- `make fmt`
- `make test`
- [ ] add instruction for setup go environment and fork set upstream etc. (actually this is no longer a problem with go mod)

- Read the [style guide](../doc/style.md)
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: add issue templates https://help.github.com/articles/creating-issue-templates-for-your-repository/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ language: go
sudo: false

go:
- "1.10"
- "1.11"
- tip

install:
- go get -u github.com/golang/dep/cmd/dep
- go get -u golang.org/x/tools/cmd/goimports
- dep version
- dep ensure

Expand Down
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This Dockerfile is a demo of using go-dev to build a go binary using multi stage build
# It is based on
# https://docs.docker.com/v17.09/engine/userguide/eng-image/dockerfile_best-practices/#use-multi-stage-builds
FROM dyweb/go-dev:1.11.4 as builder

LABEL maintainer="[email protected]"

ARG PROJECT_ROOT=/go/src/github.com/dyweb/gommon/

WORKDIR $PROJECT_ROOT

# Gopkg.toml and Gopkg.lock lists project dependencies
# These layers will only be re-built when Gopkg files are updated
COPY Gopkg.lock Gopkg.toml $PROJECT_ROOT
RUN dep ensure -v -vendor-only

# Copy all project and build it
COPY . $PROJECT_ROOT
RUN make install

# NOTE: use ubuntu instead of alphine
#
# When using alpine I saw standard_init_linux.go:190: exec user process caused "no such file or directory",
# because I didn't compile go with static flag
# https://stackoverflow.com/questions/49535379/binary-compiled-in-prestage-doesnt-work-in-scratch-container
FROM ubuntu:18.04 as runner
LABEL maintainer="[email protected]"
LABEL github="github.com/dyweb/gommon"
WORKDIR /usr/bin
COPY --from=builder /go/bin/gommon .
ENTRYPOINT ["gommon"]
CMD ["help"]
31 changes: 23 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ help:
@echo "$$GOMMON_MAKEFILE_HELP_MSG"

# -- build vars ---
PKGS =./errors/... ./generator/... ./log/... ./noodle/... ./requests/... ./structure/... ./util/...
PKGST =./cmd ./errors ./generator ./log ./noodle ./requests ./structure ./util
PKGS =./errors/... ./generator/... ./log/... ./noodle/... ./structure/... ./util/...
PKGST =./cmd ./errors ./generator ./log ./noodle ./structure ./util
VERSION = 0.0.8
BUILD_COMMIT := $(shell git rev-parse HEAD)
BUILD_TIME := $(shell date +%Y-%m-%dT%H:%M:%S%z)
CURRENT_USER = $(USER)
FLAGS = -X main.version=$(VERSION) -X main.commit=$(BUILD_COMMIT) -X main.buildTime=$(BUILD_TIME) -X main.buildUser=$(CURRENT_USER)
DOCKER_REPO = dyweb/gommon
# -- build vars ---

.PHONY: install
install:
install: fmt test
go install -ldflags "$(FLAGS)" ./cmd/gommon

.PHONY: fmt
Expand Down Expand Up @@ -78,9 +79,13 @@ generate:
gommon generate -v

# --- test ---
.PHONY: test test-cover test-cover-html test-race test-log
.PHONY: test test-cover test-cover-html test-race
.PHONY: test-log test-errors

test:
go test -cover $(PKGS)

test-verbose:
go test -v -cover $(PKGS)

test-cover:
Expand All @@ -95,6 +100,8 @@ test-race:

test-log:
go test -v -cover ./log/...
test-errors:
go test -v -cover ./errors/...
# --- test ---

# TODO: refer tools used in https://github.com/360EntSecGroup-Skylar/goreporter
Expand Down Expand Up @@ -123,12 +130,20 @@ dep-update:
# --- dependency management ---

# --- docker ---
.PHONY: docker-test
.PHONY: docker-build docker-push docker-test

docker-build:
docker build -t $(DOCKER_REPO):$(VERSION) .

docker-push:
docker push $(DOCKER_REPO):$(VERSION)

# TODO: deprecated docker-compose based test
docker-test:
docker-compose -f scripts/docker-compose.yml run --rm golang1.10
docker-compose -f scripts/docker-compose.yml run --rm golanglatest

.PHONY: docker-remove-all-containers
docker-remove-all-containers:
docker rm $(shell docker ps -a -q)
#.PHONY: docker-remove-all-containers
#docker-remove-all-containers:
# docker rm $(shell docker ps -a -q)
# --- docker ---
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# Gommon - Go common libraries

<h1 align="center">
<br>
<img width="100%" src="doc/media/gommon.png" alt="gommon">
<br>
<br>
<br>
</h1>

[![GoDoc](https://godoc.org/github.com/dyweb/gommon?status.svg)](https://godoc.org/github.com/dyweb/gommon)
[![Build Status](https://travis-ci.org/dyweb/gommon.svg?branch=master)](https://travis-ci.org/dyweb/gommon)
[![codecov](https://codecov.io/gh/dyweb/gommon/branch/master/graph/badge.svg)](https://codecov.io/gh/dyweb/gommon)
[![Go Report Card](https://goreportcard.com/badge/github.com/dyweb/gommon)](https://goreportcard.com/report/github.com/dyweb/gommon)
[![codebeat badge](https://codebeat.co/badges/8d42a846-f1dc-4a6b-8bd9-5862726ed35d)](https://codebeat.co/projects/github-com-dyweb-gommon-master)
[![Sourcegraph](https://sourcegraph.com/github.com/dyweb/gommon/-/badge.svg)](https://sourcegraph.com/github.com/dyweb/gommon?badge)
[![](https://tokei.rs/b1/github/dyweb/gommon)](https://github.com/dyweb/gommon)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fdyweb%2Fgommon.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fdyweb%2Fgommon?ref=badge_shield)
Expand All @@ -13,11 +20,11 @@ Gommon is a collection of common util libraries written in Go.

It has the following components:

- [errors](errors) Typed error with context, multi error
- [generator](generator) Render go template, generate methods for logger interface based on `gommon.yml`
- [log](log) A Javaish logger for Go, application can set level for their dependencies based on package, struct
- [noodle](noodle) Embed static assets for web application with `.noodleignore` support
- [structure](structure) Bring data structure like Set etc. to Golang
- [errors](errors) error wrapping, inspection, multi error (error list), common error types
- [log](log) fine grained level control and reasonable performance
- [noodle](noodle) embed static assets for web application with `.noodleignore` support
- [generator](generator) render go template, generate methods for logger interface based on `gommon.yml`
- [structure](structure) data structure like Set etc. to go
- [util](util) small utils over standard libraries utils

Deprecating
Expand Down
55 changes: 55 additions & 0 deletions build/go-dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
ARG BASE_IMAGE=ubuntu:18.04
FROM $BASE_IMAGE

LABEL maintainer="[email protected]"

# https://github.com/dyweb/gommon/issues/98
RUN \
export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get upgrade -y --no-install-recommends \
&& apt-get install -y --no-install-recommends \
bash \
build-essential \
ca-certificates \
curl \
wget \
git-core \
ssh-client \
man \
vim \
zip \
unzip \
tmux \
netcat \
telnet \
tree \
strace \
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ENV GOPATH=/go
ENV PATH=$GOPATH/bin:/usr/local/go/bin:$PATH
# TODO: do we need chmod -R 777 like https://github.com/docker-library/golang/blob/master/Dockerfile-debian.template
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin"

ARG BUILD_GO_VERSION=1.11.2

# glide no longer have release, just hard code it to latest version
ENV GO_VERSION=$BUILD_GO_VERSION \
GLIDE_VERSION=v0.13.1

# TODO: might put glide under GOPATH/bin
RUN \
curl -L https://dl.google.com/go/go$GO_VERSION.linux-amd64.tar.gz | tar -C /usr/local -xz \
&& curl -sSL https://github.com/Masterminds/glide/releases/download/$GLIDE_VERSION/glide-$GLIDE_VERSION-linux-amd64.tar.gz \
| tar -vxz -C /usr/local/bin --strip=1 \
&& rm /usr/local/bin/README.md /usr/local/bin/LICENSE

# TODO: install dep may have problem when go mod is enabled ...
# dep releases are way behind master, so we install from source
RUN go get -u -v github.com/golang/dep/cmd/dep \
&& go get -u -v golang.org/x/tools/cmd/goimports

WORKDIR $GOPATH
19 changes: 19 additions & 0 deletions build/go-dev/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DOCKER_REPO = dyweb/go-dev
GO_VERSIONS = 1.10.7 1.11.4
BUILDS = $(addprefix build-, $(GO_VERSIONS))
PUSHS = $(addprefix push-, $(GO_VERSIONS))

.PHONY: build push

$(BUILDS):
docker build -t $(DOCKER_REPO):$(subst build-,,$@) --build-arg BUILD_GO_VERSION=$(subst build-,,$@) .

$(PUSHS):
docker push $(DOCKER_REPO):$(subst push-,,$@)

build: $(BUILDS)

push: $(PUSHS)

run:
docker run --rm -it --entrypoint /bin/bash $(DOCKER_REPO):1.11.4
28 changes: 28 additions & 0 deletions build/go-dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# go-dev

go-dev is a base image for building go code

See [#98](https://github.com/dyweb/gommon/issues/98)

- Docker Hub https://hub.docker.com/r/dyweb/go-dev

## Use

Use it as base in you multi stage build, the image is big, you don't want to use it directly unless
you need a full go environment to run your code (looking at ginkgo)

TODO: example using multi stage build using dep and go mods

Use it as a playground, run the container in interactive mode and remove it after you are done

````bash
docker run --rm -it --entrypoint /bin/bash dyweb/go-dev:1.11.4
````

## Build

````bash
make build
# need to login to dockerhub and be member
make push
````
63 changes: 61 additions & 2 deletions cmd/gommon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/spf13/cobra"

Expand All @@ -13,6 +17,7 @@ import (
dlog "github.com/dyweb/gommon/log"
"github.com/dyweb/gommon/log/handlers/cli"
"github.com/dyweb/gommon/noodle"
"github.com/dyweb/gommon/util/fsutil"
"github.com/dyweb/gommon/util/logutil"
)

Expand Down Expand Up @@ -67,6 +72,7 @@ func main() {
rootCmd.AddCommand(
versionCmd,
genCmd(),
addBuildIgnoreCmd(),
)
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -75,7 +81,7 @@ func main() {
}

func genCmd() *cobra.Command {
gen := &cobra.Command{
gen := cobra.Command{
Use: "generate",
Aliases: []string{"gen"},
Short: "generate code based on gommon.yml",
Expand Down Expand Up @@ -132,7 +138,60 @@ func genCmd() *cobra.Command {
noodleCmd.Flags().StringVar(&pkg, "pkg", "gen", "go package of generated file")
noodleCmd.Flags().StringVar(&output, "output", "noodle.go", "path for generated file")
gen.AddCommand(noodleCmd)
return gen
return &gen
}

func addBuildIgnoreCmd() *cobra.Command {
cmd := cobra.Command{
Use: "add-build-ignore",
Short: "Add // +build ignore to go files",
Run: func(cmd *cobra.Command, args []string) {
pwd, err := os.Getwd()
if err != nil {
log.Fatalf("error get current directory: %s", err)
return
}
buildIgnore := "// +build ignore\n\n"
ignores := fsutil.NewIgnores([]fsutil.IgnorePattern{
fsutil.ExactPattern(".git"),
fsutil.ExactPattern("testdata"),
fsutil.ExactPattern("vendor"),
fsutil.ExactPattern(".idea"),
fsutil.ExactPattern(".vscode"),
}, nil)
for _, p := range args {
err = fsutil.Walk(filepath.Join(pwd, p), ignores, func(path string, info os.FileInfo) {
if info.IsDir() || !strings.HasSuffix(info.Name(), ".go") {
return
}
f := filepath.Join(path, info.Name())
b, err := ioutil.ReadFile(f)
if err != nil {
log.Fatalf("error read file %s", err)
return
}
// NOTE: we only
prefix := []byte(buildIgnore)
if bytes.HasPrefix(b, prefix) {
log.Warnf("%s already have build ignore prefix", f)
return
}
// prepend prefix
b = append(prefix, b...)
if err := fsutil.WriteFile(f, b); err != nil {
log.Fatalf("error write file with build prefix: %s", err)
return
}
log.Infof("updated %s", f)
})
if err != nil {
log.Fatalf("error walk %s", err)
return
}
}
},
}
return &cmd
}

func init() {
Expand Down
Loading

0 comments on commit fd4886c

Please sign in to comment.