/
result.go
140 lines (115 loc) · 3.7 KB
/
result.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
package edgectl
import (
"bytes"
"text/template"
"github.com/gookit/color"
"github.com/pkg/browser"
"github.com/datawire/ambassador/internal/pkg/edgectl/client"
)
// Result represents the result of an installation attempt
type Result struct {
Report string // Action to report to Metriton
ShortMessage string // Short human-readable error message
Message string // Message to show to the user
TryAgain bool // Whether to show the "try again" message (TODO: Is this necessary?)
URL string // Docs URL to show and open
Err error // Error condition (nil -> successful installation)
}
// UnhandledErrResult returns a minimal Result that passes along an error but
// otherwise does not do anything. It is intended for transitional use, until
// *every* error is handled explicitly to yield a meaningful Result.
func UnhandledErrResult(err error) Result {
return Result{
Err: err,
}
}
// AdditionalDatum represents a key-value pair that may be used in template
// expansion
type AdditionalDatum struct {
key string
value interface{}
}
// ShowTemplated displays a string to the user (using ShowWrapped) after
// rendering the supplied text as a template using values from the installer
// object and the additional parameters. It also processes color tags.
// TODO: Fix color tag processing so it is effective on Windows.
func (i *Installer) ShowTemplated(text string, more ...AdditionalDatum) {
t := template.New("installer")
template.Must(t.Parse(text))
// Note: May fail before we have k8sVersion, so handle this special case.
var k8sClientVersion = "unknown"
var k8sServerVersion = "unknown"
// Assign if we have k8sVersion available.
if i.k8sVersion.Server.GitVersion != "" {
k8sClientVersion = i.k8sVersion.Client.GitVersion
k8sServerVersion = i.k8sVersion.Server.GitVersion
}
data := map[string]interface{}{
"version": i.version,
"address": i.address,
"hostname": i.hostname,
"clusterID": i.clusterID,
"kubectl": k8sClientVersion,
"k8s": k8sServerVersion,
}
for _, ad := range more {
data[ad.key] = ad.value
}
templateBuffer := &bytes.Buffer{}
if err := t.Execute(templateBuffer, data); err != nil {
//i.log.Printf("WARNING: failed to render template: %+v", err)
//return text
panic(err) // An error here indicates a bug in our code
}
i.ShowWrapped(color.Render(templateBuffer.String()))
}
func (i *Installer) ShowResult(r Result) {
templateData := []AdditionalDatum{
{key: "Report", value: r.Report},
{key: "Message", value: r.Message},
{key: "TryAgain", value: r.TryAgain},
{key: "URL", value: r.URL},
{key: "Err", value: r.Err},
}
if r.Err != nil {
// Failure
i.log.Printf("Result: Installation failed")
i.log.Printf(" Error: %+v", r.Err)
if r.Report != "" {
i.Report(r.Report, client.ScoutMeta{Key: "err", Value: r.Err.Error()})
}
if r.ShortMessage != "" {
i.show.Println()
i.show.Println(r.ShortMessage)
}
if r.Message != "" {
i.show.Println()
i.show.Println("AES Installation UNSUCCESSFUL")
i.show.Println("========================================================================")
i.show.Println()
i.ShowTemplated(r.Message, templateData...)
if r.URL != "" {
i.show.Println()
if err := browser.OpenURL(r.URL); err != nil {
i.log.Printf("Failed to open browser: %+v", err)
}
}
}
} else {
// Success
if r.Report != "" {
i.Report(r.Report)
}
if r.Message != "" {
i.show.Println()
i.ShowTemplated(r.Message, templateData...)
if r.URL != "" {
i.show.Println()
if err := browser.OpenURL(r.URL); err != nil {
i.log.Printf("Failed to open browser: %+v", err)
}
}
// Assume there's no need to show the "try again" message.
}
}
}