Skip to content

Commit

Permalink
feat(module): Go module support for 1.11+
Browse files Browse the repository at this point in the history
Replaces dep with go mod for dependency management. As a result go 1.11+ is now a requirement.
  • Loading branch information
jamesjwarren committed Oct 8, 2018
1 parent c280946 commit 83923c2
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 96 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ A Go project template that gives us our standard Go project setup.
Powered by [Cookiecutter](https://github.com/audreyr/cookiecutter).

## Features
- Uses [dep](https://github.com/golang/dep) for dependancy management
- Uses `go mod` for dependancy management
- Uses [viper](https://github.com/spf13/viper) for configuraiton
- Uses [cobra](https://github.com/spf13/cobra) for CLI commands in `cmd` package
- Uses [zerlog](https://github.com/rs/zerolog) for structured logging
- Uses [zerolog](https://github.com/rs/zerolog) for structured logging

## Optional Features
- Configures GOPATH and installs deps
- Dockerfile for building go binary and dockerfile with final binary
- Option of GitlabCI

## Usage

1. Get [cookiecutter](https://github.com/audreyr/cookiecutter) via `pip` or `brew`
2. Get `dep`: https://github.com/golang/dep/releases
3. Use `cookiecutter`: `cookiecutter https://github.com/thisissoon/GoCookieCutter.git`
4. Fill in the information `cookiecutter` asks you
5. The project is now setup in `name/src/name` directory, `cd` into it
2. Use `cookiecutter`: `cookiecutter https://github.com/thisissoon/GoCookieCutter.git`
3. Fill in the information `cookiecutter` asks you
4. The project is now setup in `name` directory, `cd` into it
6. Read the `README.md`
6 changes: 2 additions & 4 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"origin": null,
"image": "n",
"use_ci": ["gitlab", "none"],
"go": "1.10.1",
"gopath": "{{ cookiecutter.name }}",
"pkg": "{{ cookiecutter.origin }}",
"install": "y"
"go": "1.11.1",
"module": "{{ cookiecutter.origin }}"
}
53 changes: 4 additions & 49 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
Does the following:
1. Inits git if used
2. Deletes dockerfiles if not going to be used
3. Configures GOPATH and installs dependencies
2. Deletes Dockerfile if not going to be used
3. Deletes CI config based on selection
"""
from __future__ import print_function
import logging
Expand Down Expand Up @@ -49,38 +49,6 @@ def remove_docker_files():
PROJECT_DIRECTORY, filename
))

def setup_gopath():
"""
Creates GOPATH structure and moves project files
"""
thismodule.FINAL_DIRECTORY = os.path.abspath(os.path.join(os.getcwd(), '..', '{{ cookiecutter.gopath }}', 'src', '{{ cookiecutter.pkg }}'))
logger.info('Setting up GOPATH structure, using package path {}'.format(FINAL_DIRECTORY))

tmp = tempfile.mkdtemp()
shutil.move(PROJECT_DIRECTORY, tmp)

# setup go src dir
src = os.path.join(PROJECT_DIRECTORY, 'src')
os.makedirs(src)
# setup pkg dirs
pkg = os.path.join(src, '{{ cookiecutter.pkg }}')
os.makedirs(pkg)

copy_and_overwrite(os.path.join(tmp, '{{ cookiecutter.name }}'), FINAL_DIRECTORY)
shutil.rmtree(tmp)

os.environ['GOPATH'] = PROJECT_DIRECTORY
os.environ['PATH'] = os.environ['PATH'] + os.path.join(PROJECT_DIRECTORY, 'bin')

def install_deps():
"""
Installs dependencies with dep
"""

logger.info('Installing dependencies...')
dep = Popen(['dep', 'ensure'], cwd=FINAL_DIRECTORY)
dep.wait()

def init_git():
"""
Initialises git on the new project folder
Expand All @@ -107,24 +75,11 @@ def init_git():
else:
remove_file('.gitlab-ci.yml')

# 3. Setup GOPATH
if '{{ cookiecutter.pkg }}'.lower() != 'n':
setup_gopath()

# 4. Install deps
if '{{ cookiecutter.install }}'.lower() != 'n':
install_deps()

# 5. Initialize Git (should be run after all file have been modified or deleted)
# 3. Initialize Git (should be run after all files have been modified or deleted)
if '{{ cookiecutter.origin }}'.lower() != 'n':
init_git()
else:
remove_file('.gitignore')

logger.info('Your project is ready to go, to start working:')
if '{{ cookiecutter.install }}'.lower() != 'n':
logger.info('`cd {0}/src/{1}`'.format(PROJECT_DIRECTORY, '{{ cookiecutter.pkg }}'))
logger.info('`export GOPATH={}`'.format(PROJECT_DIRECTORY))
else:
logger.info('Manually configure your GOPATH')
logger.info('Install deps: `dep ensure`')
logger.info('`cd {0}`'.format(PROJECT_DIRECTORY))
3 changes: 0 additions & 3 deletions {{cookiecutter.name}}/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

variables:
DOCKER_IMAGE: {{cookiecutter.image}}
SRC: src/{{cookiecutter.pkg}}

#
# Stages
Expand All @@ -39,6 +38,4 @@ test:
image: golang:{{cookiecutter.go}}-alpine
script:
- apk update && apk add make build-base git
- mkdir -p $GOPATH/$(echo $SRC | rev | cut -d'/' -f2- | rev) # mk src directory tree minus last path
- ln -s $CI_PROJECT_DIR $GOPATH/$SRC; cd $GOPATH/$SRC
- make test
8 changes: 4 additions & 4 deletions {{cookiecutter.name}}/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ ARG GIT_TAG
ARG GIT_DIRTY
ENV BIN_OUTDIR=
ENV BIN_NAME={{cookiecutter.name}}
RUN apk update && apk add build-base libressl-dev
WORKDIR /go/src/{{cookiecutter.pkg}}
COPY ./ /go/src/{{cookiecutter.pkg}}
RUN apk update && apk add build-base git libressl-dev
WORKDIR /usr/src/{{cookiecutter.name}}
COPY ./ /usr/src/{{cookiecutter.name}}
RUN make static

# Stage 2 - Final Image
Expand All @@ -21,7 +21,7 @@ RUN apk update \
&& rm -rf /var/cache/apk/* \
&& addgroup {{cookiecutter.name}} \
&& adduser -D -H -G {{cookiecutter.name}} {{cookiecutter.name}}
COPY --from=builder /{{cookiecutter.name}} /usr/bin/{{cookiecutter.name}}
COPY --from=builder /usr/src/{{cookiecutter.name}} /usr/bin/{{cookiecutter.name}}
VOLUME ["/etc/{{cookiecutter.name}}"]
ENTRYPOINT ["{{cookiecutter.name}}"]
EXPOSE 5000
Expand Down
14 changes: 0 additions & 14 deletions {{cookiecutter.name}}/Gopkg.toml

This file was deleted.

2 changes: 1 addition & 1 deletion {{cookiecutter.name}}/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# General
CMD :=
PKG := {{cookiecutter.pkg}}
PKG := {{cookiecutter.module}}
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)

# Docker
Expand Down
33 changes: 22 additions & 11 deletions {{cookiecutter.name}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

{{ cookiecutter.description }}

## Setup

## Development

- Go 1.11+
- Dependencies managed with `go mod`

### Setup

These steps will describe how to setup this project for active development. Adjust paths to your desire.

1. Create your directory paths: `mkdir -p ~/{{cookiecutter.gopath}}/src/{{cookiecutter.pkg}}`
2. Set your `GOPATH`: `export GOPATH=~/{{cookiecutter.gopath}}`
3. Change directory to the `src` directory: `cd ~/{{cookiecutter.gopath}}/src/{{cookiecutter.pkg}}`
4. Clone the repository: `git clone {{cookiecutter.origin}} .`
5. Install `dep`: https://github.com/golang/dep/releases
6. Install dependencies (these live in the `vendor/` directory: `dep ensure`
7. Build: `make build`
8. 🍻
1. Clone the repository: `git clone {{cookiecutter.origin}} {{cookiecutter.name}}`
2. Build: `make build`
3. 🍻

### Dependencies

Dependencies are managed using `go mod` (introduced in 1.11), their versions
are tracked in `go.mod`.

To add a dependency:
```
go get url/to/origin
```

## Configuration
### Configuration

Configuration can be provided through a toml file, these are loaded
in order from:
Expand All @@ -31,7 +42,7 @@ in order from:
Alternatively a config file path can be provided through the
-c/--config CLI flag.

### Example {{ cookiecutter.name }}.toml
#### Example {{ cookiecutter.name }}.toml
```toml
[log]
format = "json" # [json|console|discard]
Expand Down
4 changes: 2 additions & 2 deletions {{cookiecutter.name}}/cmd/{{cookiecutter.name}}/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"io"
"os"

"{{cookiecutter.pkg}}/internal/config"
"{{cookiecutter.pkg}}/internal/version"
"{{cookiecutter.module}}/internal/config"
"{{cookiecutter.module}}/internal/version"

"github.com/rs/zerolog"
"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.name}}/cmd/{{cookiecutter.name}}/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"os"

"{{cookiecutter.pkg}}/internal/version"
"{{cookiecutter.module}}/internal/version"

"github.com/spf13/cobra"
)
Expand Down
8 changes: 8 additions & 0 deletions {{cookiecutter.name}}/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module {{cookiecutter.module}}

require (
github.com/rs/zerolog v1.9.1
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.2.1
)

0 comments on commit 83923c2

Please sign in to comment.