-
Notifications
You must be signed in to change notification settings - Fork 0
/
spring_detector.go
145 lines (129 loc) · 3.71 KB
/
spring_detector.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
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc.
******************************************************************************/
package enricher
import (
"context"
"errors"
"io/ioutil"
"path/filepath"
"github.com/mike-hoang/alizer-copy/go/pkg/apis/model"
"github.com/mike-hoang/alizer-copy/go/pkg/utils"
"gopkg.in/yaml.v3"
)
type SpringDetector struct{}
type ApplicationProsServer struct {
Server struct {
Port int `yaml:"port,omitempty"`
Http struct {
Port int `yaml:"port,omitempty"`
} `yaml:"http,omitempty"`
} `yaml:"server,omitempty"`
}
func (s SpringDetector) GetSupportedFrameworks() []string {
return []string{"Spring", "Spring Boot"}
}
// DoFrameworkDetection uses the groupId to check for the framework name
func (s SpringDetector) DoFrameworkDetection(language *model.Language, config string) {
if hasFwk, _ := hasFramework(config, "org.springframework", ""); hasFwk {
language.Frameworks = append(language.Frameworks, s.GetSupportedFrameworks()...)
}
}
// DoPortsDetection searches for ports in the env var and
// src/main/resources/application.properties, or src/main/resources/application.yaml
func (s SpringDetector) DoPortsDetection(component *model.Component, ctx *context.Context) {
// check if port is set on env var
ports := getSpringPortsFromEnvs()
if len(ports) > 0 {
component.Ports = ports
return
}
applicationFile := utils.GetAnyApplicationFilePath(component.Path, []model.ApplicationFileInfo{
{
Dir: "src/main/resources",
File: "application.properties",
},
{
Dir: "src/main/resources",
File: "application.yml",
},
{
Dir: "src/main/resources",
File: "application.yaml",
},
}, ctx)
if applicationFile == "" {
return
}
var err error
if filepath.Ext(applicationFile) == ".yml" || filepath.Ext(applicationFile) == ".yaml" {
ports, err = getServerPortsFromYamlFile(applicationFile)
} else {
ports, err = getServerPortsFromPropertiesFile(applicationFile)
}
if err != nil {
return
}
component.Ports = ports
}
func getSpringPortsFromEnvs() []int {
return utils.GetValidPortsFromEnvs([]string{"SERVER_PORT", "SERVER_HTTP_PORT"})
}
func getServerPortsFromPropertiesFile(file string) ([]int, error) {
props, err := utils.ConvertPropertiesFileAsPathToMap(file)
if err != nil {
return []int{}, err
}
ports := getPortsFromMap(props, []string{"server.port", "server.http.port"})
if len(ports) > 0 {
return ports, nil
}
return []int{}, errors.New("no port found")
}
func getPortsFromMap(props map[string]string, keys []string) []int {
var ports []int
for _, key := range keys {
port := getPortFromMap(props, key)
if port != -1 {
ports = append(ports, port)
}
}
return ports
}
func getPortFromMap(props map[string]string, key string) int {
if portValue, exists := props[key]; exists {
if port, err := utils.GetValidPort(portValue); err == nil {
return port
}
}
return -1
}
func getServerPortsFromYamlFile(file string) ([]int, error) {
yamlFile, err := ioutil.ReadFile(file)
if err != nil {
return []int{}, err
}
var data ApplicationProsServer
err = yaml.Unmarshal(yamlFile, &data)
if err != nil {
return []int{}, err
}
var ports []int
if data.Server.Port > 0 {
ports = append(ports, data.Server.Port)
}
if data.Server.Http.Port > 0 {
ports = append(ports, data.Server.Http.Port)
}
if len(ports) > 0 {
return ports, nil
}
return []int{}, errors.New("no port found")
}