Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
luduoxin committed Nov 15, 2023
0 parents commit 5b02331
Show file tree
Hide file tree
Showing 14 changed files with 1,721 additions and 0 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/codeql-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '24 1 * * 0'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# 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@v1

# ℹ️ 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@v1
38 changes: 38 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: goreleaser

on:
push:
# run only against tags
tags:
- '*'

permissions:
contents: write
# packages: write
# issues: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v3
with:
go-version: '>=1.20.2'
cache: true
# More assembly might be required: Docker logins, GPG, etc. It all depends
# on your needs.
- uses: goreleaser/goreleaser-action@v4
with:
# either 'goreleaser' (default) or 'goreleaser-pro':
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro'
# distribution:
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.test
*.out
.idea
.vscode
.DS_Store
dist/
49 changes: 49 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64


archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# api 文档生成和同步工具

## 使用方法

假如 golang 项目已经按照 swag 要求的注释方式做好了注释

### 创建 apidoc 配置文件

在 golang 项目的根目录创建配置文件 .apidoc.yaml,填入 yapi 和 metersphere 的配置信息(注意不要将此配置文件提交到代码库,在 .gitignore 文件中添加忽略),示例格式如下:

```
yapi:
host: <your yapi host>
# 项目
project: <your project name>
# 项目token
token: <your token>
ms:
host: <your ms host>
# 工作空间
workspace: <your workspace>
# 项目
project: <your project name>
# 应用名称
application: <your application name>
accessKey: <your accessKey>
secretKey: <your secretKey>
```

### 生成api文档

在项目根目录或者项目的 app 目录执行如下命令(使用此命令生成 api 文档的前提是安装了 swag 工具,这个命令底层调用了 swag 命令来实现)

```
apidoc build
```

执行此命令后,会在当前目录下生成 docs 文件夹,此文件下面会有 json 和 yaml 格式的接口文档。

### 上传到 yapi 或metersphere

在项目的根目录执行如下命令同时上传到 yapi 和 metersphere

```
apidoc upload
```

上传到 yapi

```
apidoc upload yapi
```

上传到 meterspher

```
apidoc upload ms
```
11 changes: 11 additions & 0 deletions biz/biz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package biz

import "testing"

func TestBuildApiDoc(t *testing.T) {

}

func BenchmarkPathClean(b *testing.B) {

}
50 changes: 50 additions & 0 deletions biz/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package biz

import (
"os"

"gopkg.in/yaml.v3"
)

type config struct {
YAPI yapiConf `yaml:"yapi"`
MS msConf `yaml:"ms"`
}

type yapiConf struct {
Host string `yaml:"host"`
Project string `yaml:"project"`
Token string `yaml:"token"`
}

type msConf struct {
Host string `yaml:"host"`
Workspace string `yaml:"workspace"`
Project string `yaml:"project"`
Application string `yaml:"application"`
AccessKey string `yaml:"accessKey"`
SecretKey string `yaml:"secretKey"`
}

// read configure
func readConfig() (*config, error) {
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
apidoc := pwd + "/.apidoc.yaml"
_, err = os.Stat(apidoc)
if err != nil {
return nil, err
}
f, err := os.Open(apidoc)
if err != nil {
return nil, err
}
conf := &config{}
err = yaml.NewDecoder(f).Decode(conf)
if err != nil {
return nil, err
}
return conf, nil
}
31 changes: 31 additions & 0 deletions biz/swag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package biz

import (
"os"
"os/exec"
)

// BuildApiDoc 生成接口文档
func BuildApiDoc() error {
// 检测当前路径是否存在main.go
cmd := exec.Command("swag", "init")
pwd, err := os.Getwd()
if err != nil {
return err
}
filename := pwd + "/main.go"
_, err = os.Stat(filename)
if err != nil {
filename = pwd + "/app/main.go"
_, err = os.Stat(filename)
if err != nil {
return err
}
cmd.Dir = "./app"
}
err = cmd.Run()
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 5b02331

Please sign in to comment.