forked from redhat-developer/odo
/
odo_utils.go
90 lines (81 loc) · 2.93 KB
/
odo_utils.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
package helper
import (
"fmt"
"regexp"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// GetConfigValue returns a local config value of given key or
// returns an empty string if value is not set
func GetConfigValue(key string) string {
return GetConfigValueWithContext(key, "")
}
// GetConfigValueWithContext returns a local config value of given key and contextdir or
// returns an empty string if value is not set
func GetConfigValueWithContext(key string, context string) string {
var stdOut string
if context != "" {
stdOut = CmdShouldPass("odo", "config", "view", "--context", context)
} else {
stdOut = CmdShouldPass("odo", "config", "view")
}
re := regexp.MustCompile(key + `.+`)
odoConfigKeyValue := re.FindString(stdOut)
if odoConfigKeyValue == "" {
return fmt.Sprintf("%s not found", key)
}
trimKeyValue := strings.TrimSpace(odoConfigKeyValue)
if strings.Compare(key, trimKeyValue) != 0 {
return strings.TrimSpace(strings.SplitN(trimKeyValue, " ", 2)[1])
}
return ""
}
// GetPreferenceValue a global config value of given key or
// returns an empty string if value is not set
func GetPreferenceValue(key string) string {
stdOut := CmdShouldPass("odo", "preference", "view")
re := regexp.MustCompile(key + `.+`)
odoConfigKeyValue := re.FindString(stdOut)
if odoConfigKeyValue == "" {
return fmt.Sprintf("%s not found", key)
}
trimKeyValue := strings.TrimSpace(odoConfigKeyValue)
if strings.Compare(key, trimKeyValue) != 0 {
return strings.TrimSpace(strings.SplitN(trimKeyValue, " ", 2)[1])
}
return ""
}
// DetermineRouteURL takes context path as argument and returns the http URL
// where the current component exposes it's service this URL can
// then be used in order to interact with the deployed service running in Openshift
func DetermineRouteURL(context string) string {
return routeURL(context)
}
func routeURL(context string) string {
var stdOut string
if context != "" {
stdOut = CmdShouldPass("odo", "url", "list", "--context", context)
} else {
stdOut = CmdShouldPass("odo", "url", "list")
}
reURL := regexp.MustCompile(`\s+http://.\S+`)
odoURL := reURL.FindString(stdOut)
return strings.TrimSpace(odoURL)
}
// CreateRandProject create new project with random name (10 letters)
// without writing to the config file (without switching project)
func CreateRandProject() string {
projectName := RandString(10)
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
session := CmdShouldPass("odo", "project", "create", projectName, "-w", "-v4")
Expect(session).To(ContainSubstring("New project created"))
Expect(session).To(ContainSubstring(projectName))
return projectName
}
// DeleteProject deletes a specified project
func DeleteProject(projectName string) {
fmt.Fprintf(GinkgoWriter, "Deleting project: %s\n", projectName)
session := CmdShouldPass("odo", "project", "delete", projectName, "-f")
Expect(session).To(ContainSubstring("Deleted project : " + projectName))
}