Skip to content

Commit 644d19a

Browse files
sding3prarit
authored andcommitted
ensure gitlab API requests are canceled if user hits control-c
1 parent f4cc7b5 commit 644d19a

File tree

4 files changed

+37
-46
lines changed

4 files changed

+37
-46
lines changed

cmd/root_test.go

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

33
import (
44
"bytes"
5+
"context"
56
"io"
67
"math/rand"
78
"os"
@@ -65,7 +66,7 @@ func TestMain(m *testing.M) {
6566
if err != nil {
6667
log.Fatal(err)
6768
}
68-
lab.Init(host, u.Username, token, false)
69+
lab.Init(context.Background(), host, u.Username, token, false)
6970

7071
// Make "origin" the default remote for test cases calling
7172
// cmd.Run() directly, instead of launching the labBinaryPath

internal/gitlab/gitlab.go

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
package gitlab
66

77
import (
8+
"context"
89
"crypto/tls"
910
"crypto/x509"
1011
"fmt"
1112
"io"
1213
"io/ioutil"
13-
"net"
1414
"net/http"
1515
"os"
1616
"path/filepath"
@@ -69,49 +69,47 @@ func UserID() (int, error) {
6969
return u.ID, nil
7070
}
7171

72-
// Init initializes a gitlab client for use throughout lab.
73-
func Init(_host, _user, _token string, allowInsecure bool) {
72+
func initGitlabClient(ctx context.Context, _host, _user, _token string, tlsConfig *tls.Config) {
7473
if len(_host) > 0 && _host[len(_host)-1] == '/' {
7574
_host = _host[:len(_host)-1]
7675
}
7776
host = _host
7877
user = _user
7978
token = _token
8079

80+
tp := http.DefaultTransport.(*http.Transport).Clone()
81+
tp.TLSClientConfig = tlsConfig
8182
httpClient := &http.Client{
82-
Transport: &http.Transport{
83-
Proxy: http.ProxyFromEnvironment,
84-
DialContext: (&net.Dialer{
85-
Timeout: 30 * time.Second,
86-
KeepAlive: 30 * time.Second,
87-
DualStack: true,
88-
}).DialContext,
89-
ForceAttemptHTTP2: true,
90-
MaxIdleConns: 100,
91-
IdleConnTimeout: 90 * time.Second,
92-
TLSHandshakeTimeout: 10 * time.Second,
93-
ExpectContinueTimeout: 1 * time.Second,
94-
TLSClientConfig: &tls.Config{
95-
InsecureSkipVerify: allowInsecure,
96-
},
97-
},
83+
Transport: tp,
9884
}
9985

100-
lab, _ = gitlab.NewClient(token, gitlab.WithHTTPClient(httpClient), gitlab.WithBaseURL(host+"/api/v4"), gitlab.WithCustomLeveledLogger(log))
86+
lab, _ = gitlab.NewClient(token,
87+
gitlab.WithHTTPClient(httpClient),
88+
gitlab.WithBaseURL(host+"/api/v4"),
89+
gitlab.WithCustomLeveledLogger(log),
90+
gitlab.WithRequestOptions(gitlab.WithContext(ctx)),
91+
)
92+
}
93+
94+
// Init initializes a gitlab client for use throughout lab.
95+
func Init(ctx context.Context, _host, _user, _token string, allowInsecure bool) {
96+
initGitlabClient(ctx, _host, _user, _token, &tls.Config{
97+
InsecureSkipVerify: allowInsecure,
98+
})
10199
}
102100

103101
// InitWithCustomCA open the HTTP client using a custom CA file (a self signed
104102
// one for instance) instead of relying only on those installed in the current
105103
// system database
106-
func InitWithCustomCA(_host, _user, _token, caFile string) error {
104+
func InitWithCustomCA(ctx context.Context, _host, _user, _token, caFile string) error {
107105
if len(_host) > 0 && _host[len(_host)-1] == '/' {
108106
_host = _host[:len(_host)-1]
109107
}
110108
host = _host
111109
user = _user
112110
token = _token
113111

114-
caCert, err := ioutil.ReadFile(caFile)
112+
caCert, err := os.ReadFile(caFile)
115113
if err != nil {
116114
return err
117115
}
@@ -122,26 +120,9 @@ func InitWithCustomCA(_host, _user, _token, caFile string) error {
122120
}
123121
caCertPool.AppendCertsFromPEM(caCert)
124122

125-
httpClient := &http.Client{
126-
Transport: &http.Transport{
127-
Proxy: http.ProxyFromEnvironment,
128-
DialContext: (&net.Dialer{
129-
Timeout: 30 * time.Second,
130-
KeepAlive: 30 * time.Second,
131-
DualStack: true,
132-
}).DialContext,
133-
ForceAttemptHTTP2: true,
134-
MaxIdleConns: 100,
135-
IdleConnTimeout: 90 * time.Second,
136-
TLSHandshakeTimeout: 10 * time.Second,
137-
ExpectContinueTimeout: 1 * time.Second,
138-
TLSClientConfig: &tls.Config{
139-
RootCAs: caCertPool,
140-
},
141-
},
142-
}
143-
144-
lab, _ = gitlab.NewClient(token, gitlab.WithHTTPClient(httpClient), gitlab.WithBaseURL(host+"/api/v4"))
123+
initGitlabClient(ctx, _host, _user, _token, &tls.Config{
124+
RootCAs: caCertPool,
125+
})
145126
return nil
146127
}
147128

internal/gitlab/gitlab_test.go

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

33
import (
4+
"context"
45
"math/rand"
56
"os"
67
"path/filepath"
@@ -39,7 +40,7 @@ func TestMain(m *testing.M) {
3940
log.Fatal(err)
4041
}
4142

42-
Init(host, u.Username, token, false)
43+
Init(context.Background(), host, u.Username, token, false)
4344

4445
code := m.Run()
4546

main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package main
22

33
import (
4+
"context"
5+
"log"
46
"os"
7+
"os/signal"
58

69
"github.com/rsteube/carapace"
710
"github.com/zaquestion/lab/cmd"
@@ -13,15 +16,20 @@ import (
1316
var version = "master"
1417

1518
func main() {
19+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
20+
defer cancel()
21+
1622
cmd.Version = version
1723
initSkipped := skipInit()
1824
if !initSkipped {
1925
h, u, t, ca, skipVerify := config.LoadMainConfig()
2026

2127
if ca != "" {
22-
lab.InitWithCustomCA(h, u, t, ca)
28+
if err := lab.InitWithCustomCA(ctx, h, u, t, ca); err != nil {
29+
log.Fatal(err)
30+
}
2331
} else {
24-
lab.Init(h, u, t, skipVerify)
32+
lab.Init(ctx, h, u, t, skipVerify)
2533
}
2634
}
2735
cmd.Execute(initSkipped)

0 commit comments

Comments
 (0)