Skip to content

Commit 488d5a2

Browse files
committed
refactor: added more documentation, upgraded dependencies, and substituted go-logger by slog
1 parent 5af8a32 commit 488d5a2

File tree

13 files changed

+347
-96
lines changed

13 files changed

+347
-96
lines changed

cloud/gcloud/loader.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import (
1010
)
1111

1212
// LoadGoogleCredentials loads the Google credentials
13+
//
14+
// Parameters:
15+
//
16+
// - ctx: context.Context
17+
//
18+
// Returns:
19+
//
20+
// - *google.Credentials: Google credentials
21+
// - error: error if any
1322
func LoadGoogleCredentials(ctx context.Context) (*google.Credentials, error) {
1423
credentials, err := google.FindDefaultCredentials(ctx)
1524
if err != nil {
@@ -19,6 +28,17 @@ func LoadGoogleCredentials(ctx context.Context) (*google.Credentials, error) {
1928
}
2029

2130
// LoadServiceAccountCredentials loads the service account credentials
31+
//
32+
// Parameters:
33+
//
34+
// - ctx: context.Context
35+
// - url: string
36+
// - credentials: *google.Credentials
37+
//
38+
// Returns:
39+
//
40+
// - *oauth.TokenSource: OAuth2 token source
41+
// - error: error if any
2242
func LoadServiceAccountCredentials(
2343
ctx context.Context, url string, credentials *google.Credentials,
2444
) (*oauth.TokenSource, error) {

cloud/gcloud/logger.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
11
package gcloud
22

33
import (
4-
gologgermode "github.com/ralvarezdev/go-logger/mode"
5-
gologgermodenamed "github.com/ralvarezdev/go-logger/mode/named"
4+
"log/slog"
5+
66
"google.golang.org/grpc/credentials/oauth"
77
)
88

9-
// Logger is the logger for Google Cloud
10-
type Logger struct {
11-
logger gologgermodenamed.Logger
12-
}
13-
14-
// NewLogger is the logger for Google Cloud
15-
func NewLogger(header string, modeLogger gologgermode.Logger) (*Logger, error) {
16-
// Initialize the mode named logger
17-
namedLogger, err := gologgermodenamed.NewDefaultLogger(header, modeLogger)
18-
if err != nil {
19-
return nil, err
20-
}
21-
22-
return &Logger{logger: namedLogger}, nil
23-
}
24-
259
// FailedToLoadTokenSource logs the failed to load token source
26-
func (l *Logger) FailedToLoadTokenSource(err error) {
27-
l.logger.Error(
28-
"failed to load token source",
29-
err,
30-
)
10+
//
11+
// Parameters:
12+
//
13+
// - err: the error
14+
// - logger: the logger
15+
func FailedToLoadTokenSource(err error, logger *slog.Logger) {
16+
if logger != nil {
17+
logger.Error(
18+
"failed to load token source",
19+
slog.String("error", err.Error()),
20+
)
21+
}
3122
}
3223

3324
// LoadedTokenSource logs the loaded token source
34-
func (l *Logger) LoadedTokenSource(tokenSource *oauth.TokenSource) {
25+
//
26+
// Parameters:
27+
//
28+
// - tokenSource: the token source
29+
// - logger: the logger
30+
func LoadedTokenSource(tokenSource *oauth.TokenSource, logger *slog.Logger) {
3531
// Check if the token source is nil
3632
if tokenSource == nil {
37-
l.FailedToLoadTokenSource(ErrNilTokenSource)
33+
FailedToLoadTokenSource(ErrNilTokenSource, logger)
3834
return
3935
}
4036

4137
// Get the access token from the token source
4238
token, err := tokenSource.Token()
4339
if err != nil {
44-
l.FailedToLoadTokenSource(err)
40+
FailedToLoadTokenSource(err, logger)
4541
return
4642
}
4743

48-
l.logger.Debug(
49-
"loaded token source",
50-
token.AccessToken,
51-
)
44+
if logger != nil {
45+
logger.Debug(
46+
"loaded token source",
47+
slog.String("access_token", token.AccessToken),
48+
)
49+
}
5250
}

env/errors.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package env
22

3-
import "errors"
3+
import (
4+
"errors"
5+
)
6+
7+
const (
8+
ErrNilDestination = "destination cannot be nil for the given key: %v"
9+
ErrEnvironmentVariableNotFound = "environment variable not found: %v"
10+
ErrInvalidDuration = "invalid key '%v' duration value: %v"
11+
ErrInvalidInteger = "invalid key '%v' integer value: %v"
12+
)
413

514
var (
615
ErrNilLoader = errors.New("loader cannot be nil")
7-
ErrNilDestination = "destination cannot be nil for the given key: %v"
8-
ErrEnvironmentVariableNotFound = "environment variable not found: %v"
916
ErrFailedToLoadEnvironmentVariables = errors.New("failed to load environment variables")
10-
ErrInvalidDuration = "invalid key '%v' duration value: %v"
11-
ErrInvalidInteger = "invalid key '%v' integer value: %v"
1217
)

env/interfaces.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package env
2+
3+
import (
4+
"time"
5+
)
6+
7+
type (
8+
// Loader interface for loading environment variables
9+
Loader interface {
10+
LoadVariable(key string, dest *string) error
11+
LoadDurationVariable(key string, dest *time.Duration) error
12+
LoadSecondsVariable(key string, dest *float64) error
13+
LoadIntVariable(key string, dest *int) error
14+
}
15+
)

env/loader.go

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@ package env
22

33
import (
44
"fmt"
5+
"log/slog"
56
"os"
67
"strconv"
78
"time"
89
)
910

1011
type (
11-
// Loader interface for loading environment variables
12-
Loader interface {
13-
LoadVariable(key string, dest *string) error
14-
LoadDurationVariable(key string, dest *time.Duration) error
15-
LoadSecondsVariable(key string, dest *float64) error
16-
LoadIntVariable(key string, dest *int) error
17-
}
18-
1912
// DefaultLoader struct
2013
DefaultLoader struct {
21-
logger *Logger
14+
logger *slog.Logger
2215
}
2316
)
2417

2518
// NewDefaultLoader creates a new default environment variable loader
26-
func NewDefaultLoader(loadFn func() error, logger *Logger) (
19+
//
20+
// Parameters:
21+
//
22+
// - loadFn: function to load the environment variables (e.g., from a file)
23+
// - logger: logger instance for logging
24+
//
25+
// Returns:
26+
//
27+
// - *DefaultLoader: instance of DefaultLoader
28+
// - error: error if any occurred during loading
29+
func NewDefaultLoader(loadFn func() error, logger *slog.Logger) (
2730
*DefaultLoader,
2831
error,
2932
) {
@@ -33,11 +36,25 @@ func NewDefaultLoader(loadFn func() error, logger *Logger) (
3336
return nil, ErrFailedToLoadEnvironmentVariables
3437
}
3538
}
39+
40+
// Prepare the logger
41+
if logger != nil {
42+
logger = logger.With("component", "env_loader")
43+
}
3644
return &DefaultLoader{logger}, nil
3745
}
3846

3947
// LoadVariable load variable from environment variables
40-
func (d *DefaultLoader) LoadVariable(key string, dest *string) error {
48+
//
49+
// Parameters:
50+
//
51+
// - key: environment variable key
52+
// - dest: pointer to the destination string where the value will be stored
53+
//
54+
// Returns:
55+
//
56+
// - error: error if any occurred during loading or parsing
57+
func (d DefaultLoader) LoadVariable(key string, dest *string) error {
4158
// Check if the destination is nil
4259
if dest == nil {
4360
return fmt.Errorf(ErrNilDestination, key)
@@ -51,15 +68,22 @@ func (d *DefaultLoader) LoadVariable(key string, dest *string) error {
5168
*dest = variable
5269

5370
// Log the environment variable
54-
if d.logger != nil {
55-
d.logger.EnvironmentVariableLoaded(key)
56-
}
71+
EnvironmentVariableLoaded(d.logger, key)
5772

5873
return nil
5974
}
6075

6176
// LoadDurationVariable load duration variable from environment variables
62-
func (d *DefaultLoader) LoadDurationVariable(
77+
//
78+
// Parameters:
79+
//
80+
// - key: environment variable key
81+
// - dest: pointer to the destination time.Duration where the value will be stored
82+
//
83+
// Returns:
84+
//
85+
// - error: error if any occurred during loading or parsing
86+
func (d DefaultLoader) LoadDurationVariable(
6387
key string,
6488
dest *time.Duration,
6589
) error {
@@ -84,7 +108,16 @@ func (d *DefaultLoader) LoadDurationVariable(
84108
}
85109

86110
// LoadSecondsVariable load duration variable in seconds from environment variables
87-
func (d *DefaultLoader) LoadSecondsVariable(key string, dest *float64) error {
111+
//
112+
// Parameters:
113+
//
114+
// - key: environment variable key
115+
// - dest: pointer to the destination float64 where the value in seconds will be stored
116+
//
117+
// Returns:
118+
//
119+
// - error: error if any occurred during loading or parsing
120+
func (d DefaultLoader) LoadSecondsVariable(key string, dest *float64) error {
88121
// Check if the destination is nil
89122
if dest == nil {
90123
return fmt.Errorf(ErrNilDestination, key)
@@ -100,7 +133,16 @@ func (d *DefaultLoader) LoadSecondsVariable(key string, dest *float64) error {
100133
}
101134

102135
// LoadIntVariable load integer variable from environment variables
103-
func (d *DefaultLoader) LoadIntVariable(key string, dest *int) error {
136+
//
137+
// Parameters:
138+
//
139+
// - key: environment variable key
140+
// - dest: pointer to the destination int where the value will be stored
141+
//
142+
// Returns:
143+
//
144+
// - error: error if any occurred during loading or parsing
145+
func (d DefaultLoader) LoadIntVariable(key string, dest *int) error {
104146
// Check if the destination is nil
105147
if dest == nil {
106148
return fmt.Errorf(ErrNilDestination, key)

env/logger.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
package env
22

33
import (
4-
gologgermode "github.com/ralvarezdev/go-logger/mode"
5-
gologgermodenamed "github.com/ralvarezdev/go-logger/mode/named"
4+
"log/slog"
65
)
76

8-
// Logger is the environment logger
9-
type Logger struct {
10-
logger gologgermodenamed.Logger
11-
}
12-
13-
// NewLogger creates a new environment logger
14-
func NewLogger(header string, modeLogger gologgermode.Logger) (*Logger, error) {
15-
// Initialize the mode named logger
16-
namedLogger, err := gologgermodenamed.NewDefaultLogger(header, modeLogger)
17-
if err != nil {
18-
return nil, err
19-
}
20-
21-
return &Logger{logger: namedLogger}, nil
22-
}
23-
247
// EnvironmentVariableLoaded logs a message when an environment variable is loaded
25-
func (l *Logger) EnvironmentVariableLoaded(variablesName ...string) {
26-
l.logger.Debug(
27-
"environment variable loaded",
28-
variablesName...,
29-
)
8+
//
9+
// Parameters:
10+
//
11+
// - logger: The slog.Logger instance to use for logging.
12+
// - variablesName: A variadic list of environment variable names that were loaded.
13+
func EnvironmentVariableLoaded(logger *slog.Logger, variablesName ...string) {
14+
if logger != nil {
15+
logger.Debug(
16+
"environment variable loaded",
17+
slog.Any("variables", variablesName),
18+
)
19+
}
3020
}

filesystem/errors.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"errors"
55
)
66

7-
var (
7+
const (
88
ErrUnableToReadFile = "unable to read file: %v"
9-
ErrNilFile = errors.New("file cannot be nil")
9+
)
10+
11+
var (
12+
ErrNilFile = errors.New("file cannot be nil")
1013
)

0 commit comments

Comments
 (0)