Skip to content

Commit

Permalink
优化代码和说明文档
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 1, 2024
1 parent ba01883 commit 72f37a3
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 108 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/release.yml
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 }}
21 changes: 21 additions & 0 deletions LICENSE
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.
10 changes: 10 additions & 0 deletions Makefile
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) ./...
111 changes: 109 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,114 @@
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/yyle88/formatgo/release.yml?branch=main&label=BUILD)](https://github.com/yyle88/formatgo/actions/workflows/release.yml?query=branch%3Amain)
[![GoDoc](https://pkg.go.dev/badge/github.com/yyle88/formatgo)](https://pkg.go.dev/github.com/yyle88/formatgo)
[![Coverage Status](https://img.shields.io/coveralls/github/yyle88/formatgo/master.svg)](https://coveralls.io/github/yyle88/formatgo?branch=main)
![Supported Go Versions](https://img.shields.io/badge/Go-1.22%2C%201.23-lightgrey.svg)
[![GitHub Release](https://img.shields.io/github/release/yyle88/formatgo.svg)](https://github.com/yyle88/formatgo/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/yyle88/formatgo)](https://goreportcard.com/report/github.com/yyle88/formatgo)

# formatgo
格式化代码 gofmt 工具 format golang source code 的工具,当然顺带还能整理 import 的引用内容

还是非常重要的
`formatgo` is a Go package that provides utilities for formatting Go source code, whether it's in a byte slice, string, or a file, and even for entire directories containing Go files.

## Installation

To install the `formatgo` package, you can run the following command:

```bash
go get github.com/yyle88/formatgo
```

## Usage

The package provides several functions for formatting Go code. Below are the main functions that you can use:

### `FormatBytes`

Formats Go source code from a byte slice.

```go
formattedCode, err := formatgo.FormatBytes(code []byte)
```

- `code`: The source code as a byte slice.
- Returns the formatted code as a byte slice or an error if something goes wrong.

### `FormatCode`

Formats Go source code from a string.

```go
formattedCode, err := formatgo.FormatCode(code string)
```

- `code`: The source code as a string.
- Returns the formatted code as a string or an error if something goes wrong.

### `FormatFile`

Formats a Go source code file at the given path.

```go
err := formatgo.FormatFile(path string)
```

- `path`: The path to the Go source code file.
- Returns an error if the formatting fails.

### `FormatRoot`

Formats all Go source files in the specified root directory and its subdirectories.

```go
err := formatgo.FormatRoot(root string)
```

- `root`: The root directory to start formatting files from.
- Returns an error if something goes wrong during the formatting process.

## Example

Here’s a simple example of how to format Go code from a string:

```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("Error formatting code:", err)
return
}

fmt.Println("Formatted Code:", formattedCode)
}
```

## License

`formatgo` is open-source and released under the MIT License. See the LICENSE file for more information.

---

## Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

**Thank you for your support!**

**Happy Coding with `formatgo`!** 🎉

Give me stars. Thank you!!!

Expand Down
104 changes: 104 additions & 0 deletions README.zh.md
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!!!
Loading

0 comments on commit 72f37a3

Please sign in to comment.