chore(go): modernize syntax (#2672)

* modernize go syntax

* fix lint
This commit is contained in:
Jo 2025-09-18 18:05:58 +02:00 committed by GitHub
parent 122c221d02
commit 81440e54a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 39 additions and 100 deletions

View File

@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
@ -508,10 +509,8 @@ func TestIntegrationLocalInstallGenerateSRCINFO(t *testing.T) {
}
captureOverride := func(cmd *exec.Cmd) (stdout string, stderr string, err error) {
for _, arg := range cmd.Args {
if arg == "--printsrcinfo" {
return string(srcinfo), "", nil
}
if slices.Contains(cmd.Args, "--printsrcinfo") {
return string(srcinfo), "", nil
}
return strings.Join(tars, "\n"), "", nil
}

View File

@ -529,14 +529,14 @@ func (ae *AlpmExecutor) AlpmArchitectures() ([]string, error) {
}
func alpmSetLogCallback(alpmHandle *alpm.Handle, cb func(alpm.LogLevel, string)) {
alpmHandle.SetLogCallback(func(ctx interface{}, lvl alpm.LogLevel, msg string) {
alpmHandle.SetLogCallback(func(ctx any, lvl alpm.LogLevel, msg string) {
cbo := ctx.(func(alpm.LogLevel, string))
cbo(lvl, msg)
}, cb)
}
func alpmSetQuestionCallback(alpmHandle *alpm.Handle, cb func(alpm.QuestionAny)) {
alpmHandle.SetQuestionCallback(func(ctx interface{}, q alpm.QuestionAny) {
alpmHandle.SetQuestionCallback(func(ctx any, q alpm.QuestionAny) {
cbo := ctx.(func(alpm.QuestionAny))
cbo(q)
}, cb)

View File

@ -1,15 +1,11 @@
package db
import "slices"
func ArchIsSupported(alpmArch []string, arch string) bool {
if arch == "any" {
return true
}
for _, a := range alpmArch {
if a == arch {
return true
}
}
return false
return slices.Contains(alpmArch, arch)
}

View File

@ -244,8 +244,6 @@ func (g *Grapher) GraphFromSrcInfos(ctx context.Context, graph *topo.Graph[strin
aurPkgsAdded := []*aurc.Pkg{}
for pkgBuildDir, pkgbuild := range srcInfos {
pkgBuildDir := pkgBuildDir
aurPkgs, err := makeAURPKGFromSrcinfo(g.dbExecutor, pkgbuild)
if err != nil {
return nil, err
@ -260,8 +258,6 @@ func (g *Grapher) GraphFromSrcInfos(ctx context.Context, graph *topo.Graph[strin
}
for _, pkg := range aurPkgs {
pkg := pkg
reason := Explicit
if pkg := g.dbExecutor.LocalPackage(pkg.Name); pkg != nil {
reason = Reason(pkg.Reason())

View File

@ -2,6 +2,7 @@ package topo
import (
"fmt"
"maps"
"strings"
"github.com/Jguer/go-alpm/v2"
@ -344,9 +345,7 @@ func (g *Graph[T, V]) buildTransitive(root T, nextFn func(T) NodeSet[T]) NodeSet
func (s NodeSet[T]) copy() NodeSet[T] {
out := make(NodeSet[T], len(s))
for k, v := range s {
out[k] = v
}
maps.Copy(out, s)
return out
}

View File

@ -123,7 +123,6 @@ func Test_getPackageURL(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := getPackagePKGBUILDURL(tt.args.pkgName)
@ -174,7 +173,6 @@ func TestGetABSPkgbuild(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
httpClient := &testClient{
@ -257,7 +255,6 @@ func Test_getPackageRepoURL(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := getPackageRepoURL(tt.args.pkgName)

View File

@ -55,7 +55,6 @@ func TestGetAURPkgbuild(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
httpClient := &testClient{

View File

@ -123,7 +123,6 @@ func TestIntRange_Get(t *testing.T) {
{name: "normal end range false", fields: fields{1, 2}, args: args{3}, want: false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r := IntRange{
@ -183,7 +182,6 @@ func TestIntRanges_Get(t *testing.T) {
{name: "normal end range false", rs: IntRanges{{1, 2}}, args: args{3}, want: false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := tt.rs.Get(tt.args.n); got != tt.want {

View File

@ -103,7 +103,7 @@ func editPkgbuilds(log *text.Logger, pkgbuildDirs map[string]string, bases []str
if len(pkgbuilds) > 0 {
editor, editorArgs := editor(log, editorConfig, editorFlags, noConfirm)
editorArgs = append(editorArgs, pkgbuilds...)
editcmd := exec.Command(editor, editorArgs...)
editcmd := exec.CommandContext(context.Background(), editor, editorArgs...)
editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := editcmd.Run(); err != nil {

View File

@ -129,7 +129,6 @@ func TestPrintNewsFeed(t *testing.T) {
}
t.Setenv("TZ", "UTC")
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
gock.New("https://archlinux.org").
Get("/feeds/news").

View File

@ -43,7 +43,7 @@ type SourceQueryBuilder struct {
sortBy string
searchBy string
targetMode parser.TargetMode
queryMap map[string]map[string]interface{}
queryMap map[string]map[string]any
bottomUp bool
singleLineResults bool
separateSources bool
@ -71,7 +71,7 @@ func NewSourceQueryBuilder(
searchBy: searchBy,
singleLineResults: singleLineResults,
separateSources: separateSources,
queryMap: map[string]map[string]interface{}{},
queryMap: map[string]map[string]any{},
results: make([]abstractResult, 0, 100),
}
}
@ -154,7 +154,7 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor
for i := range aurResults {
if s.queryMap[dbName] == nil {
s.queryMap[dbName] = map[string]interface{}{}
s.queryMap[dbName] = map[string]any{}
}
by := getSearchBy(s.searchBy)
@ -182,7 +182,7 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor
for i := range repoResults {
dbName := repoResults[i].DB().Name()
if s.queryMap[dbName] == nil {
s.queryMap[dbName] = map[string]interface{}{}
s.queryMap[dbName] = map[string]any{}
}
s.queryMap[dbName][repoResults[i].Name()] = repoResults[i]

View File

@ -284,7 +284,7 @@ func (c *CmdBuilder) sudoLoopBackground() {
func (c *CmdBuilder) updateSudo() {
for {
err := c.Show(exec.Command(c.SudoBin, "-v"))
err := c.Show(exec.CommandContext(context.Background(), c.SudoBin, "-v"))
if err != nil {
c.Log.Errorln(err)
} else {

View File

@ -10,8 +10,8 @@ import (
)
type Call struct {
Res []interface{}
Args []interface{}
Res []any
Args []any
Dir string
}
@ -51,8 +51,8 @@ func (m *MockBuilder) BuildMakepkgCmd(ctx context.Context, dir string, extraArgs
m.BuildMakepkgCmdCallsMu.Lock()
m.BuildMakepkgCmdCalls = append(m.BuildMakepkgCmdCalls, Call{
Res: []interface{}{res},
Args: []interface{}{
Res: []any{res},
Args: []any{
ctx,
dir,
extraArgs,
@ -103,7 +103,7 @@ func (m *MockBuilder) GetKeepSrc() bool {
func (m *MockRunner) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
m.CaptureCallsMu.Lock()
m.CaptureCalls = append(m.CaptureCalls, Call{
Args: []interface{}{
Args: []any{
cmd,
},
Dir: cmd.Dir,
@ -125,7 +125,7 @@ func (m *MockRunner) Show(cmd *exec.Cmd) error {
m.ShowCallsMu.Lock()
m.ShowCalls = append(m.ShowCalls, Call{
Args: []interface{}{
Args: []any{
cmd,
},
Dir: cmd.Dir,

View File

@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"maps"
"os"
"strings"
@ -87,9 +88,7 @@ func (a *Arguments) Copy() (cp *Arguments) {
cp.Op = a.Op
for k, v := range a.Options {
cp.Options[k] = v
}
maps.Copy(cp.Options, a.Options)
cp.Targets = make([]string, len(a.Targets))
copy(cp.Targets, a.Targets)

View File

@ -37,7 +37,6 @@ func TestOption_Add(t *testing.T) {
}, want: []string{"c"}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
o := &Option{
@ -75,7 +74,6 @@ func TestOption_Set(t *testing.T) {
}, want: []string{"c"}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
o := &Option{
@ -105,7 +103,6 @@ func TestOption_First(t *testing.T) {
}, want: ""},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
o := &Option{
@ -158,7 +155,6 @@ func TestArguments_CopyGlobal(t *testing.T) {
}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cmdArgs := &Arguments{
@ -207,7 +203,6 @@ func TestArguments_Copy(t *testing.T) {
}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cmdArgs := &Arguments{
@ -265,7 +260,6 @@ func TestArguments_FormatArgs(t *testing.T) {
}, wantArgs: []string{"-Y", "--overwrite", "/tmp/a", "--overwrite", "/tmp/b", "--overwrite", "/tmp/c", "--needed"}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cmdArgs := &Arguments{
@ -313,7 +307,6 @@ func TestArguments_FormatGlobalArgs(t *testing.T) {
}, wantArgs: []string(nil)},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cmdArgs := &Arguments{

View File

@ -3,6 +3,7 @@ package build
import (
"context"
"fmt"
"maps"
"os"
"github.com/Jguer/yay/v12/pkg/db"
@ -126,9 +127,7 @@ func (installer *Installer) Install(ctx context.Context,
}
func mergeLayers(layer1, layer2 map[string]*dep.InstallInfo) map[string]*dep.InstallInfo {
for name, info := range layer2 {
layer1[name] = info
}
maps.Copy(layer1, layer2)
return layer1
}

View File

@ -88,7 +88,6 @@ func TestInstaller_InstallNeeded(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
tmpDir := td.TempDir()
pkgTar := tmpDir + "/yay-91.0.0-1-x86_64.pkg.tar.zst"
@ -356,7 +355,6 @@ func TestInstaller_InstallMixedSourcesAndLayers(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
pkgTar := tmpDir + "/yay-91.0.0-1-x86_64.pkg.tar.zst"
jfinPkgTar := tmpDirJfin + "/jellyfin-server-10.8.8-1-x86_64.pkg.tar.zst"
@ -561,7 +559,6 @@ func TestInstaller_CompileFailed(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
pkgTar := tmpDir + "/yay-91.0.0-1-x86_64.pkg.tar.zst"
@ -718,7 +715,6 @@ func TestInstaller_InstallSplitPackage(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
pkgTars := []string{
tmpDir + "/jellyfin-10.8.4-1-x86_64.pkg.tar.zst",
@ -854,7 +850,6 @@ func TestInstaller_InstallDownloadOnly(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
tmpDir := td.TempDir()
pkgTar := tmpDir + "/yay-91.0.0-1-x86_64.pkg.tar.zst"
@ -979,7 +974,6 @@ func TestInstaller_InstallGroup(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
tmpDir := td.TempDir()
@ -1176,7 +1170,6 @@ func TestInstaller_InstallRebuild(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
tmpDir := td.TempDir()
pkgTar := tmpDir + "/yay-91.0.0-1-x86_64.pkg.tar.zst"
@ -1294,7 +1287,6 @@ func TestInstaller_InstallUpgrade(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
mockDB := &mock.DBExecutor{}
mockRunner := &exe.MockRunner{}
@ -1391,7 +1383,6 @@ func TestInstaller_KeepSrc(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
tmpDir := td.TempDir()
pkgTar := tmpDir + "/yay-92.0.0-1-x86_64.pkg.tar.zst"

View File

@ -133,7 +133,7 @@ func TestParsePackageList(t *testing.T) {
}
for _, tc := range testCases {
tc := tc // capture range variable
// capture range variable
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()

View File

@ -220,7 +220,6 @@ func TestCheckPgpKeys(t *testing.T) {
}
for _, tt := range testcases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
mockRunner := &exe.MockRunner{
ShowFn: tt.showFn,

View File

@ -80,7 +80,6 @@ func Test_downloadPKGBUILDSource(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.desc, func(td *testing.T) {
cmdBuilder := &TestMakepkgBuilder{
parentBuilder: &exe.CmdBuilder{

View File

@ -38,7 +38,7 @@ func (l *Logger) Debugln(a ...any) {
return
}
l.Println(append([]interface{}{
l.Println(append([]any{
Bold(yellow(fmt.Sprintf("[DEBUG:%s]", l.name))),
}, a...)...)
}
@ -52,15 +52,15 @@ func (l *Logger) OperationInfo(a ...any) {
}
func (l *Logger) SprintOperationInfo(a ...any) string {
return fmt.Sprint(append([]interface{}{Bold(Cyan(opSymbol + " ")), boldCode}, a...)...) + ResetCode
return fmt.Sprint(append([]any{Bold(Cyan(opSymbol + " ")), boldCode}, a...)...) + ResetCode
}
func (l *Logger) Info(a ...any) {
l.Print(append([]interface{}{Bold(Green(arrow + " "))}, a...)...)
l.Print(append([]any{Bold(Green(arrow + " "))}, a...)...)
}
func (l *Logger) Infoln(a ...any) {
l.Println(append([]interface{}{Bold(Green(arrow))}, a...)...)
l.Println(append([]any{Bold(Green(arrow))}, a...)...)
}
func (l *Logger) Warn(a ...any) {
@ -72,7 +72,7 @@ func (l *Logger) Warnln(a ...any) {
}
func (l *Logger) SprintWarn(a ...any) string {
return fmt.Sprint(append([]interface{}{Bold(yellow(smallArrow + " "))}, a...)...)
return fmt.Sprint(append([]any{Bold(yellow(smallArrow + " "))}, a...)...)
}
func (l *Logger) Error(a ...any) {
@ -84,7 +84,7 @@ func (l *Logger) Errorln(a ...any) {
}
func (l *Logger) SprintError(a ...any) string {
return fmt.Sprint(append([]interface{}{Bold(Red(smallArrow + " "))}, a...)...)
return fmt.Sprint(append([]any{Bold(Red(smallArrow + " "))}, a...)...)
}
func (l *Logger) Printf(format string, a ...any) {

View File

@ -23,10 +23,7 @@ func SplitDBFromName(pkg string) (db, name string) {
// LessRunes compares two rune values, and returns true if the first argument is lexicographicaly smaller.
func LessRunes(iRunes, jRunes []rune) bool {
maxLen := len(iRunes)
if maxLen > len(jRunes) {
maxLen = len(jRunes)
}
maxLen := min(len(iRunes), len(jRunes))
for idx := 0; idx < maxLen; idx++ {
ir := iRunes[idx]

View File

@ -40,7 +40,6 @@ func TestLessRunes(t *testing.T) {
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := LessRunes(tt.args.iRunes, tt.args.jRunes)

View File

@ -885,7 +885,6 @@ func TestUpgradeService_GraphUpgrades_zfs_dkms(t *testing.T) {
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
dbExe := &mock.DBExecutor{

View File

@ -122,7 +122,6 @@ func Test_upAUR(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
@ -228,7 +227,6 @@ func Test_upDevel(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := UpDevel(context.Background(),

View File

@ -2,6 +2,7 @@ package vcs
import (
"context"
"slices"
"github.com/Jguer/go-alpm/v2"
gosrc "github.com/Morganamilo/go-srcinfo"
@ -13,12 +14,7 @@ type Mock struct {
}
func (m *Mock) ToUpgrade(ctx context.Context, pkgName string) bool {
for _, pkg := range m.ToUpgradeReturn {
if pkg == pkgName {
return true
}
}
return false
return slices.Contains(m.ToUpgradeReturn, pkgName)
}
func (m *Mock) Update(ctx context.Context, pkgName string, sources []gosrc.ArchString) {

View File

@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"slices"
"strings"
"sync"
"time"
@ -170,14 +171,7 @@ func parseSource(source string) (url, branch string, protocols []string) {
protocols = strings.SplitN(split[0], "+", 2)
git := false
for _, protocol := range protocols {
if protocol == "git" {
git = true
break
}
}
git := slices.Contains(protocols, "git")
protocols = protocols[len(protocols)-1:]

View File

@ -84,7 +84,6 @@ func TestNewInfoStore(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := NewInfoStore(tt.args.filePath, tt.args.cmdBuilder,
@ -231,7 +230,6 @@ func TestInfoStoreToUpgrade(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
@ -364,7 +362,6 @@ func TestInfoStore_NeedsUpdate(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
@ -413,7 +410,6 @@ func TestInfoStore_Update(t *testing.T) {
require.NoError(t, err)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
@ -476,7 +472,6 @@ func TestInfoStore_Remove(t *testing.T) {
require.NoError(t, err)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
@ -524,7 +519,6 @@ func TestInfoStore_CleanOrphans(t *testing.T) {
require.NoError(t, err)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{

View File

@ -73,7 +73,7 @@ func biggestPackages(logger *text.Logger, dbExecutor db.Executor) {
return
}
for i := 0; i < 10; i++ {
for i := range 10 {
logger.Printf("%s: %s\n", text.Bold(pkgS[i].Name()), text.Cyan(text.Human(pkgS[i].ISize())))
}
}

View File

@ -630,7 +630,6 @@ func TestSyncUpgrade_NoCombinedUpgrade(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
makepkgBin := t.TempDir() + "/makepkg"