-
Notifications
You must be signed in to change notification settings - Fork 9
/
parsing.go
81 lines (63 loc) · 1.86 KB
/
parsing.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package runtime
import (
"encoding/json"
"fmt"
"os"
"namespacelabs.dev/foundation/schema/runtime"
)
type Server = runtime.Server
func LoadRuntimeConfig() (*runtime.RuntimeConfig, error) {
configBytes, err := os.ReadFile("/namespace/config/runtime.json")
if err != nil {
return nil, fmt.Errorf("failed to unwrap runtime configuration: %w", err)
}
rt := &runtime.RuntimeConfig{}
if err := json.Unmarshal(configBytes, rt); err != nil {
return nil, fmt.Errorf("failed to unmarshal runtime configuration: %w", err)
}
return rt, nil
}
func LoadBuildVCS() (*runtime.BuildVCS, error) {
serializedVCS, err := os.ReadFile("/namespace/config/buildvcs.json")
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("failed to load BuildVCS: %w", err)
}
vcs := &runtime.BuildVCS{}
if err := json.Unmarshal(serializedVCS, vcs); err != nil {
return nil, fmt.Errorf("failed to parse BuildVCS: %w", err)
}
return vcs, nil
}
func Endpoint(srv *Server, name string) (string, error) {
for _, s := range srv.Service {
if s.Name == name {
return s.Endpoint, nil
}
}
return "", fmt.Errorf("endpoint %s not found for server %s", name, srv.PackageName)
}
func FirstIngress(srv *Server, name string) *string {
for _, s := range srv.Service {
if s.Name == name {
if len(s.Ingress.GetDomain()) > 0 {
return &s.Ingress.Domain[0].BaseUrl
}
return nil
}
}
return nil
}
func ServerEndpoint(rtcfg *runtime.RuntimeConfig, pkg, name string) (string, error) {
for _, e := range rtcfg.StackEntry {
if e.PackageName == pkg {
return Endpoint(e, name)
}
}
return "", fmt.Errorf("server %s not found in runtime config stack", pkg)
}