-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy patheula.go
More file actions
270 lines (216 loc) · 6.63 KB
/
eula.go
File metadata and controls
270 lines (216 loc) · 6.63 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */
package ui
import (
"fmt"
"strings"
"github.com/jroimartin/gocui"
errs "github.com/open-cmsis-pack/cpackget/cmd/errors"
"github.com/open-cmsis-pack/cpackget/cmd/utils"
log "github.com/sirupsen/logrus"
)
var Agreed = true
var Disagreed = false
var LicenseAgreed *bool
var Extract = false
// LicenseWindowType defines the struct to handle UI
type LicenseWindowType struct {
// LayoutManager is a function that defines the elements in the ui
LayoutManager func(g *gocui.Gui) error
Scroll func(v *gocui.View, dy int) error
ScrollUp func(g *gocui.Gui, v *gocui.View) error
ScrollDown func(g *gocui.Gui, v *gocui.View) error
Agree func(g *gocui.Gui, v *gocui.View) error
Disagree func(g *gocui.Gui, v *gocui.View) error
Extract func(g *gocui.Gui, v *gocui.View) error
Gui *gocui.Gui
}
// DisplayAndWaitForEULA prints out the license to the user through a UI
// and waits for user confirmation.
func DisplayAndWaitForEULA(licenseTitle, licenseContents string) (bool, error) {
if Extract {
return false, errs.ErrExtractEula
}
promptText := "License Agreement: [A]ccept [D]ecline [E]xtract"
if !utils.IsTerminalInteractive() {
// Show input on non-interactive terminals
promptText = "License Agreement: [A]ccept [D]ecline [E]xtract: "
fmt.Printf("*** %v ***", licenseTitle)
fmt.Println()
fmt.Println(licenseContents)
fmt.Println()
fmt.Print(promptText)
if LicenseAgreed != nil {
return *LicenseAgreed, nil
}
if Extract {
return false, errs.ErrExtractEula
}
var input string
_, _ = fmt.Scanln(&input)
if input == "a" || input == "A" {
return true, nil
}
if input == "e" || input == "E" {
return false, errs.ErrExtractEula
}
return false, nil
}
licenseWindow := NewLicenseWindow(licenseTitle, licenseContents, promptText)
if err := licenseWindow.Setup(); err != nil {
return false, err
}
defer licenseWindow.Gui.Close()
return licenseWindow.PromptUser()
}
func NewLicenseWindow(licenseTitle, licenseContents, promptText string) *LicenseWindowType {
licenseWindow := &LicenseWindowType{}
licenseHeight := utils.CountLines(licenseContents)
licenseMarginBottom := 10
// The LayoutManager is called on events, like key press or window resize
// and it is going to look more or less like
//
// +-- license file name -------+
// | License contents line 1 |
// | License contents line 2 |
// | License contents line N |
// +----------------------------+
// +----------------------------+
// | promptText |
// +----------------------------+
licenseWindow.LayoutManager = func(g *gocui.Gui) error {
terminalWidth, terminalHeight := g.Size()
marginSize := 1
promptWindowHeight := 3
// License window dimensions
licenseWindowBeginX := marginSize
licenseWindowBeginY := marginSize
licenseWindowEndX := terminalWidth - marginSize
licenseWindowEndY := terminalHeight - marginSize - promptWindowHeight
if v, err := g.SetView("license", licenseWindowBeginX, licenseWindowBeginY, licenseWindowEndX, licenseWindowEndY); err != nil {
if err != gocui.ErrUnknownView {
log.Error("Cannot modify license window: ", err)
return err
}
v.Wrap = true
v.Title = licenseTitle
fmt.Fprint(v, strings.ReplaceAll(licenseContents, "\r", ""))
}
// Prompt window dimensions
promptWindowBeginX := licenseWindowBeginX
promptWindowBeginY := licenseWindowEndY + marginSize
promptWindowEndX := licenseWindowEndX
promptWindowEndY := terminalHeight - marginSize
if v, err := g.SetView("prompt", promptWindowBeginX, promptWindowBeginY, promptWindowEndX, promptWindowEndY); err != nil {
if err != gocui.ErrUnknownView {
log.Error("Cannot modify prompt window: ", err)
return err
}
fmt.Fprint(v, promptText)
}
_, err := g.SetCurrentView("license")
if err != nil && err != gocui.ErrUnknownView {
return err
}
if LicenseAgreed != nil {
return gocui.ErrQuit
}
if Extract {
return errs.ErrExtractEula
}
return nil
}
licenseWindow.ScrollUp = func(g *gocui.Gui, v *gocui.View) error {
return licenseWindow.Scroll(v, -1)
}
licenseWindow.ScrollDown = func(g *gocui.Gui, v *gocui.View) error {
return licenseWindow.Scroll(v, 1)
}
licenseWindow.Scroll = func(v *gocui.View, dy int) error {
if v != nil {
ox, oy := v.Origin()
_, terminalHeight := licenseWindow.Gui.Size()
y := oy + dy
if y < 0 || y+terminalHeight-licenseMarginBottom >= licenseHeight {
return nil
}
if err := v.SetOrigin(ox, oy+dy); err != nil {
log.Errorf("Cannot scroll to %v, %v: %v", ox, oy+dy, err.Error())
return err
}
}
return nil
}
licenseWindow.Agree = func(g *gocui.Gui, v *gocui.View) error {
LicenseAgreed = &Agreed
return gocui.ErrQuit
}
licenseWindow.Disagree = func(g *gocui.Gui, v *gocui.View) error {
LicenseAgreed = &Disagreed
return gocui.ErrQuit
}
licenseWindow.Extract = func(g *gocui.Gui, v *gocui.View) error {
Extract = true
return gocui.ErrQuit
}
return licenseWindow
}
func (l *LicenseWindowType) Setup() error {
log.Debug("Setting up UI to display license")
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Error("Cannot initialize UI: ", err)
return err
}
g.SetManagerFunc(l.LayoutManager)
bindings := []struct {
key interface{}
funcPointer func(g *gocui.Gui, v *gocui.View) error
}{
// Agree with 'a' or 'A'
{'a', l.Agree},
{'A', l.Agree},
// Disagree with 'd' or 'D'
{'d', l.Disagree},
{'D', l.Disagree},
// Extract with 'e' or 'E'
{'e', l.Extract},
{'E', l.Extract},
// Scroll up with mouse/page/arrow up
{gocui.MouseWheelUp, l.ScrollUp},
{gocui.KeyArrowUp, l.ScrollUp},
{gocui.KeyPgup, l.ScrollUp},
// Scroll down with mouse/page/arrow down, enter or space
{gocui.MouseWheelDown, l.ScrollDown},
{gocui.KeyPgdn, l.ScrollDown},
{gocui.KeyArrowDown, l.ScrollDown},
{gocui.KeyEnter, l.ScrollDown},
{gocui.KeySpace, l.ScrollDown},
// Exit with Ctrl+C
{gocui.KeyCtrlC, quit},
}
for _, binding := range bindings {
_ = g.SetKeybinding("license", binding.key, gocui.ModNone, binding.funcPointer)
}
l.Gui = g
return nil
}
func (l *LicenseWindowType) PromptUser() (bool, error) {
log.Debug("Prompting user for license agreement")
err := l.Gui.MainLoop()
if err != nil && err != gocui.ErrQuit && err != errs.ErrExtractEula {
log.Error("Cannot obtain user response: ", err)
return false, err
}
if LicenseAgreed != nil {
return *LicenseAgreed, nil
}
if Extract {
return false, errs.ErrExtractEula
}
return false, nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
log.Warn("Aborting license agreement")
return gocui.ErrQuit
}