Skip to content

Commit 4abee23

Browse files
committed
Linting fixes
1 parent 4ab9b9b commit 4abee23

File tree

3 files changed

+64
-46
lines changed

3 files changed

+64
-46
lines changed

examples/custom_store/main.go

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io/ioutil"
9+
"io"
10+
"log"
1011
"net/http"
1112
"net/http/httptest"
1213
"os"
@@ -25,6 +26,9 @@ const (
2526
password = "postgres"
2627
dbName = "postgres"
2728
port = 5432
29+
30+
maxOpenConns = 5
31+
maxIdleConns = 2
2832
)
2933

3034
// This example assumes a PostgreSQL database running on localhost:5432
@@ -39,47 +43,51 @@ func main() {
3943
return
4044
}
4145

42-
fmt.Println(err)
43-
os.Exit(1)
46+
log.Fatal(err)
4447
}
4548

4649
func Run() error {
4750
var err error
4851

49-
url, close := setupTestServer()
50-
defer close()
51-
52-
store, err := NewHTTPTestConnectingStore(url, "GET", nil, func(r *http.Response) (driver.Credentials, error) {
53-
b, err := ioutil.ReadAll(r.Body)
54-
if err != nil {
55-
return nil, err
56-
}
57-
58-
var v map[string]interface{}
59-
if err := json.Unmarshal([]byte(b), &v); err != nil {
60-
return nil, err
61-
}
62-
63-
username, ok := v["username"].(string)
64-
if !ok {
65-
return nil, errors.New("missing username")
66-
}
67-
68-
password, ok := v["password"].(string)
69-
if !ok {
70-
return nil, errors.New("missing password")
71-
}
72-
73-
return &store.Credential{
74-
Username: username,
75-
Password: password,
76-
}, nil
77-
})
52+
u, serverClose := setupTestServer()
53+
defer serverClose()
54+
55+
s, err := NewHTTPTestConnectingStore(
56+
u,
57+
"GET",
58+
nil,
59+
func(r *http.Response) (driver.Credentials, error) {
60+
b, err := io.ReadAll(r.Body)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
var v map[string]any
66+
if err := json.Unmarshal([]byte(b), &v); err != nil {
67+
return nil, err
68+
}
69+
70+
username, ok := v["username"].(string)
71+
if !ok {
72+
return nil, errors.New("missing username")
73+
}
74+
75+
password, ok := v["password"].(string)
76+
if !ok {
77+
return nil, errors.New("missing password")
78+
}
79+
80+
return &store.Credential{
81+
Username: username,
82+
Password: password,
83+
}, nil
84+
},
85+
)
7886
if err != nil {
7987
return err
8088
}
8189

82-
c, err := driver.NewConnector(store, "pgx", &driver.Config{
90+
c, err := driver.NewConnector(s, "pgx", &driver.Config{
8391
Host: host,
8492
Port: port,
8593
DB: dbName,
@@ -98,13 +106,13 @@ func Run() error {
98106
// connection lifetime very short. In a production environment, Vault role TTLs and
99107
// connection lifetime should be tuned based on database performance requirements.
100108
database.SetConnMaxLifetime(2 * time.Second)
101-
database.SetMaxIdleConns(2)
102-
database.SetMaxOpenConns(5)
109+
database.SetMaxIdleConns(maxIdleConns)
110+
database.SetMaxOpenConns(maxOpenConns)
103111

104112
ctx, cancel := context.WithCancel(context.Background())
105113
defer cancel()
106114

107-
appSignal := make(chan os.Signal)
115+
appSignal := make(chan os.Signal, 2)
108116
signal.Notify(appSignal, os.Interrupt)
109117

110118
go func() {
@@ -116,7 +124,8 @@ func Run() error {
116124
// First ping the DB to open a connection
117125
err = db.Ping(ctx, database)
118126
if err != nil {
119-
fmt.Println(err)
127+
log.Println(err)
128+
120129
break
121130
}
122131

@@ -126,21 +135,25 @@ func Run() error {
126135
// Now get users
127136
users, err := db.QueryUsers(ctx, database, nil)
128137
if err != nil {
129-
fmt.Println(err)
138+
log.Println(err)
139+
130140
break
131141
}
132142

133-
fmt.Println(users)
143+
log.Printf("Users: %v", users)
134144
}
135145

136146
return err
137147
}
138148

139149
func setupTestServer() (string, func()) {
140-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
150+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
151+
log.Println("Server: retrieved credentials")
141152
w.Header().Set("Content-Type", "application/json")
142153
resp := fmt.Sprintf(`{"username": "%s", "password": "%s"}`, username, password)
143-
json.NewEncoder(w).Encode(json.RawMessage(resp))
154+
if err := json.NewEncoder(w).Encode(json.RawMessage(resp)); err != nil {
155+
w.WriteHeader(http.StatusInternalServerError)
156+
}
144157
}))
145158

146159
return ts.URL, ts.Close

examples/custom_store/store.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ type ResponseHandler func(r *http.Response) (driver.Credentials, error)
1313

1414
// HTTPTestConnectingStore is a store implementation that connects to an endpoint to retrieve credentials
1515
type HTTPTestConnectingStore struct {
16-
url string
17-
method string
1816
headers http.Header
1917
handler ResponseHandler
18+
url string
19+
method string
2020
}
2121

2222
// NewHTTPTestConnectingStore creates a new instance of a HTTPTestConnectingStore
23-
func NewHTTPTestConnectingStore(url, method string, headers http.Header, handler ResponseHandler) (*HTTPTestConnectingStore, error) { //nolint:revive
23+
func NewHTTPTestConnectingStore(
24+
url, method string,
25+
headers http.Header,
26+
handler ResponseHandler,
27+
) (*HTTPTestConnectingStore, error) { //nolint:revive
2428
if handler == nil {
2529
return nil, errors.New("handler must be implemented")
2630
}

examples/db/db.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
"context"
55
"database/sql"
66
"fmt"
7+
"log"
78
)
89

910
// Ping the database to verify DSN provided by the user is valid and the
1011
// server accessible. If the ping fails exit the program with an error.
1112
func Ping(ctx context.Context, db *sql.DB) error {
12-
fmt.Println("ping")
13+
log.Println("PING")
1314
if err := db.PingContext(ctx); err != nil {
1415
return fmt.Errorf("unable to connect to database: %w", err)
1516
}
16-
fmt.Println("pong")
17+
log.Println("PONG")
1718

1819
return nil
1920
}

0 commit comments

Comments
 (0)