Skip to content

Commit 3e32107

Browse files
committed
small improvements
1 parent 29c34c0 commit 3e32107

File tree

9 files changed

+29
-66
lines changed

9 files changed

+29
-66
lines changed

cmd/print.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
"certalert/internal/certificates"
2020
"certalert/internal/config"
2121
"certalert/internal/print"
22-
"certalert/internal/utils"
2322
"fmt"
2423
"os"
24+
"slices"
2525
"strings"
2626

2727
"github.com/rs/zerolog/log"
@@ -55,7 +55,7 @@ Examples:
5555
certalert print my-cert --output yaml
5656
`,
5757
Run: func(cmd *cobra.Command, args []string) {
58-
if !utils.IsInList(outputFormat, supportedOutputFormats) {
58+
if !slices.Contains(supportedOutputFormats, outputFormat) {
5959
fmt.Printf("Unsupported output format: %s. Supported formats are: %s\n", outputFormat, strings.Join(supportedOutputFormats, ", "))
6060
cmd.Help()
6161
os.Exit(1)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module certalert
22

3-
go 1.21.0
3+
go 1.24
44

55
toolchain go1.24.1
66

internal/certificates/utils.go

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

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/rs/zerolog/log"
@@ -80,7 +81,7 @@ func certExistsInSlice(cert CertificateInfo, slice []CertificateInfo) bool {
8081
// If failOnError is true, returns an error with the provided error message (errMsg); otherwise, returns nil.
8182
func handleFailOnError(certInfoList *[]CertificateInfo, certName, certType, errMsg string, failOnError bool) error {
8283
if failOnError {
83-
return fmt.Errorf(errMsg)
84+
return errors.New(errMsg)
8485
}
8586
log.Warn().Msgf("Failed to extract certificate information: %v", errMsg)
8687
*certInfoList = append(*certInfoList, CertificateInfo{

internal/config/parse.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"certalert/internal/certificates"
55
"certalert/internal/resolve"
66
"certalert/internal/utils"
7+
"errors"
78
"fmt"
89
"path/filepath"
10+
"slices"
911
"strings"
1012

1113
"github.com/rs/zerolog/log"
@@ -46,7 +48,7 @@ func (c *Config) parseCertificatesConfig() (err error) {
4648
handleFailOnError := func(cert certificates.Certificate, idx int, errMsg string) error {
4749
if c.FailOnError {
4850
c.Certs[idx] = cert
49-
return fmt.Errorf(errMsg)
51+
return errors.New(errMsg)
5052
}
5153
log.Warn().Msg(errMsg)
5254
return nil
@@ -97,7 +99,7 @@ func (c *Config) parseCertificatesConfig() (err error) {
9799
}
98100

99101
// The Type can be specified in the config file, but it must be one of the supported types
100-
if !utils.IsInList(cert.Type, certificates.FileExtensionsTypesSorted) {
102+
if !slices.Contains(certificates.FileExtensionsTypesSorted, cert.Type) {
101103
if err := handleFailOnError(cert, idx, fmt.Sprintf("Certificate '%s' has an invalid type '%s'. Must be one of %s.", cert.Name, cert.Type, certificates.FileExtensionsTypesSorted)); err != nil {
102104
return err
103105
}
@@ -127,7 +129,7 @@ func (c *Config) parsePushgatewayConfig() (err error) {
127129
// handleFailOnError is a helper function to handle errors during pushgateway validation
128130
handleFailOnError := func(errMsg string) error {
129131
if c.FailOnError {
130-
return fmt.Errorf(errMsg)
132+
return errors.New(errMsg)
131133
}
132134
log.Warn().Msg(errMsg)
133135
return nil

internal/config/read.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"fmt"
55

6-
"github.com/mitchellh/mapstructure"
76
"github.com/spf13/viper"
87
)
98

@@ -27,7 +26,7 @@ func (c *Config) Read(configPath string) error {
2726
return fmt.Errorf("Failed to read config file: %v", err)
2827
}
2928

30-
if err := viper.Unmarshal(c, func(d *mapstructure.DecoderConfig) { d.ZeroFields = true }); err != nil {
29+
if err := viper.Unmarshal(c); err != nil {
3130
return fmt.Errorf("Failed to unmarshal config file: %v", err)
3231
}
3332

internal/config/read_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ func TestReadConfig(t *testing.T) {
5959
cfg := &Config{}
6060
err = cfg.Read(filePath.Name())
6161
assert.Error(t, err) // We expect an error because the file has incorrect content
62-
assert.Contains(t, err.Error(), "Failed to unmarshal config file: 1 error(s) decoding:\n\n* cannot parse 'failOnError' as bool: strconv.ParseBool: parsing \"not_an_integer\": invalid syntax")
62+
assert.Contains(t, err.Error(), "Failed to unmarshal config file: decoding failed due to the following error(s):\n\ncannot parse 'failOnError' as bool: strconv.ParseBool: parsing \"not_an_integer\": invalid syntax")
6363
})
6464
}

internal/test_helpers/test_helpers_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package test_helpers
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
@@ -29,7 +28,7 @@ func TestCreateTempFile(t *testing.T) {
2928
func TestReadFile(t *testing.T) {
3029
t.Run("successful file read", func(t *testing.T) {
3130
content := "hello, world"
32-
tmpfile, err := ioutil.TempFile("", "example.*.txt")
31+
tmpfile, err := os.CreateTemp("", "example.*.txt")
3332
if err != nil {
3433
t.Fatalf("Failed to create temp file: %v", err)
3534
}

internal/utils/utils.go

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,39 +69,19 @@ func BoolPtr(b bool) *bool {
6969
return &b
7070
}
7171

72-
// IsInList checks if a given value is present in a list of strings.
73-
//
74-
// Parameters:
75-
// - value: string
76-
// The value to check for in the list.
77-
// - list: []string
78-
// The list of strings to search for the specified value.
79-
//
80-
// Returns:
81-
// - bool
82-
// True if the value is found in the list, false otherwise.
83-
func IsInList(value string, list []string) bool {
84-
for _, v := range list {
85-
if value == v {
86-
return true
87-
}
88-
}
89-
return false
90-
}
91-
92-
// ExtractMapKeys is a utility function that takes an interface{} argument,
72+
// ExtractMapKeys is a utility function that takes an any argument,
9373
// checks if it's a map, and then returns the keys of that map as a slice of strings.
9474
// If the argument is not a map or the map's keys are not strings, it returns nil.
9575
//
9676
// Parameters:
97-
// - m: interface{}
77+
// - m: any
9878
// The input value, which is expected to be a map.
9979
//
10080
// Returns:
10181
// - []string
10282
// A slice of strings representing the keys of the map. If the input is not a map
10383
// or if the map's keys are not strings, it returns nil.
104-
func ExtractMapKeys(m interface{}) []string {
84+
func ExtractMapKeys(m any) []string {
10585
v := reflect.ValueOf(m) // Get the value of m
10686

10787
// Check if the value is of type 'Map'
@@ -194,15 +174,15 @@ func IsValidURL(str string) bool {
194174
// DeepCopy creates a deep copy of the source object and assigns it to the destination.
195175
//
196176
// Parameters:
197-
// - src: interface{}
177+
// - src: any
198178
// The source object to be copied.
199-
// - dest: interface{}
179+
// - dest: any
200180
// The destination object to which the copied value is assigned.
201181
//
202182
// Returns:
203183
// - error
204184
// An error if there was an issue during the copying process.
205-
func DeepCopy(src, dest interface{}) error {
185+
func DeepCopy(src, dest any) error {
206186
copied, err := copystructure.Copy(src)
207187
if err != nil {
208188
return err
@@ -215,16 +195,16 @@ func DeepCopy(src, dest interface{}) error {
215195
// HasStructField checks if a struct has a field with the specified key.
216196
//
217197
// Parameters:
218-
// - s: interface{}
198+
// - s: any
219199
// The struct object to be checked for the presence of the field.
220200
// - key: string
221201
// The key representing the field, which may include nested fields separated by dots.
222202
//
223203
// Returns:
224204
// - bool
225205
// True if the field with the given key exists, false otherwise.
226-
func HasStructField(s interface{}, key string) bool {
227-
v := reflect.ValueOf(s) // Obtain the Value of the passed interface{}
206+
func HasStructField(s any, key string) bool {
207+
v := reflect.ValueOf(s) // Obtain the Value of the passed any
228208

229209
keys := strings.Split(key, ".") // Split the key string by dots to handle nested keys
230210

internal/utils/utils_test.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,6 @@ func TestBoolPtr(t *testing.T) {
3535
})
3636
}
3737

38-
func TestIsInList(t *testing.T) {
39-
t.Run("element exists in the list", func(t *testing.T) {
40-
list := []string{"one", "two", "three"}
41-
element := "one"
42-
43-
if !IsInList(element, list) {
44-
t.Fatalf("'%s' should be in the list", element)
45-
}
46-
})
47-
48-
t.Run("element does not exist in the list", func(t *testing.T) {
49-
list := []string{"one", "two", "three"}
50-
element := "four"
51-
52-
if IsInList(element, list) {
53-
t.Fatalf("'%s' should not be in the list", element)
54-
}
55-
})
56-
}
57-
5838
func TestExtractMapKeys(t *testing.T) {
5939
t.Run("Valid map with string keys", func(t *testing.T) {
6040
input := map[string]int{
@@ -83,7 +63,7 @@ func TestExtractMapKeys(t *testing.T) {
8363
}
8464
})
8565
t.Run("Valid map with mixed keys", func(t *testing.T) {
86-
input := map[interface{}]string{
66+
input := map[any]string{
8767
1: "one",
8868
2: "two",
8969
3: "three",
@@ -123,7 +103,8 @@ func TestCheckFileAccessibility(t *testing.T) {
123103
defer tmpFile.Close()
124104
defer os.Remove(tmpFile.Name())
125105

126-
os.Chmod(tmpFile.Name(), 0222) // Write-only permissions
106+
// Write-only permissions
107+
os.Chmod(tmpFile.Name(), 0222) // nolint: errcheck
127108
err = CheckFileAccessibility(tmpFile.Name())
128109
if err == nil {
129110
t.Errorf("Expected a 'failed to open file' error, got nil")
@@ -138,7 +119,8 @@ func TestCheckFileAccessibility(t *testing.T) {
138119
defer tmpFile.Close()
139120
defer os.Remove(tmpFile.Name())
140121

141-
os.Chmod(tmpFile.Name(), 0444) // Read-only permissions
122+
// Read-only permissions
123+
os.Chmod(tmpFile.Name(), 0444) // nolint: errcheck
142124
err = CheckFileAccessibility(tmpFile.Name())
143125
if err != nil {
144126
t.Errorf("Expected no error for readable file, got %v", err)
@@ -356,7 +338,7 @@ func TestHasStructField(t *testing.T) {
356338
})
357339

358340
t.Run("field in interface", func(t *testing.T) {
359-
s := interface{}(&MyStruct{})
341+
s := any(&MyStruct{})
360342
key := "Name"
361343
expect := true
362344

@@ -368,7 +350,7 @@ func TestHasStructField(t *testing.T) {
368350
})
369351

370352
t.Run("field nested in interface", func(t *testing.T) {
371-
s := interface{}(&MyStruct{})
353+
s := any(&MyStruct{})
372354
key := "Inner.InnerField"
373355
expect := true
374356

0 commit comments

Comments
 (0)