From 88b1910d990a9fd84b542f04f76bb2e4744aec1e Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Fri, 18 Sep 2026 16:27:52 +0200 Subject: [PATCH] fix: clear all go vet findings (upstream lint debt) - pkg/serializer: rename GobDecode -> DecodeGob (custom signature, not gob.GobDecoder) - aria2 rpc: pointer receivers for Call (were copying sync.Once) - HMACAuth: keyed struct literals (8 sites) - webdav prop: keyed xml.Name literal - request_test: pointer receiver on ClientMock (dead mock, copied sync.Mutex) CI backend job now passes vet. Authored By: TDvorak --- application/migrator/model/init.go | 2 +- pkg/cluster/node.go | 14 ++--- pkg/credmanager/credmanager.go | 6 +- pkg/downloader/aria2/rpc/call.go | 4 +- pkg/downloader/slave/slave.go | 4 +- pkg/filemanager/driver/local/local.go | 8 +-- pkg/filemanager/driver/remote/client.go | 8 +-- pkg/filemanager/driver/remote/remote.go | 2 +- pkg/request/request_test.go | 2 +- pkg/serializer/response.go | 4 +- pkg/util/ip_test.go | 6 +- pkg/util/logger_test.go | 1 + pkg/webdav/internal/xml/marshal.go | 48 +++++++-------- pkg/webdav/internal/xml/read.go | 81 ++++++++++++------------- pkg/webdav/prop.go | 2 +- service/admin/node.go | 2 +- 16 files changed, 95 insertions(+), 99 deletions(-) diff --git a/application/migrator/model/init.go b/application/migrator/model/init.go index ec00c89d..1e2aca0d 100644 --- a/application/migrator/model/init.go +++ b/application/migrator/model/init.go @@ -28,7 +28,7 @@ func Init() error { if confDBType == "sqlite3" { confDBType = "sqlite" } - + // 兼容 "mariadb" 数据库 if confDBType == "mariadb" { confDBType = "mysql" diff --git a/pkg/cluster/node.go b/pkg/cluster/node.go index 584e85c7..831259e4 100644 --- a/pkg/cluster/node.go +++ b/pkg/cluster/node.go @@ -92,9 +92,7 @@ func newMasterNode(model *ent.Node, config conf.ConfigProvider, settings setting if config.System().Mode == conf.SlaveMode { n.client = request.NewClient(config, request.WithCorrelationID(), - request.WithCredential(auth.HMACAuth{ - []byte(config.Slave().Secret), - }, int64(config.Slave().SignatureTTL)), + request.WithCredential(auth.HMACAuth{SecretKey: []byte(config.Slave().Secret)}, int64(config.Slave().SignatureTTL)), ) } @@ -126,7 +124,7 @@ func (b *masterNode) PrepareUpload(ctx context.Context, args *fs.StatelessPrepar } uploadRequest := &fs.StatelessPrepareUploadResponse{} - resp.GobDecode(uploadRequest) + resp.DecodeGob(uploadRequest) return uploadRequest, nil } @@ -245,7 +243,7 @@ func newSlaveNode(ctx context.Context, model *ent.Node, config conf.ConfigProvid request.WithCorrelationID(), request.WithSlaveMeta(model.ID), request.WithMasterMeta(siteBasic.ID, settings.SiteURL(setting.UseFirstSiteUrl(ctx)).String()), - request.WithCredential(auth.HMACAuth{[]byte(model.SlaveKey)}, int64(settings.SlaveRequestSignTTL(ctx))), + request.WithCredential(auth.HMACAuth{SecretKey: []byte(model.SlaveKey)}, int64(settings.SlaveRequestSignTTL(ctx))), request.WithEndpoint(model.Server)), } } @@ -276,7 +274,7 @@ func (n *slaveNode) CreateTask(ctx context.Context, taskType string, state strin } taskId := 0 - if resp.GobDecode(&taskId); taskId > 0 { + if resp.DecodeGob(&taskId); taskId > 0 { return taskId, nil } @@ -301,7 +299,7 @@ func (n *slaveNode) GetTask(ctx context.Context, id int, clearOnComplete bool) ( } summary := &SlaveTaskSummary{} - resp.GobDecode(summary) + resp.DecodeGob(summary) return summary, nil } @@ -359,7 +357,7 @@ func (b *nodeBase) CreateTask(ctx context.Context, taskType string, state string } func (b *nodeBase) AuthInstance() auth.Auth { - return auth.HMACAuth{[]byte(b.model.SlaveKey)} + return auth.HMACAuth{SecretKey: []byte(b.model.SlaveKey)} } func (b *nodeBase) GetTask(ctx context.Context, id int, clearOnComplete bool) (*SlaveTaskSummary, error) { diff --git a/pkg/credmanager/credmanager.go b/pkg/credmanager/credmanager.go index b47deb0c..39d00260 100644 --- a/pkg/credmanager/credmanager.go +++ b/pkg/credmanager/credmanager.go @@ -166,9 +166,7 @@ func NewSlaveManager(kv cache.Driver, config conf.ConfigProvider) CredManager { kv: kv, client: request.NewClient( config, - request.WithCredential(auth.HMACAuth{ - []byte(config.Slave().Secret), - }, int64(config.Slave().SignatureTTL)), + request.WithCredential(auth.HMACAuth{SecretKey: []byte(config.Slave().Secret)}, int64(config.Slave().SignatureTTL)), ), } } @@ -228,7 +226,7 @@ func (m *slaveCredManager) requestCredFromMaster(ctx context.Context, key string } cred := &CredentialResponse{} - resp.GobDecode(&cred) + resp.DecodeGob(&cred) if err := m.kv.Set(key, *cred, max(int(time.Until(cred.Expiry()).Seconds()), 1)); err != nil { return nil, fmt.Errorf("failed to update credential in KV for key %q: %w", key, err) diff --git a/pkg/downloader/aria2/rpc/call.go b/pkg/downloader/aria2/rpc/call.go index 11cb137e..0f92d661 100644 --- a/pkg/downloader/aria2/rpc/call.go +++ b/pkg/downloader/aria2/rpc/call.go @@ -120,7 +120,7 @@ func (h *httpCaller) setNotifier(ctx context.Context, u url.URL, notifer Notifie return } -func (h httpCaller) Call(method string, params, reply interface{}) (err error) { +func (h *httpCaller) Call(method string, params, reply interface{}) (err error) { payload, err := EncodeClientRequest(method, params) if err != nil { return @@ -236,7 +236,7 @@ func (w *websocketCaller) Close() (err error) { return } -func (w websocketCaller) Call(method string, params, reply interface{}) (err error) { +func (w *websocketCaller) Call(method string, params, reply interface{}) (err error) { ctx, cancel := context.WithTimeout(context.Background(), w.timeout) defer cancel() select { diff --git a/pkg/downloader/slave/slave.go b/pkg/downloader/slave/slave.go index f25ff054..2ab27102 100644 --- a/pkg/downloader/slave/slave.go +++ b/pkg/downloader/slave/slave.go @@ -63,7 +63,7 @@ func (s *slaveDownloader) CreateTask(ctx context.Context, url string, options ma } var taskHandle *downloader.TaskHandle - if resp.GobDecode(&taskHandle); taskHandle != nil { + if resp.DecodeGob(&taskHandle); taskHandle != nil { return taskHandle, nil } @@ -101,7 +101,7 @@ func (s *slaveDownloader) Info(ctx context.Context, handle *downloader.TaskHandl } var taskStatus *downloader.TaskStatus - if resp.GobDecode(&taskStatus); taskStatus != nil { + if resp.DecodeGob(&taskStatus); taskStatus != nil { return taskStatus, nil } diff --git a/pkg/filemanager/driver/local/local.go b/pkg/filemanager/driver/local/local.go index 33f6ea93..699c5d91 100644 --- a/pkg/filemanager/driver/local/local.go +++ b/pkg/filemanager/driver/local/local.go @@ -229,9 +229,9 @@ func (handler *Driver) Token(ctx context.Context, uploadSession *fs.UploadSessio } else { // When disk pre-allocation is disabled, concurrent chunk uploads must be 1 // to avoid disk fragmentation and write contention on local storage. - settings := *handler.Policy.Settings - settings.ChunkConcurrency = 1 - handler.Policy.Settings = &settings + settings := *handler.Policy.Settings + settings.ChunkConcurrency = 1 + handler.Policy.Settings = &settings } return &fs.UploadCredential{ @@ -275,7 +275,7 @@ func (handler *Driver) CompleteUpload(ctx context.Context, session *fs.UploadSes nil, request.WithTimeout(time.Duration(handler.config.Slave().CallbackTimeout)*time.Second), request.WithCredential( - auth.HMACAuth{[]byte(session.Policy.Edges.Node.SlaveKey)}, + auth.HMACAuth{SecretKey: []byte(session.Policy.Edges.Node.SlaveKey)}, int64(handler.config.Slave().SignatureTTL), ), request.WithContext(ctx), diff --git a/pkg/filemanager/driver/remote/client.go b/pkg/filemanager/driver/remote/client.go index 34041eab..859abafa 100644 --- a/pkg/filemanager/driver/remote/client.go +++ b/pkg/filemanager/driver/remote/client.go @@ -61,7 +61,7 @@ func NewClient(ctx context.Context, policy *ent.StoragePolicy, settings setting. return nil, fmt.Errorf("remote storage policy %d has no node", policy.ID) } - authInstance := auth.HMACAuth{[]byte(policy.Edges.Node.SlaveKey)} + authInstance := auth.HMACAuth{SecretKey: []byte(policy.Edges.Node.SlaveKey)} serverURL, err := url.Parse(policy.Edges.Node.Server) if err != nil { return nil, err @@ -176,7 +176,7 @@ func (c *remoteClient) DeleteFiles(ctx context.Context, files ...string) ([]stri var failed []string failed = files if resp.Code == serializer.CodeNotFullySuccess { - resp.GobDecode(&failed) + resp.DecodeGob(&failed) } return failed, errors.New(resp.Error) } @@ -201,7 +201,7 @@ func (c *remoteClient) MediaMeta(ctx context.Context, src, ext, language string) } var metas []driver.MediaMeta - resp.GobDecode(&metas) + resp.DecodeGob(&metas) return metas, nil } @@ -250,7 +250,7 @@ func (c *remoteClient) List(ctx context.Context, path string, recursive bool) ([ } var objects []fs.PhysicalObject - resp.GobDecode(&objects) + resp.DecodeGob(&objects) return objects, nil } diff --git a/pkg/filemanager/driver/remote/remote.go b/pkg/filemanager/driver/remote/remote.go index cc0b89fa..b7a762df 100644 --- a/pkg/filemanager/driver/remote/remote.go +++ b/pkg/filemanager/driver/remote/remote.go @@ -47,7 +47,7 @@ func New(ctx context.Context, policy *ent.StoragePolicy, settings setting.Provid return &Driver{ Policy: policy, Client: request.NewClient(config), - AuthInstance: auth.HMACAuth{[]byte(policy.Edges.Node.SlaveKey)}, + AuthInstance: auth.HMACAuth{SecretKey: []byte(policy.Edges.Node.SlaveKey)}, uploadClient: client, settings: settings, config: config, diff --git a/pkg/request/request_test.go b/pkg/request/request_test.go index a42f1f37..ce507d09 100644 --- a/pkg/request/request_test.go +++ b/pkg/request/request_test.go @@ -18,7 +18,7 @@ type ClientMock struct { testMock.Mock } -func (m ClientMock) Request(method, target string, body io.Reader, opts ...Option) *Response { +func (m *ClientMock) Request(method, target string, body io.Reader, opts ...Option) *Response { args := m.Called(method, target, body, opts) return args.Get(0).(*Response) } diff --git a/pkg/serializer/response.go b/pkg/serializer/response.go index 7f10e1e5..134c26a4 100644 --- a/pkg/serializer/response.go +++ b/pkg/serializer/response.go @@ -28,8 +28,8 @@ func NewResponseWithGobData(c context.Context, data interface{}) Response { return Response{Data: w.Bytes()} } -// GobDecode 将 Response 正文解码至目标指针 -func (r *Response) GobDecode(target interface{}) { +// DecodeGob 将 Response 正文解码至目标指针 +func (r *Response) DecodeGob(target interface{}) { src := r.Data.(string) raw := make([]byte, len(src)*len(src)/base64.StdEncoding.DecodedLen(len(src))) base64.StdEncoding.Decode(raw, []byte(src)) diff --git a/pkg/util/ip_test.go b/pkg/util/ip_test.go index df794d25..935fb9a8 100644 --- a/pkg/util/ip_test.go +++ b/pkg/util/ip_test.go @@ -322,9 +322,9 @@ func TestIPMatcher_MatchIP_WithNetIP(t *testing.T) { func TestWildcardToCIDR(t *testing.T) { tests := []struct { - pattern string - cidr string - wantErr bool + pattern string + cidr string + wantErr bool }{ {"192.168.1.*", "192.168.1.0/24", false}, {"192.168.*.*", "192.168.0.0/16", false}, diff --git a/pkg/util/logger_test.go b/pkg/util/logger_test.go index 5f1352d5..a67c9588 100644 --- a/pkg/util/logger_test.go +++ b/pkg/util/logger_test.go @@ -1,3 +1,4 @@ +//go:build !race // +build !race package util diff --git a/pkg/webdav/internal/xml/marshal.go b/pkg/webdav/internal/xml/marshal.go index cb82ec21..4dd0f417 100644 --- a/pkg/webdav/internal/xml/marshal.go +++ b/pkg/webdav/internal/xml/marshal.go @@ -32,33 +32,33 @@ const ( // elements containing the data. // // The name for the XML elements is taken from, in order of preference: -// - the tag on the XMLName field, if the data is a struct -// - the value of the XMLName field of type xml.Name -// - the tag of the struct field used to obtain the data -// - the name of the struct field used to obtain the data -// - the name of the marshalled type +// - the tag on the XMLName field, if the data is a struct +// - the value of the XMLName field of type xml.Name +// - the tag of the struct field used to obtain the data +// - the name of the struct field used to obtain the data +// - the name of the marshalled type // // The XML element for a struct contains marshalled elements for each of the // exported fields of the struct, with these exceptions: -// - the XMLName field, described above, is omitted. -// - a field with tag "-" is omitted. -// - a field with tag "name,attr" becomes an attribute with -// the given name in the XML element. -// - a field with tag ",attr" becomes an attribute with the -// field name in the XML element. -// - a field with tag ",chardata" is written as character data, -// not as an XML element. -// - a field with tag ",innerxml" is written verbatim, not subject -// to the usual marshalling procedure. -// - a field with tag ",comment" is written as an XML comment, not -// subject to the usual marshalling procedure. It must not contain -// the "--" string within it. -// - a field with a tag including the "omitempty" option is omitted -// if the field value is empty. The empty values are false, 0, any -// nil pointer or interface value, and any array, slice, map, or -// string of length zero. -// - an anonymous struct field is handled as if the fields of its -// value were part of the outer struct. +// - the XMLName field, described above, is omitted. +// - a field with tag "-" is omitted. +// - a field with tag "name,attr" becomes an attribute with +// the given name in the XML element. +// - a field with tag ",attr" becomes an attribute with the +// field name in the XML element. +// - a field with tag ",chardata" is written as character data, +// not as an XML element. +// - a field with tag ",innerxml" is written verbatim, not subject +// to the usual marshalling procedure. +// - a field with tag ",comment" is written as an XML comment, not +// subject to the usual marshalling procedure. It must not contain +// the "--" string within it. +// - a field with a tag including the "omitempty" option is omitted +// if the field value is empty. The empty values are false, 0, any +// nil pointer or interface value, and any array, slice, map, or +// string of length zero. +// - an anonymous struct field is handled as if the fields of its +// value were part of the outer struct. // // If a field uses a tag "a>b>c", then the element c will be nested inside // parent elements a and b. Fields that appear next to each other that name diff --git a/pkg/webdav/internal/xml/read.go b/pkg/webdav/internal/xml/read.go index 4089056a..bfaef6f1 100644 --- a/pkg/webdav/internal/xml/read.go +++ b/pkg/webdav/internal/xml/read.go @@ -35,57 +35,57 @@ import ( // In the rules, the tag of a field refers to the value associated with the // key 'xml' in the struct field's tag (see the example above). // -// * If the struct has a field of type []byte or string with tag -// ",innerxml", Unmarshal accumulates the raw XML nested inside the -// element in that field. The rest of the rules still apply. +// - If the struct has a field of type []byte or string with tag +// ",innerxml", Unmarshal accumulates the raw XML nested inside the +// element in that field. The rest of the rules still apply. // -// * If the struct has a field named XMLName of type xml.Name, -// Unmarshal records the element name in that field. +// - If the struct has a field named XMLName of type xml.Name, +// Unmarshal records the element name in that field. // -// * If the XMLName field has an associated tag of the form -// "name" or "namespace-URL name", the XML element must have -// the given name (and, optionally, name space) or else Unmarshal -// returns an error. +// - If the XMLName field has an associated tag of the form +// "name" or "namespace-URL name", the XML element must have +// the given name (and, optionally, name space) or else Unmarshal +// returns an error. // -// * If the XML element has an attribute whose name matches a -// struct field name with an associated tag containing ",attr" or -// the explicit name in a struct field tag of the form "name,attr", -// Unmarshal records the attribute value in that field. +// - If the XML element has an attribute whose name matches a +// struct field name with an associated tag containing ",attr" or +// the explicit name in a struct field tag of the form "name,attr", +// Unmarshal records the attribute value in that field. // -// * If the XML element contains character data, that data is -// accumulated in the first struct field that has tag ",chardata". -// The struct field may have type []byte or string. -// If there is no such field, the character data is discarded. +// - If the XML element contains character data, that data is +// accumulated in the first struct field that has tag ",chardata". +// The struct field may have type []byte or string. +// If there is no such field, the character data is discarded. // -// * If the XML element contains comments, they are accumulated in -// the first struct field that has tag ",comment". The struct -// field may have type []byte or string. If there is no such -// field, the comments are discarded. +// - If the XML element contains comments, they are accumulated in +// the first struct field that has tag ",comment". The struct +// field may have type []byte or string. If there is no such +// field, the comments are discarded. // -// * If the XML element contains a sub-element whose name matches -// the prefix of a tag formatted as "a" or "a>b>c", unmarshal -// will descend into the XML structure looking for elements with the -// given names, and will map the innermost elements to that struct -// field. A tag starting with ">" is equivalent to one starting -// with the field name followed by ">". +// - If the XML element contains a sub-element whose name matches +// the prefix of a tag formatted as "a" or "a>b>c", unmarshal +// will descend into the XML structure looking for elements with the +// given names, and will map the innermost elements to that struct +// field. A tag starting with ">" is equivalent to one starting +// with the field name followed by ">". // -// * If the XML element contains a sub-element whose name matches -// a struct field's XMLName tag and the struct field has no -// explicit name tag as per the previous rule, unmarshal maps -// the sub-element to that struct field. +// - If the XML element contains a sub-element whose name matches +// a struct field's XMLName tag and the struct field has no +// explicit name tag as per the previous rule, unmarshal maps +// the sub-element to that struct field. // -// * If the XML element contains a sub-element whose name matches a -// field without any mode flags (",attr", ",chardata", etc), Unmarshal -// maps the sub-element to that struct field. +// - If the XML element contains a sub-element whose name matches a +// field without any mode flags (",attr", ",chardata", etc), Unmarshal +// maps the sub-element to that struct field. // -// * If the XML element contains a sub-element that hasn't matched any -// of the above rules and the struct has a field with tag ",any", -// unmarshal maps the sub-element to that struct field. +// - If the XML element contains a sub-element that hasn't matched any +// of the above rules and the struct has a field with tag ",any", +// unmarshal maps the sub-element to that struct field. // -// * An anonymous struct field is handled as if the fields of its -// value were part of the outer struct. +// - An anonymous struct field is handled as if the fields of its +// value were part of the outer struct. // -// * A struct field with tag "-" is never unmarshalled into. +// - A struct field with tag "-" is never unmarshalled into. // // Unmarshal maps an XML element to a string or []byte by saving the // concatenation of that element's character data in the string or @@ -110,7 +110,6 @@ import ( // // Unmarshal maps an XML element to a pointer by setting the pointer // to a freshly allocated value and then mapping the element to that value. -// func Unmarshal(data []byte, v interface{}) error { return NewDecoder(bytes.NewReader(data)).Decode(v) } diff --git a/pkg/webdav/prop.go b/pkg/webdav/prop.go index e7ea8728..cc14b1f1 100644 --- a/pkg/webdav/prop.go +++ b/pkg/webdav/prop.go @@ -113,7 +113,7 @@ func (m *metadataDeadProps) DeadProps() (map[xml.Name]Property, error) { } spaceLocal := strings.SplitN(strings.TrimPrefix(k, DeadPropsMetadataPrefix), SpaceNameSeparator, 2) - name := xml.Name{spaceLocal[0], spaceLocal[1]} + name := xml.Name{Space: spaceLocal[0], Local: spaceLocal[1]} propsStore := &DeadPropsStore{} if err := json.Unmarshal([]byte(v), propsStore); err != nil { return nil, err diff --git a/service/admin/node.go b/service/admin/node.go index 1badadac..409c83d3 100644 --- a/service/admin/node.go +++ b/service/admin/node.go @@ -141,7 +141,7 @@ func (service *TestNodeDownloaderService) Test(c *gin.Context) (string, error) { request.WithCorrelationID(), request.WithSlaveMeta(service.Node.ID), request.WithMasterMeta(settings.SiteBasic(c).ID, settings.SiteURL(setting.UseFirstSiteUrl(c)).String()), - request.WithCredential(auth.HMACAuth{[]byte(service.Node.SlaveKey)}, int64(settings.SlaveRequestSignTTL(c))), + request.WithCredential(auth.HMACAuth{SecretKey: []byte(service.Node.SlaveKey)}, int64(settings.SlaveRequestSignTTL(c))), request.WithEndpoint(service.Node.Server), ), service.Node.Settings) }