Skip to content

Commit a83787b

Browse files
authored
Initial commit
0 parents  commit a83787b

19 files changed

+944
-0
lines changed

.github/workflows/changelog.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Changelog
2+
on:
3+
workflow_dispatch:
4+
5+
jobs:
6+
build:
7+
name: Build
8+
runs-on: ubuntu-latest
9+
steps:
10+
# Fetch depth 0 is required for Changelog generation
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Create changelog text
17+
id: changelog
18+
uses: loopwerk/tag-changelog@v1
19+
with:
20+
token: ${{ secrets.GITHUB_TOKEN }}
21+
22+
- name: Print changelog
23+
run: |
24+
cat <<EOF
25+
${{ steps.changelog.outputs.changes }}
26+
EOF

.github/workflows/go.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This workflow will test a golang project
2+
3+
name: Go
4+
5+
on:
6+
push:
7+
branches: [ "main" ]
8+
pull_request:
9+
branches: [ "main" ]
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v3
19+
with:
20+
go-version: 1.19
21+
22+
- name: Test
23+
run: make test
24+
25+
- name: Build
26+
run: make all
27+

.github/workflows/release.yml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Release
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
tag:
6+
description: "Version Tag (vX.X.X)"
7+
type: string
8+
required: true
9+
prerelease:
10+
type: boolean
11+
required: false
12+
default: yes
13+
14+
jobs:
15+
build:
16+
name: Build
17+
runs-on: ubuntu-latest
18+
steps:
19+
# Fetch depth 0 is required for Changelog generation
20+
- name: Checkout
21+
uses: actions/checkout@v2
22+
with:
23+
fetch-depth: 0
24+
25+
- name: Setup go
26+
uses: actions/setup-go@v2
27+
with:
28+
go-version: 1.19.5
29+
stable: false
30+
31+
- name: Go Test
32+
run: |
33+
make test
34+
35+
- name: Cross Build
36+
# You may pin to the exact commit or the version.
37+
run: |
38+
make package
39+
- name: Create Tag
40+
uses: negz/create-tag@v1
41+
with:
42+
version: ${{ github.event.inputs.tag }}
43+
message: "create tag"
44+
token: ${{ secrets.GITHUB_TOKEN }}
45+
46+
- name: Create changelog text
47+
id: changelog
48+
uses: loopwerk/tag-changelog@v1
49+
with:
50+
token: ${{ secrets.GITHUB_TOKEN }}
51+
52+
- name: Print changelog
53+
run: |
54+
cat <<EOF
55+
${{ steps.changelog.outputs.changes }}
56+
EOF
57+
- name: Release & Assets
58+
uses: Hs1r1us/[email protected]
59+
env:
60+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
with:
62+
# The name of the tag
63+
tag_name: ${{ github.event.inputs.tag }}
64+
# The name of the release
65+
release_name: ${{ github.event.inputs.tag }}
66+
# Text describing the contents of the tag
67+
body: ${{ steps.changelog.outputs.changes }}
68+
# The path to the asset you want to upload
69+
asset_files: dist/
70+
# `true` to create a draft (unpublished) release, `false` to create a published one. Default: `false`
71+
draft: false
72+
# `true` to identify the release as a prerelease. `false` to identify the release as a full release. Default: `false`
73+
prerelease: ${{ github.event.inputs.prerelease }}
74+

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
*.log
17+
18+
dist/

.vscode/launch.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch App",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${workspaceFolder}/cmd/greet/main.go",
13+
"args": [
14+
"-v",
15+
"Zhang San",
16+
"Lao Wang",
17+
"-c",
18+
"${workspaceFolder}/conf/config.example.toml"
19+
]
20+
}
21+
]
22+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Zijing Zhang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
PROJECT_NAME=greet-app
2+
BIN_NAMES=greet
3+
GOARCHS=amd64 386 arm arm64
4+
GOARCHS_MAC=amd64 arm64
5+
6+
dev: linux
7+
8+
default: all
9+
10+
all: windows linux mac
11+
12+
prepare:
13+
@mkdir -p dist
14+
15+
windows: prepare
16+
for BIN_NAME in $(BIN_NAMES); do \
17+
[ -z "$$BIN_NAME" ] && continue; \
18+
for GOARCH in $(GOARCHS); do \
19+
mkdir -p dist/windows_$$GOARCH; \
20+
OOSG=windows GOARCH=$$GOARCH go build -o dist/windows_$$GOARCH/$$BIN_NAME.exe cmd/$$BIN_NAME/main.go; \
21+
done \
22+
done
23+
24+
linux: prepare
25+
for BIN_NAME in $(BIN_NAMES); do \
26+
[ -z "$$BIN_NAME" ] && continue; \
27+
for GOARCH in $(GOARCHS); do \
28+
mkdir -p dist/linux_$$GOARCH; \
29+
GOOS=linux GOARCH=$$GOARCH go build -o dist/linux_$$GOARCH/$$BIN_NAME cmd/$$BIN_NAME/main.go; \
30+
done \
31+
done
32+
33+
mac: prepare
34+
for BIN_NAME in $(BIN_NAMES); do \
35+
[ -z "$$BIN_NAME" ] && continue; \
36+
for GOARCH in $(GOARCHS_MAC); do \
37+
mkdir -p dist/mac_$$GOARCH; \
38+
GOOS=darwin GOARCH=$$GOARCH go build -o dist/mac_$$GOARCH/$$BIN_NAME cmd/$$BIN_NAME/main.go; \
39+
done \
40+
done
41+
42+
package: all
43+
for GOARCH in $(GOARCHS); do \
44+
zip -q -r dist/$(PROJECT_NAME)-windows-$$GOARCH.zip dist/windows_$$GOARCH/; \
45+
zip -q -r dist/$(PROJECT_NAME)-linux-$$GOARCH.zip dist/linux_$$GOARCH/; \
46+
done
47+
48+
for GOARCH in $(GOARCHS_MAC); do \
49+
zip -q -r dist/$(PROJECT_NAME)-mac-$$GOARCH.zip dist/mac_$$GOARCH/; \
50+
done
51+
52+
ARCH_RELEASE_DIRS=$$(find dist -type d -name "*_*"); \
53+
for ARCH_RELEASE_DIR in $$ARCH_RELEASE_DIRS; do \
54+
cp conf/config.default.toml $$ARCH_RELEASE_DIR/config.toml; \
55+
rm -rfd $$ARCH_RELEASE_DIR; \
56+
done
57+
58+
test:
59+
go test -v ./...
60+
61+
clean:
62+
rm -rfd dist
63+
64+
.PHONY: all, default, clean

README.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Go CLI Boilerplate
2+
3+
A powerful Golang CLI application scaffold integrated with logrus, go-arg, config, testify and Github Action.
4+
5+
## Usage
6+
7+
### 1. Use as template
8+
9+
Click `Use this template` button to create a new repository. Then clone the new repository to your local machine.
10+
11+
### 2. Do some changes
12+
13+
1. Change the package name
14+
+ Replace `example.com/m` with your own package name.
15+
+ The default is `example.com/m` You can replace by `sed`:
16+
17+
```bash
18+
sed -i 's/example.com\/m/your.package.name/g' $(find . -type f)
19+
```
20+
21+
Or use global search and replace in your IDE.
22+
23+
2. Change the module name
24+
+ The default application name is `greet`
25+
+ Rename `cmd/greet` to `cmd/YOUR_APP_NAME`
26+
+ Rename inside `Makefile` for `BIN_NAMES`
27+
+ Rename inside `conf/*.toml` for logger file name
28+
+ Rename insdie `.vscode` for debugging
29+
30+
3. Add more application if needed
31+
+ Add more application in `cmd` folder just like `greet` app.
32+
+ Add more application name in `Makefile` for `BIN_NAMES`, for example:
33+
34+
```makefile
35+
BIN_NAMES=app1 app2
36+
```
37+
38+
4. Enable write permission for workflow
39+
+ Click `Settings` tab of your repository.
40+
+ Select `Action -> General` in left sidebar.
41+
+ Locate `Workflow permissions` section.
42+
+ Check `Read and write permission`
43+
44+
### 3. Release
45+
46+
1. Edit `README.md`. Switch `LICENSE` to your own license.
47+
2. Coding for your application.
48+
3. Run `make dev` to build for your local machine.
49+
4. Run `make test` to run unit tests.
50+
5. Run `make package` to cross compile for different platforms.
51+
6. Click `Action` tab of your repository to see the Github Action workflow. In release workflow, click `Run workflow` button to release your application.
52+
53+
## Run Example
54+
55+
```bash
56+
go run cmd/greet/main.go Jack -v -c ./dist/config.example.toml
57+
```
58+
59+
## Features
60+
61+
+ [x] Logrus logger. JSON format
62+
+ [x] Multiple logging output: file, stdout, stderr
63+
+ [x] Command line args, such as `--verbose` and `--config`
64+
+ [x] Config file, such as `./dist/config.example.toml`
65+
+ [x] Log rotation
66+
67+
Use `| jq` to pretty print JSON log.
68+
69+
Generate structs for your config file: [Toml to Go](https://xuri.me/toml-to-go/)

cmd/greet/app/args.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app
2+
3+
type Args struct {
4+
Names []string `arg:"positional,required" help:"names to greet"`
5+
Seperately bool `arg:"-s,--seperately" help:"greet each name seperately" default:"false"`
6+
Verbose bool `arg:"-v,--verbose" help:"verbose output" default:"false"`
7+
Config string `arg:"-c,--config" help:"config file" default:"config.toml"`
8+
}

0 commit comments

Comments
 (0)