feat: modify pkg

This commit is contained in:
Rogee
2024-11-28 19:30:36 +08:00
parent 900b260698
commit 554db07758
14 changed files with 100 additions and 241 deletions

View File

@@ -1,164 +0,0 @@
linters-settings:
forbidigo:
# Forbid the following identifiers (list of regexp).
# Default: ["^(fmt\\.Print(|f|ln)|print|println)$"]
forbid:
- ^print.*$
- 'fmt\.Print.*'
# Optionally put comments at the end of the regex, surrounded by `(# )?`
# Escape any special characters.
- 'fmt\.Print.*(# Do not commit print statements\.)?'
# Exclude godoc examples from forbidigo checks.
# Default: true
# exclude_godoc_examples: false
gofumpt:
# Select the Go version to target.
# Default: "1.15"
# Deprecated: use the global `run.go` instead.
lang-version: "1.18"
# Module path which contains the source code being formatted.
# Default: ""
# Choose whether to use the extra rules.
# Default: false
govet:
check-shadowing: true
# enable:
# - fieldalignment
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
golint:
min-confidence: 0
gocyclo:
min-complexity: 32
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
depguard:
list-type: blacklist
packages:
# logging is allowed only by logutils.Log, logrus
# is allowed to use only in logutils package
- github.com/sirupsen/logrus
misspell:
locale: US
lll:
line-length: 200
goimports:
local-prefixes: git.gobies.org
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
- yodaStyleExpr
- unnamedResult
# - dupImport # https://github.com/go-critic/go-critic/issues/845
- commentedOutCode
- importShadow
- appendCombine
- ifElseChain
- typeAssertChain
- builtinShadow
gosec:
excludes:
- G404
- G501
- G401
revive:
ignore-generated-header: true
severity: warning
rules:
- name: atomic
- name: line-length-limit
severity: error
arguments: [200]
linters:
# enable:
# - errcheck
# - goimports
# - golint
# - govet
# - staticcheck
# - gocyclo
# - maligned
# - goconst
# - depguard
# - misspell
# - lll
# - gocritic
# disable-all: true
enable:
- gocritic
- gocyclo
- lll
- goconst
- misspell
- govet
- errcheck
- forbidigo
# - staticcheck
- unused
# - maligned
- gosimple
# - structcheck
# - varcheck
- ineffassign
# - deadcode
- typecheck
# - golint
- revive
- gosec
- unconvert
# - goimports
- depguard
- prealloc
# - scopelint
- whitespace
- revive
- nilnil
run:
go: '1.18'
# default concurrency is a available CPU number
concurrency: 20
# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 10m
# exit code when at least one issue was found, default is 1
issues-exit-code: 1
# include test files or not, default is true
tests: false
skip-dirs:
- vendor/(passes)
# - third_party/(checker|passes)
golint:
# minimal confidence for issues, default is 0.8
min-confidence: 0.5
issues:
exclude-use-default: true
max-issues-per-linter: 10
max-same-issues: 0
# exclude:
# - EXC0002
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
# service:
# golangci-lint-version: 1.17.x # use the fixed version to not introduce new linters unexpectedly
# prepare:
# - echo "here I can run custom commands, but no preparation needed for this repo"

View File

@@ -152,9 +152,9 @@ module/
"github.com/rogeecn/atom"
"github.com/rogeecn/atom-addons/providers/swagger"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/contracts"
"github.com/rogeecn/atom/utils/opt"
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/contracts"
"git.ipao.vip/rogeecn/atom/utils/opt"
)
func Providers() container.Providers {

View File

@@ -3,9 +3,9 @@ package atom
import (
"fmt"
"github.com/atom-providers/config"
"git.ipao.vip/rogeecn/atom/config"
"git.ipao.vip/rogeecn/atom/container"
"github.com/pkg/errors"
"github.com/rogeecn/atom/container"
"github.com/spf13/cobra"
"go.uber.org/dig"
)

View File

@@ -7,7 +7,7 @@ import (
"os"
"os/exec"
"github.com/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/container"
"github.com/spf13/cobra"
)

57
config/config.go Normal file
View File

@@ -0,0 +1,57 @@
package config
import (
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/rogeecn/atom/container"
"github.com/spf13/viper"
)
func Load(file, app string) (*viper.Viper, error) {
v := viper.NewWithOptions(viper.KeyDelimiter("_"))
v.AutomaticEnv()
if file == "" {
v.SetConfigType("toml")
v.SetConfigName(app + ".toml")
paths := []string{"."}
// execute path
execPath, err := os.Executable()
if err == nil {
paths = append(paths, filepath.Dir(execPath))
}
// home path
homePath, err := os.UserHomeDir()
if err == nil {
paths = append(paths, homePath, homePath+"/"+app, homePath+"/.config", homePath+"/.config/"+app)
}
paths = append(paths, "/etc", "/etc/"+app, "/usr/local/etc", "/usr/local/etc/"+app)
log.Println("try load config from paths:", paths)
for _, path := range paths {
v.AddConfigPath(path)
}
} else {
v.SetConfigFile(file)
}
err := v.ReadInConfig()
log.Println("use config file:", v.ConfigFileUsed())
if err != nil {
return nil, errors.Wrap(err, "config file read error")
}
err = container.Container.Provide(func() (*viper.Viper, error) {
return v, nil
})
if err != nil {
return nil, err
}
return v, nil
}

View File

@@ -7,7 +7,7 @@ import (
"os/signal"
"syscall"
"github.com/rogeecn/atom/utils/opt"
"git.ipao.vip/rogeecn/atom/utils/opt"
"github.com/spf13/viper"
"go.uber.org/dig"
)

View File

View File

@@ -1,6 +0,0 @@
package contracts
type MicroService interface {
Serve() error
GetEngine() any
}

View File

@@ -1,12 +0,0 @@
package contracts
import "gorm.io/gorm"
// Migration route interface
type Migration interface {
ID() string
Up(tx *gorm.DB) error
Down(tx *gorm.DB) error
}
type MigrationProvider func() Migration

View File

@@ -1,13 +0,0 @@
package contracts
import (
"github.com/brianvoe/gofakeit/v6"
"gorm.io/gorm"
)
// Migration route interface
type Seeder interface {
Run(*gofakeit.Faker, *gorm.DB)
}
type SeederProvider func() Seeder

5
go.mod
View File

@@ -1,11 +1,12 @@
module github.com/rogeecn/atom
module git.ipao.vip/rogeecn/atom
go 1.19
go 1.22
require (
github.com/atom-providers/config v0.0.0-20230801062037-db91396e3287
github.com/brianvoe/gofakeit/v6 v6.23.0
github.com/pkg/errors v0.9.1
github.com/rogeecn/atom v1.0.4
github.com/rogeecn/gen v1.0.15
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0

9
go.sum
View File

@@ -71,6 +71,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
@@ -83,6 +84,7 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
@@ -130,6 +132,7 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@@ -173,9 +176,11 @@ github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgSh
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
@@ -197,10 +202,13 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogeecn/atom v1.0.4 h1:GT9XL+jCCu7a4Fmu8/TsZBzSbdvF+zzsSz12E1ZBL5Q=
github.com/rogeecn/atom v1.0.4/go.mod h1:uT3tmB8pALm1NaNRcd6pVy0IR/fO8H5eZrWrFlGmPoU=
github.com/rogeecn/gen v1.0.15 h1:B2c8DegRLBJZv1FnS9sy5nJ2jPVFGDwuJQNKDsxAQ6U=
github.com/rogeecn/gen v1.0.15/go.mod h1:2bsmE6voSNriUNoCRRVye/y6tcnu79gkI/RGt0asmvU=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
@@ -545,6 +553,7 @@ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

View File

@@ -1,8 +0,0 @@
package cli
func Tap[T any](cmd *T, funcs ...func(*T)) *T {
for _, f := range funcs {
f(cmd)
}
return cmd
}

View File

@@ -1,15 +1,10 @@
package db
import (
"database/sql"
"fmt"
"gorm.io/gorm"
)
func TruncateTable(db *gorm.DB, table string) {
if db.Dialector.Name() == "postgres" {
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", table))
return
}
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s", table))
func TruncateTable(db *sql.DB, table string) {
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", table))
}