Skip to content

Commit ba475e2

Browse files
committed
fix some linter warnings
1 parent 58d37ac commit ba475e2

10 files changed

Lines changed: 79 additions & 77 deletions

File tree

cmd/raptly/main.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ type Context struct {
1515
client *aptly.Client
1616
}
1717

18-
var cli struct {
19-
Version kong.VersionFlag `name:"version" help:"Print version information and quit"`
20-
21-
Url string `kong:"help='required,Aptly server API URL',env='RAPTLY_URL'"`
22-
Insecure bool `kong:"optional,help='Allow insecure HTTPS connections'"`
23-
User *string `kong:"help='HTTP basic auth username',env='RAPTLY_USER'"`
24-
BasicPW *string `kong:"name='basic-pass',help='HTTP basic auth password',env='RAPTLY_BASIC_PASS'"`
25-
26-
Repo RepoCLI `kong:"cmd,help='Repository management commands',group='Repo'"`
27-
Publish publishCLI `kong:"cmd,help='Published lists commands',group='publish'"`
28-
Snapshot SnapshotCLI `kong:"cmd,help='Snapshot lists commands',group='snapshot'"`
29-
Package PkgsCLI `kong:"cmd,help='Package search commands',group='package'"`
30-
Files FilesCLI `kong:"cmd,help='Uploaded file management commands',group='Files'"`
31-
Status StatusCLI `kong:"cmd,help='Aptly server status command',group='Status'"`
32-
}
33-
3418
func main() {
19+
var cli struct {
20+
Version kong.VersionFlag `name:"version" help:"Print version information and quit"`
21+
22+
Url string `kong:"help='required,Aptly server API URL',env='RAPTLY_URL'"`
23+
Insecure bool `kong:"optional,help='Allow insecure HTTPS connections'"`
24+
User *string `kong:"help='HTTP basic auth username',env='RAPTLY_USER'"`
25+
BasicPW *string `kong:"name='basic-pass',help='HTTP basic auth password',env='RAPTLY_BASIC_PASS'"`
26+
27+
Repo RepoCLI `kong:"cmd,help='Repository management commands',group='Repo'"`
28+
Publish publishCLI `kong:"cmd,help='Published lists commands',group='publish'"`
29+
Snapshot SnapshotCLI `kong:"cmd,help='Snapshot lists commands',group='snapshot'"`
30+
Package PkgsCLI `kong:"cmd,help='Package search commands',group='package'"`
31+
Files FilesCLI `kong:"cmd,help='Uploaded file management commands',group='Files'"`
32+
Status StatusCLI `kong:"cmd,help='Aptly server status command',group='Status'"`
33+
}
34+
3535
ctx := kong.Parse(&cli,
3636
kong.Vars{"version": Version})
3737

cmd/raptly/publish.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ func formatPublishedRepository(list *aptly.PublishedList) string {
2020
publishes := ""
2121
for i, src := range list.Sources {
2222
if i > 0 {
23-
publishes = publishes + ", "
23+
publishes += ", "
2424
}
25-
publishes = publishes + fmt.Sprintf("%s: [%s]", src.Component, src.Name)
25+
publishes += fmt.Sprintf("%s: [%s]", src.Component, src.Name)
2626
}
2727

2828
if list.SourceKind == "local" {

cmd/raptly/repo.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"math/rand"
6+
"math/rand/v2"
77
"os"
88
"path/filepath"
99
aptly "raptly/pkg/rest-aptly"
@@ -167,6 +167,11 @@ func (c *RepoRemoveCmd) Run(ctx *Context) error {
167167
return nil
168168
}
169169

170+
const ExtDsc = ".dsc"
171+
const ExtDeb = ".deb"
172+
const ExtUdeb = ".ubed"
173+
const ExtChanged = ".changes"
174+
170175
type RepoAddCmd struct {
171176
ForceReplace bool `kong:"name='force-replace'"`
172177
// RemoveFiles bool `kong:"name='remove-files'"`
@@ -190,7 +195,7 @@ func (c *RepoAddCmd) Run(ctx *Context) error {
190195
extension := filepath.Ext(path)
191196
if isDebianFile(extension) {
192197
filesToUpload = append(filesToUpload, path)
193-
if extension == ".dsc" {
198+
if extension == ExtDsc {
194199
referenced, err := getFilesFromDsc(path)
195200
if err != nil {
196201
return err
@@ -211,7 +216,7 @@ func (c *RepoAddCmd) Run(ctx *Context) error {
211216
extension := filepath.Ext(c.Path)
212217
if isDebianFile(extension) {
213218
filesToUpload = append(filesToUpload, c.Path)
214-
if extension == ".dsc" {
219+
if extension == ExtDsc {
215220
referenced, err := getFilesFromDsc(c.Path)
216221
if err != nil {
217222
return err
@@ -239,23 +244,21 @@ func (c *RepoAddCmd) Run(ctx *Context) error {
239244
return nil
240245
}
241246

242-
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
243-
244247
func randSeq(n int) string {
248+
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
245249
b := make([]rune, n)
246250
for i := range b {
247-
b[i] = letters[rand.Intn(len(letters))]
251+
b[i] = letters[rand.IntN(len(letters))]
248252
}
249253
return string(b)
250254
}
251255

252256
func isDebianFile(extension string) bool {
253-
return extension == ".deb" || extension == ".udeb" || extension == ".dsc"
257+
return extension == ExtDeb || extension == ExtUdeb || extension == ExtDsc
254258
}
255259

256260
func checkFileExists(filePath string) bool {
257261
_, error := os.Stat(filePath)
258-
//return !os.IsNotExist(err)
259262
return !errors.Is(error, os.ErrNotExist)
260263
}
261264

@@ -299,7 +302,7 @@ func (c *RepoIncludeCmd) Run(ctx *Context) error {
299302
err := filepath.Walk(c.Path, func(path string, info os.FileInfo, err error) error {
300303
if !info.IsDir() {
301304
extension := filepath.Ext(path)
302-
if extension == ".changes" {
305+
if extension == ExtChanged {
303306
filesToUpload = append(filesToUpload, path)
304307
referenced, err := getFilesFromChanges(path)
305308
if err != nil {

cmd/raptly/snapshot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ func (c *SnapshotDiffCmd) Run(ctx *Context) error {
187187
} else if pkgDiff.Right == nil {
188188
indicator = Red + "-" + Reset
189189
}
190-
arch := ""
190+
var arch string
191191
if pkgDiff.Left != nil {
192192
arch = pkgDiff.Left.Architecture
193193
} else {
194194
arch = pkgDiff.Right.Architecture
195195
}
196196

197-
pkg := ""
197+
var pkg string
198198
if pkgDiff.Left != nil {
199199
pkg = pkgDiff.Left.Package
200200
} else {

pkg/rest-aptly/client.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ type Client struct {
1111
client *resty.Client
1212
}
1313

14+
func NewClient(url string) *Client {
15+
client := new(Client)
16+
client.client = resty.New()
17+
client.client.SetBaseURL(url)
18+
client.client.SetError(APIError{})
19+
20+
return client
21+
}
22+
1423
// GetClient get resty client used for advanced use cases like testing or special auth
1524
func (c *Client) GetClient() *resty.Client {
1625
return c.client
@@ -53,15 +62,6 @@ func (c *Client) send(req *resty.Request) error {
5362
return getError(res)
5463
}
5564

56-
func NewClient(url string) *Client {
57-
client := new(Client)
58-
client.client = resty.New()
59-
client.client.SetBaseURL(url)
60-
client.client.SetError(APIError{})
61-
62-
return client
63-
}
64-
6565
type APIError struct {
6666
// as pointer to distinguish between valid error and failed parsing errors (like empty bodies)
6767
ErrorMsg *string `json:"error,omitempty"`
@@ -82,8 +82,8 @@ func (e *APIError) Valid() bool {
8282
func getError(response *resty.Response) error {
8383
e := response.Error()
8484
if e != nil {
85-
apiErr := e.(*APIError)
86-
if apiErr.Valid() {
85+
apiErr, ok := e.(*APIError)
86+
if ok && apiErr.Valid() {
8787
return apiErr
8888
}
8989
}

pkg/rest-aptly/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestGet(t *testing.T) {
2323
}
2424

2525
httpmock.RegisterResponder(http.MethodGet, "http://host.local/get",
26-
func(req *http.Request) (*http.Response, error) {
26+
func(_ *http.Request) (*http.Response, error) {
2727
return httpmock.NewJsonResponse(200, response)
2828
})
2929

@@ -41,15 +41,15 @@ func TestGetError(t *testing.T) {
4141
httpmock.ActivateNonDefault(client.GetClient().GetClient())
4242

4343
httpmock.RegisterResponder(http.MethodGet, "http://host.local/json/content-type",
44-
func(req *http.Request) (*http.Response, error) {
44+
func(_ *http.Request) (*http.Response, error) {
4545
return httpmock.NewJsonResponse(500, APIError{ErrorMsg: ptr("json")})
4646
})
4747
httpmock.RegisterResponder(http.MethodGet, "http://host.local/plain/content-type",
48-
func(req *http.Request) (*http.Response, error) {
48+
func(_ *http.Request) (*http.Response, error) {
4949
return httpmock.NewStringResponse(501, `{"error": "plain"}`), nil
5050
})
5151
httpmock.RegisterResponder(http.MethodGet, "http://host.local/invalid",
52-
func(req *http.Request) (*http.Response, error) {
52+
func(_ *http.Request) (*http.Response, error) {
5353
return httpmock.NewStringResponse(502, ""), nil
5454
})
5555

pkg/rest-aptly/files_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestFilesListDirs(t *testing.T) {
1515
client := clientForTest(t, "http://host.local")
1616

1717
httpmock.RegisterResponder(http.MethodGet, "http://host.local/api/files",
18-
func(req *http.Request) (*http.Response, error) {
18+
func(_ *http.Request) (*http.Response, error) {
1919
return httpmock.NewJsonResponse(200, []string{"dir1", "dir2", "dir3"})
2020
})
2121

@@ -28,7 +28,7 @@ func TestFilesListFiles(t *testing.T) {
2828
client := clientForTest(t, "http://host.local")
2929

3030
httpmock.RegisterResponder(http.MethodGet, "http://host.local/api/files/dirTest",
31-
func(req *http.Request) (*http.Response, error) {
31+
func(_ *http.Request) (*http.Response, error) {
3232
return httpmock.NewJsonResponse(200, []string{"file1", "file2", "file3"})
3333
})
3434

@@ -58,7 +58,7 @@ func TestFilesUpload(t *testing.T) {
5858

5959
data1 := "file0 data"
6060

61-
f1, err := os.CreateTemp("", "file0_")
61+
f1, err := os.CreateTemp(t.TempDir(), "file0_")
6262
assert.NoError(t, err)
6363
defer os.Remove(f1.Name())
6464
_, err = f1.WriteString(data1)
@@ -90,7 +90,7 @@ func TestFilesDeleteDir(t *testing.T) {
9090
client := clientForTest(t, "http://host.local")
9191

9292
httpmock.RegisterResponder(http.MethodDelete, "http://host.local/api/files/dirTest",
93-
func(req *http.Request) (*http.Response, error) {
93+
func(_ *http.Request) (*http.Response, error) {
9494
return httpmock.NewStringResponse(200, ""), nil
9595
})
9696

@@ -101,7 +101,7 @@ func TestFilesDeleteFile(t *testing.T) {
101101
client := clientForTest(t, "http://host.local")
102102

103103
httpmock.RegisterResponder(http.MethodDelete, "http://host.local/api/files/dirTest/file",
104-
func(req *http.Request) (*http.Response, error) {
104+
func(_ *http.Request) (*http.Response, error) {
105105
return httpmock.NewStringResponse(200, ""), nil
106106
})
107107

pkg/rest-aptly/packages.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package aptly
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"regexp"
78

@@ -24,7 +25,7 @@ func (opts *ListPackagesOptions) MakeParams() (map[string]string, error) {
2425
params := make(map[string]string)
2526

2627
if opts.Query == "" && opts.WithDeps {
27-
return nil, fmt.Errorf("withDeps requires a query")
28+
return nil, errors.New("withDeps requires a query")
2829
}
2930
if opts.Query != "" {
3031
params["q"] = opts.Query

pkg/rest-aptly/snapshots.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package aptly
22

3-
import "fmt"
3+
import "errors"
44

55
// Snapshot is immutable state of repository: list of packages
66
type Snapshot struct {
@@ -162,32 +162,31 @@ func (c *Client) SnapshotDiff(left string, right string, onlyMatching bool) ([]P
162162

163163
err := c.send(req)
164164

165-
if err == nil {
166-
diff := make([]PackageDiff, 0, len(diffs))
165+
if err != nil {
166+
return nil, err
167+
}
167168

168-
for _, d := range diffs {
169-
var left, right *Package
169+
diff := make([]PackageDiff, 0, len(diffs))
170+
for _, d := range diffs {
171+
var left, right *Package
170172

171-
if d.Left != nil {
172-
leftPkg, err := PackageFromKey(*d.Left)
173-
if err != nil {
174-
return nil, err
175-
}
176-
left = &leftPkg
173+
if d.Left != nil {
174+
leftPkg, err := PackageFromKey(*d.Left)
175+
if err != nil {
176+
return nil, err
177177
}
178-
if d.Right != nil {
179-
rightPkg, err := PackageFromKey(*d.Right)
180-
if err != nil {
181-
return nil, err
182-
}
183-
right = &rightPkg
178+
left = &leftPkg
179+
}
180+
if d.Right != nil {
181+
rightPkg, err := PackageFromKey(*d.Right)
182+
if err != nil {
183+
return nil, err
184184
}
185-
diff = append(diff, PackageDiff{Left: left, Right: right})
185+
right = &rightPkg
186186
}
187-
return diff, nil
188-
} else {
189-
return nil, err
187+
diff = append(diff, PackageDiff{Left: left, Right: right})
190188
}
189+
return diff, nil
191190
}
192191

193192
type SnapshotUpdateOptions struct {
@@ -224,10 +223,10 @@ func (c *Client) SnapshotMerge(destination string, sources []string, opts Snapsh
224223
}
225224
// check for simple errors before hitting the server
226225
if len(sources) == 0 {
227-
return snap, fmt.Errorf("minimum one source snapshot is required")
226+
return snap, errors.New("minimum one source snapshot is required")
228227
}
229228
if opts.Latest && opts.NoRemove {
230-
return snap, fmt.Errorf("minimum one source snapshot is required")
229+
return snap, errors.New("minimum one source snapshot is required")
231230
}
232231

233232
params := make(map[string]string)

pkg/rest-aptly/status.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package aptly
22

3-
type AptlyAPIVersion struct {
3+
type ServerVersion struct {
44
Version string
55
}
6-
type Version = AptlyAPIVersion
76

8-
func (c *Client) Version() (Version, error) {
9-
var version Version
7+
func (c *Client) Version() (ServerVersion, error) {
8+
var version ServerVersion
109

1110
req := c.get("api/version").
1211
SetResult(&version)

0 commit comments

Comments
 (0)