feat: adjust fiber route generation
This commit is contained in:
150
pkg/swag/format/format.go
Normal file
150
pkg/swag/format/format.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.ipao.vip/rogeecn/atomctl/pkg/swag"
|
||||
)
|
||||
|
||||
// Format implements `fmt` command for formatting swag comments in Go source
|
||||
// files.
|
||||
type Format struct {
|
||||
formatter *swag.Formatter
|
||||
|
||||
// exclude exclude dirs and files in SearchDir
|
||||
exclude map[string]bool
|
||||
}
|
||||
|
||||
// New creates a new Format instance
|
||||
func New() *Format {
|
||||
return &Format{
|
||||
exclude: map[string]bool{},
|
||||
formatter: swag.NewFormatter(),
|
||||
}
|
||||
}
|
||||
|
||||
// Config specifies configuration for a format run
|
||||
type Config struct {
|
||||
// SearchDir the swag would be parse
|
||||
SearchDir string
|
||||
|
||||
// excludes dirs and files in SearchDir,comma separated
|
||||
Excludes string
|
||||
|
||||
// MainFile (DEPRECATED)
|
||||
MainFile string
|
||||
}
|
||||
|
||||
var defaultExcludes = []string{"docs", "vendor"}
|
||||
|
||||
// Build runs formatter according to configuration in config
|
||||
func (f *Format) Build(config *Config) error {
|
||||
searchDirs := strings.Split(config.SearchDir, ",")
|
||||
for _, searchDir := range searchDirs {
|
||||
if _, err := os.Stat(searchDir); os.IsNotExist(err) {
|
||||
return fmt.Errorf("fmt: %w", err)
|
||||
}
|
||||
for _, d := range defaultExcludes {
|
||||
f.exclude[filepath.Join(searchDir, d)] = true
|
||||
}
|
||||
}
|
||||
for _, fi := range strings.Split(config.Excludes, ",") {
|
||||
if fi = strings.TrimSpace(fi); fi != "" {
|
||||
f.exclude[filepath.Clean(fi)] = true
|
||||
}
|
||||
}
|
||||
for _, searchDir := range searchDirs {
|
||||
err := filepath.Walk(searchDir, f.visit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Format) visit(path string, fileInfo os.FileInfo, err error) error {
|
||||
if fileInfo.IsDir() && f.excludeDir(path) {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if f.excludeFile(path) {
|
||||
return nil
|
||||
}
|
||||
if err := f.format(path); err != nil {
|
||||
return fmt.Errorf("fmt: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Format) excludeDir(path string) bool {
|
||||
return f.exclude[path] ||
|
||||
filepath.Base(path)[0] == '.' &&
|
||||
len(filepath.Base(path)) > 1 // exclude hidden folders
|
||||
}
|
||||
|
||||
func (f *Format) excludeFile(path string) bool {
|
||||
return f.exclude[path] ||
|
||||
strings.HasSuffix(strings.ToLower(path), "_test.go") ||
|
||||
filepath.Ext(path) != ".go"
|
||||
}
|
||||
|
||||
func (f *Format) format(path string) error {
|
||||
original, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contents := make([]byte, len(original))
|
||||
copy(contents, original)
|
||||
formatted, err := f.formatter.Format(path, contents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if bytes.Equal(original, formatted) {
|
||||
// Skip write if no change
|
||||
return nil
|
||||
}
|
||||
return write(path, formatted)
|
||||
}
|
||||
|
||||
func write(path string, contents []byte) error {
|
||||
originalFileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.CreateTemp(filepath.Dir(path), filepath.Base(path))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
if _, err := f.Write(contents); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(f.Name(), originalFileInfo.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(f.Name(), path)
|
||||
}
|
||||
|
||||
// Run the format on src and write the result to dst.
|
||||
func (f *Format) Run(src io.Reader, dst io.Writer) error {
|
||||
contents, err := io.ReadAll(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := f.formatter.Format("", contents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r := bytes.NewReader(result)
|
||||
if _, err := io.Copy(dst, r); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
151
pkg/swag/format/format_test.go
Normal file
151
pkg/swag/format/format_test.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFormat_Format(t *testing.T) {
|
||||
fx := setup(t)
|
||||
assert.NoError(t, New().Build(&Config{SearchDir: fx.basedir}))
|
||||
assert.True(t, fx.isFormatted("main.go"))
|
||||
assert.True(t, fx.isFormatted("api/api.go"))
|
||||
}
|
||||
|
||||
func TestFormat_PermissionsPreserved(t *testing.T) {
|
||||
fx := setup(t)
|
||||
|
||||
originalFileInfo, err := os.Stat(filepath.Join(fx.basedir, "main.go"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.NoError(t, New().Build(&Config{SearchDir: fx.basedir}))
|
||||
assert.True(t, permissionsEqual(t, filepath.Join(fx.basedir, "main.go"), originalFileInfo.Mode()))
|
||||
assert.True(t, permissionsEqual(t, filepath.Join(fx.basedir, "api/api.go"), originalFileInfo.Mode()))
|
||||
}
|
||||
|
||||
func TestFormat_ExcludeDir(t *testing.T) {
|
||||
fx := setup(t)
|
||||
assert.NoError(t, New().Build(&Config{
|
||||
SearchDir: fx.basedir,
|
||||
Excludes: filepath.Join(fx.basedir, "api"),
|
||||
}))
|
||||
assert.False(t, fx.isFormatted("api/api.go"))
|
||||
}
|
||||
|
||||
func TestFormat_ExcludeFile(t *testing.T) {
|
||||
fx := setup(t)
|
||||
assert.NoError(t, New().Build(&Config{
|
||||
SearchDir: fx.basedir,
|
||||
Excludes: filepath.Join(fx.basedir, "main.go"),
|
||||
}))
|
||||
assert.False(t, fx.isFormatted("main.go"))
|
||||
}
|
||||
|
||||
func TestFormat_DefaultExcludes(t *testing.T) {
|
||||
fx := setup(t)
|
||||
assert.NoError(t, New().Build(&Config{SearchDir: fx.basedir}))
|
||||
assert.False(t, fx.isFormatted("api/api_test.go"))
|
||||
assert.False(t, fx.isFormatted("docs/docs.go"))
|
||||
}
|
||||
|
||||
func TestFormat_ParseError(t *testing.T) {
|
||||
fx := setup(t)
|
||||
os.WriteFile(filepath.Join(fx.basedir, "parse_error.go"), []byte(`package main
|
||||
func invalid() {`), 0644)
|
||||
assert.Error(t, New().Build(&Config{SearchDir: fx.basedir}))
|
||||
}
|
||||
|
||||
func TestFormat_ReadError(t *testing.T) {
|
||||
fx := setup(t)
|
||||
os.Chmod(filepath.Join(fx.basedir, "main.go"), 0)
|
||||
assert.Error(t, New().Build(&Config{SearchDir: fx.basedir}))
|
||||
}
|
||||
|
||||
func TestFormat_WriteError(t *testing.T) {
|
||||
fx := setup(t)
|
||||
os.Chmod(fx.basedir, 0555)
|
||||
assert.Error(t, New().Build(&Config{SearchDir: fx.basedir}))
|
||||
os.Chmod(fx.basedir, 0755)
|
||||
}
|
||||
|
||||
func TestFormat_InvalidSearchDir(t *testing.T) {
|
||||
formatter := New()
|
||||
assert.Error(t, formatter.Build(&Config{SearchDir: "no_such_dir"}))
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
t *testing.T
|
||||
basedir string
|
||||
}
|
||||
|
||||
func setup(t *testing.T) *fixture {
|
||||
fx := &fixture{
|
||||
t: t,
|
||||
basedir: t.TempDir(),
|
||||
}
|
||||
for filename, contents := range testFiles {
|
||||
fullpath := filepath.Join(fx.basedir, filepath.Clean(filename))
|
||||
if err := os.MkdirAll(filepath.Dir(fullpath), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(fullpath, contents, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return fx
|
||||
}
|
||||
|
||||
func (fx *fixture) isFormatted(file string) bool {
|
||||
contents, err := os.ReadFile(filepath.Join(fx.basedir, file))
|
||||
if err != nil {
|
||||
fx.t.Fatal(err)
|
||||
}
|
||||
return !bytes.Equal(testFiles[file], contents)
|
||||
}
|
||||
|
||||
func permissionsEqual(t *testing.T, path string, expectedMode os.FileMode) bool {
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return expectedMode == fileInfo.Mode()
|
||||
}
|
||||
|
||||
var testFiles = map[string][]byte{
|
||||
"api/api.go": []byte(`package api
|
||||
|
||||
import "net/http"
|
||||
|
||||
// @Summary Add a new pet to the store
|
||||
// @Description get string by ID
|
||||
func GetStringByInt(w http.ResponseWriter, r *http.Request) {
|
||||
//write your code
|
||||
}`),
|
||||
"api/api_test.go": []byte(`package api
|
||||
// @Summary API Test
|
||||
// @Description Should not be formatted
|
||||
func TestApi(t *testing.T) {}`),
|
||||
"docs/docs.go": []byte(`package docs
|
||||
// @Summary Documentation package
|
||||
// @Description Should not be formatted`),
|
||||
"main.go": []byte(`package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.ipao.vip/rogeecn/atomctl/pkg/swag/format/testdata/api"
|
||||
)
|
||||
|
||||
// @title Swagger Example API
|
||||
// @version 1.0
|
||||
func main() {
|
||||
http.HandleFunc("/testapi/get-string-by-int/", api.GetStringByInt)
|
||||
}`),
|
||||
"README.md": []byte(`# Format test`),
|
||||
}
|
||||
Reference in New Issue
Block a user