From 49fce346986ce83cb31cb9ad23b658328573376b Mon Sep 17 00:00:00 2001 From: thinkgo <49174849+thinkgos@users.noreply.github.com> Date: Tue, 23 Aug 2022 11:11:56 +0800 Subject: [PATCH] Initial commit --- .github/dependabot.yml | 17 +++++ .github/workflows/ci.yml | 65 ++++++++++++++++++ .github/workflows/codeql.yml | 51 ++++++++++++++ .github/workflows/lint.yml | 17 +++++ .github/workflows/pr_review_dog.yml | 25 +++++++ .gitignore | 17 +++++ LICENSE | 21 ++++++ Makefile | 7 ++ README.md | 57 +++++++++++++++ go.mod | 11 +++ go.sum | 15 ++++ math.go | 28 ++++++++ math_test.go | 103 ++++++++++++++++++++++++++++ 13 files changed, 434 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/codeql.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/pr_review_dog.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 math.go create mode 100644 math_test.go diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..ab66525 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,17 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + commit-message: + prefix: "chore" + include: "scope" + + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "daily" + commit-message: + prefix: "chore" + include: "scope" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..22f82af --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,65 @@ +name: Tests + +on: + push: + paths-ignore: + - '**.md' + pull_request: + paths-ignore: + - '**.md' + +env: + GO111MODULE: on + GOPROXY: "https://proxy.golang.org" + +jobs: + build: + name: Test on ${{ matrix.os }} @Go${{ matrix.go-version }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + go-version: ["1.17.x"] + os: [ubuntu-latest, macos-latest, windows-latest] + + steps: + - name: Set up Go ${{ matrix.go-version }} + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + + - name: Print Go environment + id: vars + run: | + printf "Using go at: $(which go)\n" + printf "Go version: $(go version)\n" + printf "\n\nGo environment:\n\n" + go env + printf "\n\nSystem environment:\n\n" + env + # Calculate the short SHA1 hash of the git commit + echo "::set-output name=short_sha::$(git rev-parse --short HEAD)" + echo "::set-output name=go_cache::$(go env GOCACHE)" + + - name: Cache go modules + uses: actions/cache@v3 + with: + path: | + ${{ steps.vars.outputs.go_cache }} + ~/go/pkg/mod + key: ${{ runner.os }}-${{ matrix.go-version }}-go-ci-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.go-version }}-go-ci + + - name: Unit test + run: | + go test -v -race -coverprofile=coverage -covermode=atomic ./... + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + files: ./coverage + flags: unittests + verbose: true \ No newline at end of file diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..6736571 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,51 @@ +name: CodeQL + +on: + push: + paths-ignore: + - '**.md' + pull_request: + paths-ignore: + - '**.md' + schedule: + - cron: '0 5 * * 6' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + # We must fetch at least the immediate parents so that if this is + # a pull request then we can checkout the head. + fetch-depth: 2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..144f699 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,17 @@ +name: Lint + +on: + push: + paths-ignore: + - '**.md' + +jobs: + golangci: + name: Golangci-Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + args: -E goimports -E gocritic -E misspell -E revive \ No newline at end of file diff --git a/.github/workflows/pr_review_dog.yml b/.github/workflows/pr_review_dog.yml new file mode 100644 index 0000000..0ad02f4 --- /dev/null +++ b/.github/workflows/pr_review_dog.yml @@ -0,0 +1,25 @@ +name: Reviewdog + +on: + pull_request: + paths-ignore: + - '**.md' + +jobs: + golangci-lint: + name: runner / golangci-lint + runs-on: ubuntu-latest + steps: + # optionally use a specific version of Go rather than the default one + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.17.x' + + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + + - name: golangci-lint + uses: reviewdog/action-golangci-lint@v2 + with: + golangci_lint_flags: "-E goimports -E gocritic -E misspell -E revive" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..83540aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +.idea +.vscode \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9fa88da --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 thinkgo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..737910f --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: init +init: +ifeq ($(shell uname -s),Darwin) + @grep -r -l go-lib-template * | xargs sed -i "" "s/cicd-go-template/$$(basename `git rev-parse --show-toplevel`)/" +else + @grep -r -l go-lib-template * | xargs sed -i "s/cicd-go-template/$$(basename `git rev-parse --show-toplevel`)/" +endif \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6468bca --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# CD/CD go template +CD/CD go template and test useful or not + +[![GoDoc](https://godoc.org/github.com/things-labs/cicd-go-template?status.svg)](https://godoc.org/github.com/things-labs/cicd-go-template) +[![Go.Dev reference](https://img.shields.io/badge/go.dev-reference-blue?logo=go&logoColor=white)](https://pkg.go.dev/github.com/things-labs/cicd-go-template?tab=doc) +[![codecov](https://codecov.io/gh/things-labs/cicd-go-template/branch/main/graph/badge.svg)](https://codecov.io/gh/things-labs/cicd-go-template) +[![Tests](https://github.com/things-labs/cicd-go-template/actions/workflows/ci.yml/badge.svg)](https://github.com/things-labs/cicd-go-template/actions/workflows/ci.yml) +[![Go Report Card](https://goreportcard.com/badge/github.com/things-labs/cicd-go-template)](https://goreportcard.com/report/github.com/things-labs/cicd-go-template) +[![Licence](https://img.shields.io/github/license/things-labs/cicd-go-template)](https://raw.githubusercontent.com/things-labs/cicd-go-template/main/LICENSE) +[![Tag](https://img.shields.io/github/v/tag/things-labs/cicd-go-template)](https://github.com/things-labs/cicd-go-template/tags) + +This is template that help you to quick implement some library using Go. + +This repository is contains following. + +- CI/CD + - golangci-lint + - go test + - CodeQL Analysis (Go) + - dependabot for github-actions and Go + +## How to use +1. action Use this template and then create a repository +2. replace "things-labs" to your self username using sed(or others) +3. run make init +4: done + +## Features + + +## Usage + +### Installation + +Use go get. +```bash + go get github.com/things-labs/cicd-go-template +``` + +Then import the package into your own code. +```bash + import "github.com/things-labs/cicd-go-template" +``` + +### Example + +[embedmd]:# (_examples/main.go go) +```go + +``` + +## References +- [go-lib-template](https://github.com/skanehira/go-lib-template) + +## License + +This project is under MIT License. See the [LICENSE](LICENSE) file for the full license text. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6ccc2a2 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/things-labs/cicd-go-template + +go 1.17 + +require github.com/stretchr/testify v1.8.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5164829 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/math.go b/math.go new file mode 100644 index 0000000..7b6a544 --- /dev/null +++ b/math.go @@ -0,0 +1,28 @@ +package cpt + +import ( + "math" +) + +func Add(a, b int) int { + return a + b +} + +func Sub(a, b int) int { + return a - b +} + +func Mul(a, b int) int { + return a * b +} + +func Div(a, b int) int { + if b == 0 { + return 0 + } + return a / b +} + +func Pow(a, b int) int { + return int(math.Pow(float64(a), float64(b))) +} diff --git a/math_test.go b/math_test.go new file mode 100644 index 0000000..f86c02f --- /dev/null +++ b/math_test.go @@ -0,0 +1,103 @@ +package cpt + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAdd(t *testing.T) { + type args struct { + a int + b int + } + tests := []struct { + name string + args args + want int + }{ + {"add", args{10, 1}, 11}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, Add(tt.args.a, tt.args.b)) + }) + } +} + +func TestSub(t *testing.T) { + type args struct { + a int + b int + } + tests := []struct { + name string + args args + want int + }{ + {"sub", args{10, 1}, 9}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, Sub(tt.args.a, tt.args.b)) + }) + } +} + +func TestMul(t *testing.T) { + type args struct { + a int + b int + } + tests := []struct { + name string + args args + want int + }{ + {"mul", args{10, 2}, 20}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, Mul(tt.args.a, tt.args.b)) + }) + } +} + +func TestDiv(t *testing.T) { + type args struct { + a int + b int + } + tests := []struct { + name string + args args + want int + }{ + {"div", args{10, 2}, 5}, + {"div 0", args{10, 0}, 0}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, Div(tt.args.a, tt.args.b)) + }) + } +} + +func TestPow(t *testing.T) { + type args struct { + a int + b int + } + tests := []struct { + name string + args args + want int + }{ + {"div", args{10, 2}, 100}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, Pow(tt.args.a, tt.args.b)) + }) + } +}