diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6e0c900..e49fff4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -16,5 +16,5 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.29 + version: v1.41.1 args: -c .golang-ci.yml \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ae8be7a..9965b07 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,17 +3,23 @@ image: tetafro/golang-gcc:1.15-alpine stages: - lint + - build - test - release lint: stage: lint before_script: - - wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.35.2 + - wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.41.1 script: - ./bin/golangci-lint run -c .golang-ci.yml allow_failure: true +build: + stage: build + script: + - go build + test: stage: test script: diff --git a/.golang-ci.yml b/.golang-ci.yml index a558bdc..828d3c5 100644 --- a/.golang-ci.yml +++ b/.golang-ci.yml @@ -4,4 +4,7 @@ linters-settings: linters: enable-all: true disable: - - testpackage \ No newline at end of file + - testpackage + - forbidigo + - paralleltest + - exhaustivestruct \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 05440c9..6ca4408 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,16 +1,24 @@ repos: -- repo: git://github.com/dnephin/pre-commit-golang - rev: master - hooks: - - id: go-fmt - - id: go-vet - - id: go-lint - - id: go-imports - - id: go-cyclo - args: [-over=15] - - id: no-go-testing - - id: golangci-lint - - id: go-critic - - id: go-unit-tests - - id: go-build - - id: go-mod-tidy \ No newline at end of file + # golang pre commits + - repo: git://github.com/dnephin/pre-commit-golang + rev: master + hooks: + - id: go-fmt + - id: go-vet + - id: go-lint + - id: go-imports + - id: go-cyclo + args: [-over=15] + - id: no-go-testing + - id: golangci-lint + args: ["--skip-dirs=vendor -c .golang-ci.yml ."] + - id: go-critic + - id: go-unit-tests + - id: go-build + - id: go-mod-tidy + + # gitlab ci lint + - repo: https://github.com/FalcoSuessgott/lint-gitlab-ci + rev: v0.0.4 + hooks: + - id: gitlab-ci diff --git a/README.md b/README.md index 8ec9061..afd2a12 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,8 @@ A general purpose project template for golang CLI applications * [Demo Application](#demo-application) * [Makefile Targets](#makefile-targets) * [Contribute](#contribute) - * [Ideas](#ideas) - + @@ -22,11 +21,11 @@ This template serves as a starting point for golang commandline applications it # Features - [goreleaser](https://goreleaser.com/) with `deb.` and `.rpm` package releasing - [golangci-lint](https://golangci-lint.run/) for linting and formatting -- [Github Actions](.github/worflows) Stages (Linting, Testing, Releasing) -- [Gitlab CI](.gitlab-ci.yml) Configuration (Linting, Testing, Releasing) +- [Github Actions](.github/worflows) Stages (Lint, Test, Build, Release) +- [Gitlab CI](.gitlab-ci.yml) Configuration (Lint, Test, Build, Release) - [cobra](https://cobra.dev/) example setup including tests - [Makefile](Makefile) - with various useful targets and documentation (see Makefile Targets) -- [Github Pages](_config.yml) using [jekyll-theme-minimal](https://github.com/pages-themes/minimal) (checkout [https://falcosuessgott.github.io/golang-cli-template/](https://falcosuessgott.github.io/)golang-cli-template/) +- [Github Pages](_config.yml) using [jekyll-theme-minimal](https://github.com/pages-themes/minimal) (checkout [https://falcosuessgott.github.io/golang-cli-template/](https://falcosuessgott.github.io/golang-cli-template/)) - [pre-commit-hooks](https://pre-commit.com/) for formatting and validating code before committing # Project Layout @@ -62,7 +61,7 @@ Use "golang-cli-template [command] --help" for more information about a command. ``` ```sh -$> golang-cli-template example 2 5 -a +$> golang-cli-template example 2 5 -a 7 $> golang-cli-template example 2 5 -m @@ -70,22 +69,22 @@ $> golang-cli-template example 2 5 -m ``` # Makefile Targets -``` +```sh $> make build build golang binary +clean clean up environment cover display test coverage -deps clean go.mod +docker-build dockerize golang application fmtcheck run gofmt and print detected files fmt format go files help list makefile targets +install install golang binary lint-fix fix lint lint go files +pre-commit run pre-commit hooks +run run the app +test run go tests ``` # 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 diff --git a/cmd/example.go b/cmd/example.go index 112dc23..a5a120c 100644 --- a/cmd/example.go +++ b/cmd/example.go @@ -56,12 +56,12 @@ func (o *exampleOptions) run(cmd *cobra.Command, args []string) error { } func (o *exampleOptions) parseArgs(args []string) ([]int, error) { - values := make([]int, 2) + values := make([]int, 2) //nolint: gomnd for i, a := range args { v, err := convert.ToInteger(a) if err != nil { - return nil, err + return nil, fmt.Errorf("error converting to integer: %w", err) } values[i] = v diff --git a/cmd/root.go b/cmd/root.go index ecce3b3..1e290a6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,6 +12,7 @@ func newRootCmd(version string) *cobra.Command { Short: "golang-cli project template demo application", RunE: func(cmd *cobra.Command, args []string) error { fmt.Println(cmd.UsageString()) + return nil }, } @@ -24,5 +25,9 @@ func newRootCmd(version string) *cobra.Command { // Execute invokes the command. func Execute(version string) error { - return newRootCmd(version).Execute() + if err := newRootCmd(version).Execute(); err != nil { + return fmt.Errorf("error executing root command: %w", err) + } + + return nil } diff --git a/install.sh b/install.sh index ee97297..f974aed 100644 --- a/install.sh +++ b/install.sh @@ -6,10 +6,12 @@ read -rp "GitHub Username: " user read -rp "Projectname: " projectname git clone git@github.com:FalcoSuessgott/golang-cli-template.git "$projectname" -cd $projectname +cd "$projectname" rm -rf .git find . -type f -exec sed -i "s/FalcoSuessgott\/golang-cli-template/$user\/$projectname/g" {} + git init git add . git commit -m "initial commit" git remote add origin "git@github.com:$user/$projectname.git" + +echo "template successfully installed."