-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangyile
committed
Dec 1, 2024
1 parent
ba01883
commit 72f37a3
Showing
15 changed files
with
472 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
name: create-release | ||
|
||
on: | ||
push: | ||
branches: | ||
- main # 监听 main 分支的 push 操作(编译和测试/代码检查) | ||
tags: | ||
- 'v*' # 监听以 'v' 开头的标签的 push 操作(发布 Release) | ||
|
||
jobs: | ||
lint: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.23.x" | ||
- uses: actions/checkout@v4 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v6 | ||
with: | ||
version: latest | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go: [ "1.22.x", "1.23.x" ] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Run test | ||
run: make test COVERAGE_DIR=/tmp/coverage | ||
|
||
- name: Send goveralls coverage | ||
uses: shogo82148/actions-goveralls@v1 | ||
with: | ||
path-to-profile: /tmp/coverage/combined.txt | ||
flag-name: Go-${{ matrix.go }} | ||
parallel: true | ||
|
||
check-coverage: | ||
name: Check coverage | ||
needs: [ test ] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: shogo82148/actions-goveralls@v1 | ||
with: | ||
parallel-finished: true | ||
|
||
# 发布 Release | ||
release: | ||
name: Release a new version | ||
needs: [ lint, test ] | ||
runs-on: ubuntu-latest | ||
# 仅在推送标签时执行 | ||
if: ${{ success() && startsWith(github.ref, 'refs/tags/v') }} | ||
steps: | ||
# 1. 检出代码 | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
# 2. 创建 Release 和上传源码包 | ||
- name: Create Release | ||
uses: softprops/action-gh-release@v2 | ||
with: | ||
generate_release_notes: true | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 yangyile-yyle88 | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
COVERAGE_DIR ?= .coverage | ||
|
||
# cp from: https://github.com/yyle88/erero/blob/aacef44379ac6c5e3c831d1df6b47de10d731a88/Makefile#L4 | ||
test: | ||
@-rm -r $(COVERAGE_DIR) | ||
@mkdir $(COVERAGE_DIR) | ||
make test-with-flags TEST_FLAGS='-v -race -covermode atomic -coverprofile $$(COVERAGE_DIR)/combined.txt -bench=. -benchmem -timeout 20m' | ||
|
||
test-with-flags: | ||
@go test $(TEST_FLAGS) ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# formatgo | ||
|
||
`formatgo` 是一个 Go 包,用于格式化 Go 源代码,无论是字节切片、字符串还是文件,甚至是包含 Go 文件的整个目录。 | ||
|
||
## 安装 | ||
|
||
你可以通过以下命令安装 `formatgo` 包: | ||
|
||
```bash | ||
go get github.com/yyle88/formatgo | ||
``` | ||
|
||
## 使用方法 | ||
|
||
该包提供了多个函数来格式化 Go 代码,下面是主要的函数及其用法: | ||
|
||
### `FormatBytes` | ||
|
||
从字节切片格式化 Go 源代码。 | ||
|
||
```go | ||
formattedCode, err := formatgo.FormatBytes(code []byte) | ||
``` | ||
|
||
- `code`: Go 源代码(字节切片)。 | ||
- 返回值:格式化后的代码(字节切片)或者格式化出错时的错误。 | ||
|
||
### `FormatCode` | ||
|
||
从字符串格式化 Go 源代码。 | ||
|
||
```go | ||
formattedCode, err := formatgo.FormatCode(code string) | ||
``` | ||
|
||
- `code`: Go 源代码(字符串)。 | ||
- 返回值:格式化后的代码(字符串)或者格式化出错时的错误。 | ||
|
||
### `FormatFile` | ||
|
||
格式化指定路径下的 Go 源代码文件。 | ||
|
||
```go | ||
err := formatgo.FormatFile(path string) | ||
``` | ||
|
||
- `path`: Go 源代码文件的路径。 | ||
- 返回值:格式化失败时的错误。 | ||
|
||
### `FormatRoot` | ||
|
||
格式化指定根目录及其子目录下的所有 Go 源代码文件。 | ||
|
||
```go | ||
err := formatgo.FormatRoot(root string) | ||
``` | ||
|
||
- `root`: 要开始格式化的根目录路径。 | ||
- 返回值:格式化过程中发生错误时的错误。 | ||
|
||
## 示例 | ||
|
||
以下是一个简单的例子,演示如何从字符串格式化 Go 代码: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yyle88/formatgo" | ||
) | ||
|
||
func main() { | ||
code := `package main | ||
import "fmt" | ||
func main() {fmt.Println("Hello, world!")}` | ||
|
||
formattedCode, err := formatgo.FormatCode(code) | ||
if err != nil { | ||
fmt.Println("格式化代码时出错:", err) | ||
return | ||
} | ||
|
||
fmt.Println("格式化后的代码:", formattedCode) | ||
} | ||
``` | ||
|
||
## 许可证 | ||
|
||
`formatgo` 是开源项目,采用 MIT 许可证。详情请查看 LICENSE 文件。 | ||
|
||
## 贡献与支持 | ||
|
||
欢迎通过提交 pull request 或报告问题来贡献此项目。 | ||
|
||
如果你觉得这个包对你有帮助,请在 GitHub 上给个 ⭐,感谢支持!!! | ||
|
||
**感谢你的支持!** | ||
|
||
**祝编程愉快!** 🎉 | ||
|
||
Give me stars. Thank you!!! |
Oops, something went wrong.