Skip to content

Commit

Permalink
Merge pull request #1 from SimonBaeumer/review
Browse files Browse the repository at this point in the history
Various review fixes
  • Loading branch information
FalcoSuessgott authored Apr 29, 2021
2 parents ed14420 + a50cd11 commit 0a61529
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
golang-cli-template
/.idea

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
8 changes: 4 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ lint:
test:
stage: test
script:
- go test -v -race $(go list ./... | grep -v /vendor/) -v -coverprofile=coverage.out
- go test -v -race "$(go list ./... | grep -v /vendor/)" -v -coverprofile=coverage.out
- go tool cover -func=coverage.out

release:
stage: release
image:
name: goreleaser/goreleaser:latest
name: goreleaser/goreleaser:v0.164.0
entrypoint: ["/bin/bash", "-c"]
only:
refs:
Expand All @@ -32,5 +32,5 @@ release:
GITLAB_TOKEN: $GITLAB_TOKEN

script:
- cd $CI_PROJECT_DIR
- goreleaser release --rm-dist
- cd "$CI_PROJECT_DIR"
- goreleaser release --rm-dist
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ nfpms:
formats:
- deb
- rpm

10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: fmtcheck fmt build lint cover test
.PHONY: fmtcheck fmt build lint cover test deps lint-fix

build:
go build -ldflags "-X github.com/FalcoSuessgott/golang-cli-template/cmd.version=$(shell git describe --abbrev=0 --tags)" -o golang-cli-template
Expand All @@ -17,4 +17,10 @@ fmt:
gofumpt -w -s .

lint:
golangci-lint run -c .golang-ci.yml
golangci-lint run -c .golang-ci.yml

lint-fix:
golangci-lint run -c .golang-ci.yml --fix

deps:
go mod tidy
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ A general purpose project template for golang CLI applications

[![Test](https://github.com/FalcoSuessgott/golang-cli-template/actions/workflows/test.yml/badge.svg)](https://github.com/FalcoSuessgott/golang-cli-template/actions/workflows/test.yml) [![golangci-lint](https://github.com/FalcoSuessgott/golang-cli-template/actions/workflows/lint.yml/badge.svg)](https://github.com/FalcoSuessgott/golang-cli-template/actions/workflows/lint.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/FalcoSuessgott/golang-cli-template)](https://goreportcard.com/report/github.com/FalcoSuessgott/golang-cli-template) [![Go Reference](https://pkg.go.dev/badge/github.com/FalcoSuessgott/golang-cli-template.svg)](https://pkg.go.dev/github.com/FalcoSuessgott/golang-cli-template) [![codecov](https://codecov.io/gh/FalcoSuessgott/golang-cli-template/branch/main/graph/badge.svg?token=Y5K4SID71F)](https://codecov.io/gh/FalcoSuessgott/golang-cli-template)

This template serves as a starting point for golang commandline applications based on the go community default [project-layout](https://github.com/golang-standards/project-layout).
This template serves as a starting point for golang commandline applications it is based on golang projects that I consider high quality and various other useful blog posts that helped me understanding golang better.

# Features
- [goreleaser](https://goreleaser.com/) with `deb.` and `.rpm` package releasing
Expand All @@ -21,19 +21,17 @@ This template serves as a starting point for golang commandline applications bas

# How to use this template
```sh
{
GITHUB_USER="my-github-user"
PROJECT="new-golang-project"
git clone [email protected]:FalcoSuessgott/golang-cli-template.git $PROJECT
cd $PROJECT
git clone [email protected]:FalcoSuessgott/golang-cli-template.git "$PROJECT"
cd "$PROJECT"
rm -rf .git
find . -type f -exec sed -i "s/FalcoSuessgott\/golang-cli-template/$GITHUB_USER\/$PROJECT/g" {} +
make fmt
git init
git add .
git commit -m "initial commit"
git remote add origin [email protected]:$GITHUB_USER/$PROJECT.git
}
git remote add origin "[email protected]:$GITHUB_USER/$PROJECT.git"
```

# Demo Application
Expand Down Expand Up @@ -82,6 +80,10 @@ make fmt
make fmtcheck
```

## deps
```sh
make deps
```
## build
```sh
make build
Expand All @@ -95,4 +97,12 @@ make test
## cover
```sh
make cover
```
```

# Contribute
If you find issues in that setup or have some nice features / improvements, I would welcome an issue or a PR :)


# Ideas
- [ ] implement a `create` subcommand that preconfigures this project setup and all its dependencies
- [ ] introduce viper config examples
7 changes: 3 additions & 4 deletions cmd/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package cmd
import (
"fmt"

"github.com/spf13/cobra"

"github.com/FalcoSuessgott/golang-cli-template/internal/convert"
"github.com/FalcoSuessgott/golang-cli-template/pkg/example"
"github.com/spf13/cobra"
)

const (
Expand Down Expand Up @@ -46,11 +45,11 @@ func (o *exampleOptions) run(cmd *cobra.Command, args []string) error {
}

if o.multiply {
fmt.Fprintf(cmd.OutOrStdout(), "%d", example.Multiply(values[0], values[1]))
fmt.Fprintf(cmd.OutOrStdout(), "%d\n", example.Multiply(values[0], values[1]))
}

if o.add {
fmt.Fprintf(cmd.OutOrStdout(), "%d", example.Add(values[0], values[1]))
fmt.Fprintf(cmd.OutOrStdout(), "%d\n", example.Add(values[0], values[1]))
}

return nil
Expand Down
22 changes: 8 additions & 14 deletions cmd/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

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

func TestExampleCommandMultiply(t *testing.T) {
Expand All @@ -16,16 +18,12 @@ func TestExampleCommandMultiply(t *testing.T) {
cmd.SetOut(b)

err := cmd.Execute()
if err != nil {
t.Fail()
}
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

assert.Equal(t, "6", string(out))
assert.Equal(t, fmt.Sprintln("6"), string(out))
}

func TestExampleCommandMultiplyInvalidArgs(t *testing.T) {
Expand All @@ -47,14 +45,10 @@ func TestExampleAdd(t *testing.T) {
cmd.SetOut(b)

err := cmd.Execute()
if err != nil {
t.Fail()
}
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

assert.Equal(t, "5", string(out))
assert.Equal(t, fmt.Sprintln("5"), string(out))
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func newRootCmd() *cobra.Command {
}

cmd.AddCommand(newVersionCmd()) // version subcommand
cmd.AddCommand(newExampleCmd()) // examble subcommand
cmd.AddCommand(newExampleCmd()) // example subcommand

return cmd
}
Expand Down
9 changes: 3 additions & 6 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

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

func TestRootCommandOutput(t *testing.T) {
Expand All @@ -16,14 +17,10 @@ func TestRootCommandOutput(t *testing.T) {
cmd.SetOut(b)

cmdErr := cmd.Execute()
if cmdErr != nil {
t.Fail()
}
require.NoError(t, cmdErr)

out, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

assert.Equal(t, "golang-cli project template demo application\n\n"+cmd.UsageString(), string(out))
assert.Nil(t, cmdErr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func newVersionCmd() *cobra.Command {
Args: cobra.NoArgs,
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(cmd.OutOrStdout(), "%s", version)
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", version)
},
}
}
12 changes: 5 additions & 7 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

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

func TestVersionCommand(t *testing.T) {
Expand All @@ -15,14 +17,10 @@ func TestVersionCommand(t *testing.T) {
cmd.SetOut(b)

err := cmd.Execute()
if err != nil {
t.Fail()
}
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

assert.Equal(t, version, string(out))
assert.Equal(t, fmt.Sprintln(version), string(out))
}
4 changes: 2 additions & 2 deletions internal/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
)

//nolint
var errConvertionError = func(v interface{}) error {
var errConversionError = func(v interface{}) error {
return fmt.Errorf("cannot convert value %v (type %T) to integer", v, v)
}

// ToInteger converts given value to integer.
func ToInteger(v interface{}) (int, error) {
i, err := strconv.Atoi(v.(string))
if err != nil {
return i, errConvertionError(v)
return i, errConversionError(v)
}

return i, nil
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"
"os"

"github.com/FalcoSuessgott/golang-cli-template/cmd"
)

func main() {
if err := cmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
}

0 comments on commit 0a61529

Please sign in to comment.