-
Notifications
You must be signed in to change notification settings - Fork 4
/
bunny.go
61 lines (46 loc) · 1.38 KB
/
bunny.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
60
61
package lib
import (
"context"
"bunnyshell.com/cli/pkg/build"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/net"
"bunnyshell.com/sdk"
)
func GetProfile() config.Profile {
return config.GetSettings().Profile
}
func GetAPI() *sdk.APIClient {
return GetAPIFromProfile(GetProfile())
}
func GetContext() (context.Context, context.CancelFunc) {
return GetContextFromProfile(GetProfile())
}
func GetAPIFromProfile(profile config.Profile) *sdk.APIClient {
return sdk.NewAPIClient(profileToConfiguration(profile))
}
func GetContextFromProfile(profile config.Profile) (context.Context, context.CancelFunc) {
ctx := context.WithValue(context.Background(), sdk.ContextAPIKeys, map[string]sdk.APIKey{
"ApiKeyAuth": {
Key: profile.Token,
},
})
timeout := config.GetSettings().Timeout
if timeout == 0 {
return ctx, func() {}
}
return context.WithTimeout(ctx, timeout)
}
func profileToConfiguration(profile config.Profile) *sdk.Configuration {
configuration := getDefaultConfiguration()
if profile.Host != "" {
configuration.Host = profile.Host
}
return configuration
}
func getDefaultConfiguration() *sdk.Configuration {
configuration := sdk.NewConfiguration()
configuration.UserAgent = "BunnyCLI+" + build.Version + "/" + configuration.UserAgent
configuration.Debug = config.GetSettings().Debug
configuration.HTTPClient = net.GetCLIClient()
return configuration
}