Skip to content

Commit c7694e4

Browse files
committed
Refactor: remove named returns and magic numbers
1 parent 0d3a2f7 commit c7694e4

File tree

10 files changed

+172
-182
lines changed

10 files changed

+172
-182
lines changed

constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sncli
2+
3+
import "time"
4+
5+
const spinnerDelay = 100 * time.Millisecond

debug.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sncli
22

33
import (
44
"encoding/base64"
5+
"errors"
56
"fmt"
67
"strings"
78

@@ -18,7 +19,7 @@ type DecryptStringInput struct {
1819
Key string
1920
}
2021

21-
func DecryptString(input DecryptStringInput) (plaintext string, err error) {
22+
func DecryptString(input DecryptStringInput) (string, error) {
2223
key1 := input.Session.MasterKey
2324
if input.Key != "" {
2425
key1 = input.Key
@@ -34,20 +35,18 @@ func DecryptString(input DecryptStringInput) (plaintext string, err error) {
3435

3536
version, nonce, cipherText, authData := splitContent(input.In)
3637
if version != "004" {
37-
return plaintext, fmt.Errorf("only version 004 of encryption is supported")
38+
return "", errors.New("only version 004 of encryption is supported")
3839
}
3940

4041
bad, err := base64.StdEncoding.DecodeString(authData)
4142
if err != nil {
42-
err = fmt.Errorf("failed to base64 decode auth data: '%s' err: %+v", authData, err)
43-
44-
return
43+
return "", fmt.Errorf("failed to base64 decode auth data: '%s' err: %+v", authData, err)
4544
}
4645
fmt.Printf("Decoded Auth Data: %+v\n", string(bad))
4746

4847
pb, err := crypto.DecryptCipherText(cipherText, key1, nonce, authData)
4948
if err != nil {
50-
return
49+
return "", err
5150
}
5251

5352
return string(pb), nil
@@ -107,16 +106,16 @@ type CreateItemsKeyInput struct {
107106
// return nil
108107
// }
109108

110-
func splitContent(in string) (version, nonce, cipherText, authenticatedData string) {
109+
func splitContent(in string) (string, string, string, string) {
111110
components := strings.Split(in, ":")
112111
if len(components) < 3 {
113112
panic(components)
114113
}
115114

116-
version = components[0] // protocol version
117-
nonce = components[1] // encryption nonce
118-
cipherText = components[2] // ciphertext
119-
authenticatedData = components[3] // authenticated data
115+
version := components[0] // protocol version
116+
nonce := components[1] // encryption nonce
117+
cipherText := components[2] // ciphertext
118+
authenticatedData := components[3] // authenticated data
120119

121-
return
120+
return version, nonce, cipherText, authenticatedData
122121
}

healthcheck.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ func ItemKeysHealthcheck(input ItemsKeysHealthcheckInput) error {
2828
if !input.Session.Debug {
2929
prefix := color.HiWhite.Sprintf("retrieving items ")
3030

31-
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stdout))
31+
s := spinner.New(spinner.CharSets[14], spinnerDelay, spinner.WithWriter(os.Stdout))
3232
if input.UseStdOut {
33-
s = spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
33+
s = spinner.New(spinner.CharSets[14], spinnerDelay, spinner.WithWriter(os.Stderr))
3434
}
3535

3636
s.Prefix = prefix

helpers.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,19 @@ type EncryptedItemsFile struct {
130130
Items items.EncryptedItems `json:"items"`
131131
}
132132

133-
func readJSON(filePath string) (items items.EncryptedItems, err error) {
133+
func readJSON(filePath string) (items.EncryptedItems, error) {
134134
file, err := os.ReadFile(filePath)
135135
if err != nil {
136-
err = fmt.Errorf("%w failed to open: %s", err, filePath)
137-
return
136+
return nil, fmt.Errorf("%w failed to open: %s", err, filePath)
138137
}
139138

140139
var eif EncryptedItemsFile
141140

142-
err = json.Unmarshal(file, &eif)
143-
if err != nil {
144-
err = fmt.Errorf("failed to unmarshall json: %w", err)
145-
return
141+
if err = json.Unmarshal(file, &eif); err != nil {
142+
return nil, fmt.Errorf("failed to unmarshall json: %w", err)
146143
}
147144

148-
return eif.Items, err
145+
return eif.Items, nil
149146
}
150147

151148
func ItemRefsToYaml(irs []items.ItemReference) []ItemReferenceYAML {
@@ -198,12 +195,13 @@ func CommaSplit(i string) []string {
198195
return s
199196
}
200197

201-
func RemoveDeleted(in items.Items) (out items.Items) {
198+
func RemoveDeleted(in items.Items) items.Items {
199+
var out items.Items
202200
for _, i := range in {
203201
if !i.IsDeleted() {
204202
out = append(out, i)
205203
}
206204
}
207205

208-
return
206+
return out
209207
}

main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ func referenceExists(tag items.Tag, refID string) bool {
275275
return false
276276
}
277277

278-
func filterEncryptedItemsByTypes(ei items.EncryptedItems, types []string) (o items.EncryptedItems) {
278+
func filterEncryptedItemsByTypes(ei items.EncryptedItems, types []string) items.EncryptedItems {
279+
var o items.EncryptedItems
279280
for _, i := range ei {
280281
if StringInSlice(i.ContentType, types, true) {
281282
o = append(o, i)
@@ -285,7 +286,8 @@ func filterEncryptedItemsByTypes(ei items.EncryptedItems, types []string) (o ite
285286
return o
286287
}
287288

288-
func filterItemsByTypes(ei items.Items, types []string) (o items.Items) {
289+
func filterItemsByTypes(ei items.Items, types []string) items.Items {
290+
var o items.Items
289291
for _, i := range ei {
290292
if StringInSlice(i.GetContentType(), types, true) {
291293
o = append(o, i)
@@ -295,7 +297,8 @@ func filterItemsByTypes(ei items.Items, types []string) (o items.Items) {
295297
return o
296298
}
297299

298-
func filterCacheItemsByTypes(ei cache.Items, types []string) (o cache.Items) {
300+
func filterCacheItemsByTypes(ei cache.Items, types []string) cache.Items {
301+
var o cache.Items
299302
for _, i := range ei {
300303
if StringInSlice(i.ContentType, types, true) {
301304
o = append(o, i)
@@ -312,9 +315,9 @@ func (i *WipeConfig) Run() (int, error) {
312315
if !i.Session.Debug && i.UseStdOut {
313316
prefix := color.HiWhite.Sprintf("wiping ")
314317

315-
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stdout))
318+
s := spinner.New(spinner.CharSets[14], spinnerDelay, spinner.WithWriter(os.Stdout))
316319
if i.UseStdOut {
317-
s = spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
320+
s = spinner.New(spinner.CharSets[14], spinnerDelay, spinner.WithWriter(os.Stderr))
318321
}
319322

320323
s.Prefix = prefix

0 commit comments

Comments
 (0)