-
Notifications
You must be signed in to change notification settings - Fork 0
/
codefly.go
127 lines (109 loc) · 2.54 KB
/
codefly.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
package codefly
import (
"context"
"fmt"
"github.com/codefly-dev/core/resources"
"github.com/codefly-dev/core/wool"
"os"
"runtime/debug"
"strings"
)
func CatchPanic(ctx context.Context) {
w := wool.Get(ctx).In("codefly.CatchPanic")
if r := recover(); r != nil {
w.Error("Caught panic", wool.Field("panic", r), wool.Field("stack", string(debug.Stack())))
os.Exit(1)
}
}
func GetLogLevel() wool.Loglevel {
if os.Getenv("CODEFLY_SDK__LOGLEVEL") == "debug" {
return wool.DEBUG
} else if os.Getenv("CODEFLY_SDK__LOGLEVEL") == "trace" {
return wool.TRACE
}
return wool.INFO
}
func init() {
_, err := Init(context.Background())
if err != nil {
fmt.Println("Cannot initialize codefly", err)
}
}
func Init(ctx context.Context) (*wool.Provider, error) {
var err error
root, err = os.Getwd()
if err != nil {
return nil, err
}
err = LoadEnvironmentVariables()
if err != nil {
return nil, err
}
// For logging before we get the runningService
var provider *wool.Provider
err = LoadService(ctx)
if err != nil {
return nil, err
}
if runningService == nil {
fmt.Println("No service configuration found.")
provider = wool.New(ctx, resources.CLI.AsResource()).WithConsole(GetLogLevel())
} else {
// Now update the provider
provider = wool.New(ctx, runningService.Identity().AsResource()).WithConsole(GetLogLevel())
}
ctx = provider.Inject(ctx)
return provider, nil
}
var root string
var runningService *resources.Service
var runningCtx context.Context
func LoadService(ctx context.Context) error {
w := wool.Get(ctx).In("codefly.LoadService")
w.Debug("root", wool.Field("root", root))
dir, errFind := resources.FindUp[resources.Service](ctx)
if errFind != nil {
return errFind
}
if dir != nil {
svc, err := resources.LoadServiceFromDir(ctx, *dir)
if err != nil {
return err
}
w.Debug("loaded service", wool.Field("service", svc.Unique()))
runningService = svc
return nil
}
return w.NewError("no service found")
}
func Version() string {
if runningService == nil {
return "unknown"
}
return runningService.Version
}
func Service() *resources.Service {
return runningService
}
var envs []string
var uniqueEnvs = make(map[string]bool)
func LoadEnvironmentVariables() error {
for _, env := range os.Environ() {
if !strings.HasPrefix(env, "CODEFLY") {
continue
}
if _, ok := uniqueEnvs[env]; ok {
continue
}
uniqueEnvs[env] = true
envs = append(envs, env)
}
return nil
}
type EndpointOverride struct {
Name string
Override string
}
type Configuration struct {
Endpoints []EndpointOverride
}