/
client_meta_runtime.go
162 lines (132 loc) · 4.08 KB
/
client_meta_runtime.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package schema
import (
"context"
"fmt"
"github.com/selefra/selefra-utils/pkg/runtime_util"
"github.com/spf13/viper"
"strings"
"sync"
)
type ClientMetaRuntime struct {
// this runtime bind this context
myClientMeta *ClientMeta
client []any
itemMap map[string]any
itemMapLock sync.RWMutex
ProviderName string
ProviderVersion string
Workspace string
}
func NewClientMetaRuntime(ctx context.Context, workspace, providerName, providerVersion string, myClientMeta *ClientMeta, providerConfigViper *viper.Viper, isRunUserMetaInit bool) (runtime *ClientMetaRuntime, diagnostics *Diagnostics) {
diagnostics = NewDiagnostics()
runtime = &ClientMetaRuntime{
myClientMeta: myClientMeta,
// save workspace
Workspace: workspace,
ProviderName: providerName,
ProviderVersion: providerVersion,
}
myClientMeta.runtime = runtime
// bind context can not be nil
if runtime.myClientMeta == nil {
diagnostics.AddErrorMsg("ClientMetaRuntime bind ClientMeta must not nil")
return
}
runtime.itemMap = make(map[string]any)
runtime.itemMapLock = sync.RWMutex{}
// logger
if runtime.myClientMeta.InitLogger != nil && isRunUserMetaInit {
if diagnostics.Add(runtime.execInitLogger(ctx, providerConfigViper)).HasError() {
return nil, diagnostics
}
} else {
// Logger has a default value that allows users to log
clientLogger, err := NewDefaultClientLogger(NewDefaultClientLoggerConfig(workspace, providerName))
if err != nil {
return nil, diagnostics.AddErrorMsg("init default client logger error: %s", err.Error())
}
runtime.myClientMeta.ClientLogger = clientLogger
}
// client, Client has no default value, if the user sets it, it's passed in, and if it's not set, it's nil
if runtime.myClientMeta.InitClient != nil && isRunUserMetaInit {
if diagnostics.Add(runtime.execInitClient(ctx, providerConfigViper)).HasError() {
return nil, diagnostics
}
}
return
}
// for try catch panic
func (x *ClientMetaRuntime) execInitClient(ctx context.Context, config *viper.Viper) (diagnostics *Diagnostics) {
diagnostics = NewDiagnostics()
defer func() {
if r := recover(); r != nil {
msg := strings.Builder{}
msg.WriteString(fmt.Sprintf("client runtime exec InitClient panic: %#v", r))
diagnostics.AddErrorMsg(msg.String())
msg.WriteString("\nStack: \n")
msg.WriteString(runtime_util.Stack())
x.myClientMeta.Error(msg.String())
}
}()
client, d := x.myClientMeta.InitClient(ctx, x.myClientMeta, config)
if !diagnostics.AddDiagnostics(d).HasError() {
x.client = client
}
return diagnostics
}
func (x *ClientMetaRuntime) execInitLogger(ctx context.Context, config *viper.Viper) (diagnostics *Diagnostics) {
diagnostics = NewDiagnostics()
defer func() {
if r := recover(); r != nil {
msg := strings.Builder{}
msg.WriteString(fmt.Sprintf("client runtime exec InitLogger panic: %#v", r))
diagnostics.AddErrorMsg(msg.String())
// log do not initialized
//msg.WriteString("\nStack: \n")
//msg.WriteString(runtime_util.Stack())
//x.myClientMeta.Error(msg.String())
}
}()
logger, d := x.myClientMeta.InitLogger(ctx, x.myClientMeta, config)
if !diagnostics.AddDiagnostics(d).HasError() {
x.myClientMeta.ClientLogger = logger
}
return diagnostics
}
func (x *ClientMetaRuntime) SetItem(itemName string, itemValue any) {
x.itemMapLock.Lock()
defer x.itemMapLock.Unlock()
x.itemMap[itemName] = itemValue
}
func (x *ClientMetaRuntime) GetItem(itemName string) any {
x.itemMapLock.RLock()
defer x.itemMapLock.RUnlock()
return x.itemMap[itemName]
}
func (x *ClientMetaRuntime) GetStringItem(itemName, defaultValue string) string {
item := x.GetItem(itemName)
if item == nil {
return defaultValue
}
value, ok := item.(string)
if !ok {
return defaultValue
}
return value
}
func (x *ClientMetaRuntime) GetIntItem(itemName string, defaultValue int) int {
item := x.GetItem(itemName)
if item == nil {
return defaultValue
}
value, ok := item.(int)
if !ok {
return defaultValue
}
return value
}
func (x *ClientMetaRuntime) ClearItem() {
x.itemMapLock.RLock()
defer x.itemMapLock.RUnlock()
x.itemMap = make(map[string]any)
}