-
Notifications
You must be signed in to change notification settings - Fork 10
/
GitContextUtils.go
59 lines (50 loc) · 1.19 KB
/
GitContextUtils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package git
import (
"context"
"time"
)
type GitContext struct {
context.Context // Embedding original Go context
Username string
Password string
CloningMode string
}
func (gitCtx GitContext) WithCredentials(Username string, Password string) GitContext {
gitCtx.Username = Username
gitCtx.Password = Password
return gitCtx
}
func BuildGitContext(ctx context.Context) GitContext {
return GitContext{
Context: ctx,
}
}
func (gitCtx GitContext) WithTimeout(timeoutSeconds int) (GitContext, context.CancelFunc) {
ctx, cancel := context.WithTimeout(gitCtx.Context, time.Duration(timeoutSeconds)*time.Second)
gitCtx.Context = ctx
return gitCtx, cancel
}
func (gitCtx GitContext) WithCloningMode(CloningMode string) GitContext {
gitCtx.CloningMode = CloningMode
return gitCtx
}
func RunWithTimeout[T any](ctx context.Context, f func() ([]*T, error)) ([]*T, error) {
resultCh := make(chan []*T)
errCh := make(chan error)
go func() {
result, err := f()
if err != nil {
errCh <- err
return
}
resultCh <- result
}()
select {
case result := <-resultCh:
return result, nil
case err := <-errCh:
return nil, err
case <-ctx.Done():
return nil, ctx.Err()
}
}