Skip to content

Commit 8950ada

Browse files
committed
refactor(opflags): update some logic for clarity and safety
1 parent 2f5678b commit 8950ada

File tree

6 files changed

+18
-22
lines changed

6 files changed

+18
-22
lines changed

.golangci.yml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,19 @@ linters:
3030
- linters:
3131
- gosec
3232
text: 'G115: integer overflow conversion'
33-
34-
- linters:
35-
- gosec
36-
text: 'G602: slice index out of range'
37-
38-
- linters:
39-
- govet
40-
text: 'buildtag: \+build line is no longer needed'
4133

4234
- linters:
4335
- staticcheck
4436
text: 'ST1001: should not use dot imports'
45-
37+
4638
- linters:
39+
- gosec
40+
text: 'G703: Path traversal via taint analysis'
41+
42+
- linters:
43+
- gosec
4744
- staticcheck
48-
text: 'this check suggests that the pointer can be nil'
49-
path: persistence/postgres_test.go
45+
path: _test\.go$
5046

5147
paths:
5248
- third_party$

merkle/merkle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Root(hashes [][sha256.Size]byte) [sha256.Size]byte {
2222
panic(fmt.Sprintf("expected power of two number of hashes, got %d", numHashes))
2323
}
2424
var next [][sha256.Size]byte
25-
for i := 0; i < len(hashes); i += 2 {
25+
for i := 0; i < len(hashes)-1; i += 2 {
2626
left := hashes[i]
2727
right := hashes[i+1]
2828
h := sha256.Sum256(append(left[:], right[:]...))

opflags/load_cred.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
)
1111

1212
func (o *OpFlags) LoadCred() error {
13-
if o.Cred == "" {
13+
if o.CredentialsPath == "" {
1414
return nil
1515
}
1616

17-
file, err := os.Open(o.Cred)
17+
file, err := os.Open(o.CredentialsPath)
1818
if err != nil {
1919
return fmt.Errorf("error while opening file: %s", err.Error())
2020
}

opflags/load_cred_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func TestLoadCred(t *testing.T) {
4747

4848
// Initialize OpFlags with the temporary file path
4949
o := &OpFlags{
50-
Cred: tmpfile.Name(),
51-
Credentials: make(map[string][]byte),
50+
CredentialsPath: tmpfile.Name(),
51+
Credentials: make(map[string][]byte),
5252
}
5353

5454
// Call LoadCred and check the result

opflags/opflags.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ type OpFlags struct {
2424
FilterNodesCIDRs []string `long:"filter-nodes-cidrs" description:"List of CIDRs on which Magnetico can operate. Empty is open mode." default:"" yaml:"filterNodesCIDRs"`
2525
FilterNodesIpNets []net.IPNet
2626

27-
Addr string `short:"a" long:"addr" description:"Address (host:port) to serve on" default:"[::1]:8080" yaml:"addr"`
28-
Cred string `short:"c" long:"credentials" description:"Path to the credentials file" default:"" yaml:"cred"`
29-
Credentials map[string][]byte
30-
Timeout uint `short:"t" long:"timeout" description:"Timeout in seconds for the web interface and APIs." default:"600" yaml:"timeout"`
27+
Addr string `short:"a" long:"addr" description:"Address (host:port) to serve on" default:"[::1]:8080" yaml:"addr"`
28+
CredentialsPath string `short:"c" long:"credentials" description:"Path to the credentials file" default:"" yaml:"cred"`
29+
Credentials map[string][]byte
30+
Timeout uint `short:"t" long:"timeout" description:"Timeout in seconds for the web interface and APIs." default:"600" yaml:"timeout"`
3131

3232
RunDaemon bool `short:"d" long:"daemon" description:"Run the crawler without the web interface." yaml:"runDaemon"`
3333
RunWeb bool `short:"w" long:"web" description:"Run the web interface without the crawler." yaml:"runWeb"`

opflags/opflags_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func TestCheck(t *testing.T) {
6666
{
6767
name: "RunWebWithInvalidCred",
6868
opFlags: OpFlags{
69-
RunWeb: true,
70-
Cred: "invalid-cred",
69+
RunWeb: true,
70+
CredentialsPath: "invalid-cred",
7171
},
7272
expectError: true,
7373
},

0 commit comments

Comments
 (0)