forked from okteto/deploy-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
executable file
·108 lines (91 loc) · 2.47 KB
/
message.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
type contexts struct {
Current string `json:"current-context"`
Contexts map[string]context `json:"contexts"`
}
type context struct {
Name string `json:"name"`
}
//Endpoint represents an Okteto statefulset
type Endpoint struct {
URL string `json:"url"`
Divert bool `json:"divert"`
Private bool `json:"private"`
}
func main() {
previewName := os.Args[1]
previewCommandExitCode := os.Args[2]
oktetoURL, err := getOktetoURL()
if err != nil {
return
}
previewURL := fmt.Sprintf("%s/#/previews/%s", oktetoURL, previewName)
var firstLine string
if previewCommandExitCode == "0" {
firstLine = fmt.Sprintf("Your preview environment [%s](%s) has been deployed.", previewName, previewURL)
} else {
firstLine = fmt.Sprintf("Your preview environment [%s](%s) has been deployed with errors.", previewName, previewURL)
}
fmt.Println(firstLine)
endpoints, err := getEndpoints(previewName)
if err != nil {
return
}
if len(endpoints) == 1 {
fmt.Printf("\n Preview environment endpoint is available [here](%s)", endpoints[0])
} else if len(endpoints) > 1 {
endpoints = translateEndpoints(endpoints)
fmt.Printf("\n Preview environment endpoints are available at:")
for _, endpoint := range endpoints {
fmt.Printf("\n * %s", endpoint)
}
}
}
func getOktetoURL() (string, error) {
contextsPath := filepath.Join(os.Getenv("HOME"), ".okteto", "context", "config.json")
b, err := ioutil.ReadFile(contextsPath)
if err != nil {
return "", err
}
contexts := &contexts{}
if err := json.Unmarshal(b, contexts); err != nil {
return "", err
}
if val, ok := contexts.Contexts[contexts.Current]; ok {
return val.Name, nil
}
return "", fmt.Errorf("context %s is missing", contexts.Current)
}
func getEndpoints(name string) ([]string, error) {
cmd := exec.Command("okteto", "preview", "endpoints", name, "-o", "json")
cmd.Env = os.Environ()
o, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
var endpoints []Endpoint
err = json.Unmarshal(o, &endpoints)
if err != nil {
return nil, err
}
endpointURLs := make([]string, 0)
for _, e := range endpoints {
endpointURLs = append(endpointURLs, e.URL)
}
return endpointURLs, nil
}
func translateEndpoints(endpoints []string) []string {
result := make([]string, 0)
for _, endpoint := range endpoints {
result = append(result, fmt.Sprintf("[%s](%s)", endpoint, endpoint))
}
return result
}