-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
118 lines (106 loc) · 2.91 KB
/
engine.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
package main
import (
"strings"
"github.com/gin-gonic/gin"
"github.com/iver-wharf/wharf-api/v5/pkg/model/response"
)
type engineModule struct {
CIConfig *CIConfig
}
func (m engineModule) Register(r *gin.RouterGroup) {
r.GET("/engine", m.getEngineList)
}
// getEngineList godoc
// @id getEngineList
// @summary Get list of engines.
// @description Added in v5.1.0.
// @tags engine
// @produce json
// @param pretty query bool false "Pretty indented JSON output"
// @success 200 {object} response.EngineList "Engines"
// @failure 401 {object} problem.Response "Unauthorized or missing jwt token"
// @router /engine [get]
func (m engineModule) getEngineList(c *gin.Context) {
if m.CIConfig == nil {
c.JSON(200, response.EngineList{})
return
}
conf := *m.CIConfig
var res response.EngineList
if defaultEng, hasDefault := getDefaultEngineFromConfig(conf); hasDefault {
resDefaultEng := convCIEngineToResponse(defaultEng)
res.DefaultEngine = &resDefaultEng
}
engines := getEnginesFromConfig(conf)
res.List = convCIEnginesToResponses(engines)
renderJSON(c, 200, res)
}
func getEnginesFromConfig(ciConf CIConfig) []CIEngineConfig {
var engines []CIEngineConfig
if ciConf.Engine.URL != "" {
engines = append(engines, ciConf.Engine)
}
if ciConf.Engine2.URL != "" {
engines = append(engines, ciConf.Engine2)
}
return engines
}
func getDefaultEngineFromConfig(ciConf CIConfig) (CIEngineConfig, bool) {
switch {
case ciConf.Engine.URL != "":
return ciConf.Engine, true
case ciConf.Engine2.URL != "":
return ciConf.Engine2, true
default:
return CIEngineConfig{}, false
}
}
func lookupEngineOrDefaultFromConfig(ciConf CIConfig, id string) (CIEngineConfig, bool) {
switch {
case ciConf.MockTriggerResponse:
return CIEngineConfig{
ID: "mock",
Name: "Mock engine",
URL: "http://mock-url.wharf-api.localhost",
}, true
case id == "":
return getDefaultEngineFromConfig(ciConf)
default:
return lookupEngineFromConfig(ciConf, id)
}
}
func lookupEngineFromConfig(ciConf CIConfig, id string) (CIEngineConfig, bool) {
switch {
case id == "":
return getDefaultEngineFromConfig(ciConf)
case strings.EqualFold(ciConf.Engine.ID, id):
return ciConf.Engine, true
case strings.EqualFold(ciConf.Engine2.ID, id):
return ciConf.Engine2, true
default:
return CIEngineConfig{}, false
}
}
func convCIEngineToResponse(engine CIEngineConfig) response.Engine {
return response.Engine{
ID: engine.ID,
Name: engine.Name,
URL: engine.URL,
API: string(engine.API),
}
}
func convCIEnginesToResponses(engines []CIEngineConfig) []response.Engine {
resEngines := make([]response.Engine, len(engines))
for i, e := range engines {
resEngines[i] = convCIEngineToResponse(e)
}
return resEngines
}
func lookupResponseEngineFromConfig(ciConf CIConfig, id string) *response.Engine {
engine, ok := lookupEngineFromConfig(ciConf, id)
if !ok {
return nil
}
resEngine := convCIEngineToResponse(engine)
return &resEngine
}