Style: use gofumpt for fmt

This commit is contained in:
Dreamacro 2021-10-10 23:44:09 +08:00
parent 4ce35870fe
commit f1cf7e9269
34 changed files with 78 additions and 84 deletions

View file

@ -30,9 +30,8 @@ jobs:
- name: Get dependencies, run test and static check - name: Get dependencies, run test and static check
run: | run: |
go test ./... go test ./...
go vet ./... go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install honnef.co/go/tools/cmd/staticcheck@latest golangci-lint run --disable-all -E govet -E gofumpt -E megacheck ./...
staticcheck -- $(go list ./...)
- name: Build - name: Build
if: startsWith(github.ref, 'refs/tags/') if: startsWith(github.ref, 'refs/tags/')

View file

@ -112,5 +112,9 @@ $(zip_releases): %.zip : %
all-arch: $(PLATFORM_LIST) $(WINDOWS_ARCH_LIST) all-arch: $(PLATFORM_LIST) $(WINDOWS_ARCH_LIST)
releases: $(gz_releases) $(zip_releases) releases: $(gz_releases) $(zip_releases)
lint:
golangci-lint run --disable-all -E govet -E gofumpt -E megacheck ./...
clean: clean:
rm $(BINDIR)/* rm $(BINDIR)/*

View file

@ -12,8 +12,8 @@ import (
) )
var ( var (
fileMode os.FileMode = 0666 fileMode os.FileMode = 0o666
dirMode os.FileMode = 0755 dirMode os.FileMode = 0o755
) )
type parser = func([]byte) (interface{}, error) type parser = func([]byte) (interface{}, error)

View file

@ -10,9 +10,7 @@ import (
types "github.com/Dreamacro/clash/constant/provider" types "github.com/Dreamacro/clash/constant/provider"
) )
var ( var errVehicleType = errors.New("unsupport vehicle type")
errVehicleType = errors.New("unsupport vehicle type")
)
type healthCheckSchema struct { type healthCheckSchema struct {
Enable bool `provider:"enable"` Enable bool `provider:"enable"`

View file

@ -149,7 +149,6 @@ func TestSetWithExpire(t *testing.T) {
assert.Equal(t, nil, res) assert.Equal(t, nil, res)
assert.Equal(t, time.Time{}, expires) assert.Equal(t, time.Time{}, expires)
assert.Equal(t, false, exist) assert.Equal(t, false, exist)
} }
func TestStale(t *testing.T) { func TestStale(t *testing.T) {

View file

@ -67,7 +67,6 @@ func (d *digest32) bmix(p []byte) (tail []byte) {
} }
func (d *digest32) Sum32() (h1 uint32) { func (d *digest32) Sum32() (h1 uint32) {
h1 = d.h1 h1 = d.h1
var k1 uint32 var k1 uint32

View file

@ -38,7 +38,7 @@ func TestObservable_MultiSubscribe(t *testing.T) {
src := NewObservable(iter) src := NewObservable(iter)
ch1, _ := src.Subscribe() ch1, _ := src.Subscribe()
ch2, _ := src.Subscribe() ch2, _ := src.Subscribe()
var count = atomic.NewInt32(0) count := atomic.NewInt32(0)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)

View file

@ -53,6 +53,7 @@ func (alloc *Allocator) Put(buf []byte) error {
} }
//lint:ignore SA6002 ignore temporarily //lint:ignore SA6002 ignore temporarily
//nolint
alloc.buffers[bits].Put(buf) alloc.buffers[bits].Put(buf)
return nil return nil
} }

View file

@ -12,7 +12,7 @@ import (
func TestBasic(t *testing.T) { func TestBasic(t *testing.T) {
single := NewSingle(time.Millisecond * 30) single := NewSingle(time.Millisecond * 30)
foo := 0 foo := 0
var shardCount = atomic.NewInt32(0) shardCount := atomic.NewInt32(0)
call := func() (interface{}, error) { call := func() (interface{}, error) {
foo++ foo++
time.Sleep(time.Millisecond * 5) time.Sleep(time.Millisecond * 5)

View file

@ -5,8 +5,10 @@ import (
"testing" "testing"
) )
var decoder = NewDecoder(Option{TagName: "test"}) var (
var weakTypeDecoder = NewDecoder(Option{TagName: "test", WeaklyTypedInput: true}) decoder = NewDecoder(Option{TagName: "test"})
weakTypeDecoder = NewDecoder(Option{TagName: "test", WeaklyTypedInput: true})
)
type Baz struct { type Baz struct {
Foo int `test:"foo"` Foo int `test:"foo"`

View file

@ -1,8 +1,6 @@
package dialer package dialer
var ( var DefaultOptions []Option
DefaultOptions []Option
)
type config struct { type config struct {
skipDefault bool skipDefault bool

View file

@ -15,8 +15,10 @@ type Interface struct {
HardwareAddr net.HardwareAddr HardwareAddr net.HardwareAddr
} }
var ErrIfaceNotFound = errors.New("interface not found") var (
var ErrAddrNotFound = errors.New("addr not found") ErrIfaceNotFound = errors.New("interface not found")
ErrAddrNotFound = errors.New("addr not found")
)
var interfaces = singledo.NewSingle(time.Second * 20) var interfaces = singledo.NewSingle(time.Second * 20)

View file

@ -9,8 +9,10 @@ import (
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
) )
var mmdb *geoip2.Reader var (
var once sync.Once mmdb *geoip2.Reader
once sync.Once
)
func LoadFromBytes(buffer []byte) { func LoadFromBytes(buffer []byte) {
once.Do(func() { once.Do(func() {

View file

@ -25,8 +25,10 @@ var nativeEndian = func() binary.ByteOrder {
return binary.LittleEndian return binary.LittleEndian
}() }()
type SocketResolver func(network string, ip net.IP, srcPort int) (inode, uid int, err error) type (
type ProcessNameResolver func(inode, uid int) (name string, err error) SocketResolver func(network string, ip net.IP, srcPort int) (inode, uid int, err error)
ProcessNameResolver func(inode, uid int) (name string, err error)
)
// export for android // export for android
var ( var (

View file

@ -16,7 +16,7 @@ import (
var ( var (
initOnce sync.Once initOnce sync.Once
fileMode os.FileMode = 0666 fileMode os.FileMode = 0o666
defaultCache *CacheFile defaultCache *CacheFile
bucketSelected = []byte("selected") bucketSelected = []byte("selected")
@ -41,7 +41,6 @@ func (c *CacheFile) SetSelected(group, selected string) {
} }
return bucket.Put([]byte(group), []byte(selected)) return bucket.Put([]byte(group), []byte(selected))
}) })
if err != nil { if err != nil {
log.Warnln("[CacheFile] write cache to %s failed: %s", c.db.Path(), err.Error()) log.Warnln("[CacheFile] write cache to %s failed: %s", c.db.Path(), err.Error())
return return

View file

@ -4,7 +4,5 @@ import (
"go.uber.org/atomic" "go.uber.org/atomic"
) )
var ( // StoreSelected is a global switch for storing selected proxy to cache
// StoreSelected is a global switch for storing selected proxy to cache var StoreSelected = atomic.NewBool(true)
StoreSelected = atomic.NewBool(true)
)

View file

@ -12,10 +12,8 @@ const (
domainStep = "." domainStep = "."
) )
var ( // ErrInvalidDomain means insert domain is invalid
// ErrInvalidDomain means insert domain is invalid var ErrInvalidDomain = errors.New("invalid domain")
ErrInvalidDomain = errors.New("invalid domain")
)
// DomainTrie contains the main logic for adding and searching nodes for domain segments. // DomainTrie contains the main logic for adding and searching nodes for domain segments.
// support wildcard domain (e.g *.google.com) // support wildcard domain (e.g *.google.com)

View file

@ -18,7 +18,7 @@ func downloadMMDB(path string) (err error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil { if err != nil {
return err return err
} }
@ -54,7 +54,7 @@ func initMMDB() error {
func Init(dir string) error { func Init(dir string) error {
// initial homedir // initial homedir
if _, err := os.Stat(dir); os.IsNotExist(err) { if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0777); err != nil { if err := os.MkdirAll(dir, 0o777); err != nil {
return fmt.Errorf("can't create config directory %s: %s", dir, err.Error()) return fmt.Errorf("can't create config directory %s: %s", dir, err.Error())
} }
} }
@ -62,7 +62,7 @@ func Init(dir string) error {
// initial config.yaml // initial config.yaml
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) { if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
log.Infoln("Can't find config, create a initial config file") log.Infoln("Can't find config, create a initial config file")
f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil { if err != nil {
return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error()) return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error())
} }

View file

@ -32,6 +32,7 @@ func (inf *ipnetFilter) Match(ip net.IP) bool {
type fallbackDomainFilter interface { type fallbackDomainFilter interface {
Match(domain string) bool Match(domain string) bool
} }
type domainFilter struct { type domainFilter struct {
tree *trie.DomainTrie tree *trie.DomainTrie
} }

View file

@ -14,8 +14,10 @@ import (
D "github.com/miekg/dns" D "github.com/miekg/dns"
) )
type handler func(ctx *context.DNSContext, r *D.Msg) (*D.Msg, error) type (
type middleware func(next handler) handler handler func(ctx *context.DNSContext, r *D.Msg) (*D.Msg, error)
middleware func(next handler) handler
)
func withHosts(hosts *trie.DomainTrie) middleware { func withHosts(hosts *trie.DomainTrie) middleware {
return func(next handler) handler { return func(next handler) handler {

View file

@ -215,7 +215,6 @@ func (r *Resolver) shouldOnlyQueryFallback(m *D.Msg) bool {
} }
func (r *Resolver) ipExchange(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) { func (r *Resolver) ipExchange(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
if matched := r.matchPolicy(m); len(matched) != 0 { if matched := r.matchPolicy(m); len(matched) != 0 {
res := <-r.asyncExchange(ctx, matched, m) res := <-r.asyncExchange(ctx, matched, m)
return res.Msg, res.Error return res.Msg, res.Error

View file

@ -13,14 +13,12 @@ import (
D "github.com/miekg/dns" D "github.com/miekg/dns"
) )
var ( // EnhancedModeMapping is a mapping for EnhancedMode enum
// EnhancedModeMapping is a mapping for EnhancedMode enum var EnhancedModeMapping = map[string]EnhancedMode{
EnhancedModeMapping = map[string]EnhancedMode{
NORMAL.String(): NORMAL, NORMAL.String(): NORMAL,
FAKEIP.String(): FAKEIP, FAKEIP.String(): FAKEIP,
MAPPING.String(): MAPPING, MAPPING.String(): MAPPING,
} }
)
const ( const (
NORMAL EnhancedMode = iota NORMAL EnhancedMode = iota

View file

@ -24,9 +24,7 @@ import (
"github.com/Dreamacro/clash/tunnel" "github.com/Dreamacro/clash/tunnel"
) )
var ( var mux sync.Mutex
mux sync.Mutex
)
func readConfig(path string) ([]byte, error) { func readConfig(path string) ([]byte, error) {
if _, err := os.Stat(path); os.IsNotExist(err) { if _, err := os.Stat(path); os.IsNotExist(err) {

View file

@ -4,9 +4,7 @@ import (
"github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/auth"
) )
var ( var authenticator auth.Authenticator
authenticator auth.Authenticator
)
func Authenticator() auth.Authenticator { func Authenticator() auth.Authenticator {
return authenticator return authenticator

View file

@ -5,16 +5,14 @@ import (
"errors" "errors"
) )
var ( // LogLevelMapping is a mapping for LogLevel enum
// LogLevelMapping is a mapping for LogLevel enum var LogLevelMapping = map[string]LogLevel{
LogLevelMapping = map[string]LogLevel{
ERROR.String(): ERROR, ERROR.String(): ERROR,
WARNING.String(): WARNING, WARNING.String(): WARNING,
INFO.String(): INFO, INFO.String(): INFO,
DEBUG.String(): DEBUG, DEBUG.String(): DEBUG,
SILENT.String(): SILENT, SILENT.String(): SILENT,
} }
)
const ( const (
DEBUG LogLevel = iota DEBUG LogLevel = iota

View file

@ -27,12 +27,10 @@ var (
ErrSmallBuffer = errors.New("buffer too small") ErrSmallBuffer = errors.New("buffer too small")
) )
var ( var defaultHeader = http.Header{
defaultHeader = http.Header{
"content-type": []string{"application/grpc"}, "content-type": []string{"application/grpc"},
"user-agent": []string{"grpc-go/1.36.0"}, "user-agent": []string{"grpc-go/1.36.0"},
} }
)
type DialFn = func(network, addr string) (net.Conn, error) type DialFn = func(network, addr string) (net.Conn, error)

View file

@ -78,6 +78,7 @@ func (to *TLSObfs) Read(b []byte) (int, error) {
// type + ver = 3 // type + ver = 3
return to.read(b, 3) return to.read(b, 3)
} }
func (to *TLSObfs) Write(b []byte) (int, error) { func (to *TLSObfs) Write(b []byte) (int, error) {
length := len(b) length := len(b)
for i := 0; i < length; i += chunkSize { for i := 0; i < length; i += chunkSize {

View file

@ -20,6 +20,7 @@ func (sc *snellCipher) SaltSize() int { return 16 }
func (sc *snellCipher) Encrypter(salt []byte) (cipher.AEAD, error) { func (sc *snellCipher) Encrypter(salt []byte) (cipher.AEAD, error) {
return sc.makeAEAD(snellKDF(sc.psk, salt, sc.KeySize())) return sc.makeAEAD(snellKDF(sc.psk, salt, sc.KeySize()))
} }
func (sc *snellCipher) Decrypter(salt []byte) (cipher.AEAD, error) { func (sc *snellCipher) Decrypter(salt []byte) (cipher.AEAD, error) {
return sc.makeAEAD(snellKDF(sc.psk, salt, sc.KeySize())) return sc.makeAEAD(snellKDF(sc.psk, salt, sc.KeySize()))
} }

View file

@ -30,9 +30,7 @@ const (
Version byte = 1 Version byte = 1
) )
var ( var endSignal = []byte{}
endSignal = []byte{}
)
type Snell struct { type Snell struct {
net.Conn net.Conn

View file

@ -184,7 +184,7 @@ func isReservedIP(ip net.IP) bool {
} }
func readUntilNull(r io.Reader) ([]byte, error) { func readUntilNull(r io.Reader) ([]byte, error) {
var buf = &bytes.Buffer{} buf := &bytes.Buffer{}
var data [1]byte var data [1]byte
for { for {

View file

@ -14,8 +14,10 @@ import (
"github.com/Dreamacro/clash/transport/ssr/tools" "github.com/Dreamacro/clash/transport/ssr/tools"
) )
type hmacMethod func(key, data []byte) []byte type (
type hashDigestMethod func([]byte) []byte hmacMethod func(key, data []byte) []byte
hashDigestMethod func([]byte) []byte
)
func init() { func init() {
register("auth_aes128_sha1", newAuthAES128SHA1, 9) register("auth_aes128_sha1", newAuthAES128SHA1, 9)

View file

@ -92,7 +92,7 @@ func sealVMessAEADHeader(key [16]byte, data []byte, t time.Time) []byte {
payloadHeaderAEADEncrypted = payloadHeaderAEAD.Seal(nil, payloadHeaderAEADNonce, data, generatedAuthID[:]) payloadHeaderAEADEncrypted = payloadHeaderAEAD.Seal(nil, payloadHeaderAEADNonce, data, generatedAuthID[:])
} }
var outputBuffer = &bytes.Buffer{} outputBuffer := &bytes.Buffer{}
outputBuffer.Write(generatedAuthID[:]) outputBuffer.Write(generatedAuthID[:])
outputBuffer.Write(payloadHeaderLengthAEADEncrypted) outputBuffer.Write(payloadHeaderLengthAEADEncrypted)

View file

@ -28,6 +28,7 @@ type websocketConn struct {
rMux sync.Mutex rMux sync.Mutex
wMux sync.Mutex wMux sync.Mutex
} }
type websocketWithEarlyDataConn struct { type websocketWithEarlyDataConn struct {
net.Conn net.Conn
underlay net.Conn underlay net.Conn

View file

@ -8,14 +8,12 @@ import (
type TunnelMode int type TunnelMode int
var ( // ModeMapping is a mapping for Mode enum
// ModeMapping is a mapping for Mode enum var ModeMapping = map[string]TunnelMode{
ModeMapping = map[string]TunnelMode{
Global.String(): Global, Global.String(): Global,
Rule.String(): Rule, Rule.String(): Rule,
Direct.String(): Direct, Direct.String(): Direct,
} }
)
const ( const (
Global TunnelMode = iota Global TunnelMode = iota