-
Notifications
You must be signed in to change notification settings - Fork 21
/
config.go
249 lines (206 loc) · 7.39 KB
/
config.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
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
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package config provides functions for the tanzu cli configuration
package config
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/pkg/errors"
"golang.org/x/mod/semver"
"github.com/vmware-tanzu/tanzu-plugin-runtime/component"
configlib "github.com/vmware-tanzu/tanzu-plugin-runtime/config"
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
"github.com/vmware-tanzu/tanzu-cli/pkg/interfaces"
)
var (
configClient interfaces.ConfigClientWrapper
ceipPromptMsg = `VMware's Customer Experience Improvement Program ("CEIP") provides VMware with
information that enables VMware to improve its products and services and fix
problems. By choosing to participate in CEIP, you agree that VMware may collect
technical information about your use of VMware products and services on a
regular basis. This information does not personally identify you.
For more details about the program, please see https://www.vmware.com/trustvmware/ceip.html.
Note: this prompt can be avoided by setting the environment variable
TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER to "yes" or "no".
Do you agree to participate in the Customer Experience Improvement Program?
`
eulaPromptMsg = `You must agree to the Broadcom Foundation Agreement in order to download,
install, or use software from this registry via Tanzu CLI. Acceptance of the
Broadcom Foundation Agreement covers all software installed via the Tanzu CLI
during any Session. "Session" means the period from acceptance until any of the
following occurs: (1) a change to Broadcom Foundation Agreement, (2) a new
major release of the Tanzu CLI is installed, (3) software is accessed in a
separate software distribution registry, or (4) re-acceptance of the Foundation
Agreement is prompted by Broadcom.
To view the Broadcom Foundation Agreement, please see
https://www.broadcom.com/company/legal/licensing.
If you agree, the essential plugins (required by the Tanzu CLI) will be
automatically installed.
Note: this prompt can be avoided by running "tanzu config eula accept".
Do you agree to the Broadcom Foundation Agreement?
`
// CurrentEULAVersion (vMajor.Minor.Patch) is the version of the EULA to
// display if user has not agreed to a compatible EULA.
// A user is considered to have accepted the EULA if one of the recorded
// accepted semver versions matches in the major.minor of this value.
// If this value is empty, however, any previously recorded acceptance
// is considered valid.
//
// Update the EULA version to v1.1.0 to re-prompt users because of the
// default plugin discovery source change
CurrentEULAVersion = "v1.1.0"
)
func init() {
configClient = interfaces.NewConfigClient()
}
// ConfigureEnvVariables reads and configures provided environment variables
// as part of tanzu configuration file
func ConfigureEnvVariables() {
envMap := configClient.GetEnvConfigurations()
if envMap == nil {
return
}
for variable, value := range envMap {
// If environment variable is not already set
// set the environment variable
if _, isSet := os.LookupEnv(variable); !isSet {
os.Setenv(variable, value)
}
}
}
// ConfigureCEIPOptIn checks and configures the User CEIP Opt-in choice in the tanzu configuration file
func ConfigureCEIPOptIn() error {
ceipOptInConfigVal, _ := configlib.GetCEIPOptIn()
// If CEIP Opt-In config parameter is already set, do nothing
if ceipOptInConfigVal != "" {
return nil
}
ceipOptInUserVal, err := getCEIPUserOptIn()
if err != nil {
return errors.Wrapf(err, "failed to get CEIP Opt-In status")
}
err = configlib.SetCEIPOptIn(strconv.FormatBool(ceipOptInUserVal))
if err != nil {
return errors.Wrapf(err, "failed to update the CEIP Opt-In status")
}
return nil
}
func getCEIPUserOptIn() (bool, error) {
var ceipOptIn string
optInPromptChoiceEnvVal := os.Getenv(constants.CEIPOptInUserPromptAnswer)
if optInPromptChoiceEnvVal != "" {
return strings.EqualFold(optInPromptChoiceEnvVal, "Yes"), nil
}
// prompt user and record their choice
err := component.Prompt(
&component.PromptConfig{
Message: ceipPromptMsg,
Options: []string{"Yes", "No"},
Default: "Yes",
},
&ceipOptIn,
)
if err != nil {
return false, err
}
// Put a delimiter after the prompt as it can be followed by
// standard CLI output
fmt.Println("")
fmt.Println("==")
return strings.EqualFold(ceipOptIn, "Yes"), nil
}
func IsCompatibleEULAAccepted(acceptedVersions []string) bool {
// As a special case, if there is no expected or valid CurrentEULAVersion to
// match, no incompatibility is flagged.
if CurrentEULAVersion == "" || !semver.IsValid(CurrentEULAVersion) {
return true
}
if len(acceptedVersions) == 0 {
return false
}
expectedEULAVersionMajorMinor := semver.MajorMinor(CurrentEULAVersion)
for _, v := range acceptedVersions {
if semver.IsValid(v) && semver.MajorMinor(v) == expectedEULAVersionMajorMinor {
return true
}
}
return false
}
// UpdateEULAAcceptance updates the user's EULA accept status, and records the current
// EULA version as accepted, if necessary
func UpdateEULAAcceptance(status configlib.EULAStatus) error {
err := configlib.SetEULAStatus(status)
if err != nil {
return errors.Wrapf(err, "failed update EULA status")
}
if status == configlib.EULAStatusAccepted && CurrentEULAVersion != "" {
acceptedVersions, err := configlib.GetEULAAcceptedVersions()
if err != nil {
return errors.Wrapf(err, "unable to get accepted eula versions")
}
// check if version already registered as accepted
for _, v := range acceptedVersions {
if v == CurrentEULAVersion {
return nil
}
}
acceptedVersions = append(acceptedVersions, CurrentEULAVersion)
err = configlib.SetEULAAcceptedVersions(acceptedVersions)
if err != nil {
return errors.Wrapf(err, "unable to update accepted eula version")
}
}
return nil
}
// ConfigureEULA checks and configures the user's EULA acceptance status
func ConfigureEULA(alwaysPrompt bool) error {
configVal, _ := configlib.GetEULAStatus()
acceptedVersions, err := configlib.GetEULAAcceptedVersions()
compatibleAcceptedEULAFound := false
if err == nil {
compatibleAcceptedEULAFound = IsCompatibleEULAAccepted(acceptedVersions)
}
// Unless forced to always prompt, it is a no-op if a compatible EULA is already accepted.
if !alwaysPrompt && configVal == configlib.EULAStatusAccepted && compatibleAcceptedEULAFound {
return nil
}
accepted, err := promptForEULA()
if err != nil {
return errors.Wrapf(err, "failed to get EULA status")
}
status := configlib.EULAStatusShown
if accepted {
err = configlib.SetEULAAcceptedVersions(acceptedVersions)
if err != nil {
return errors.Wrapf(err, "unable to update accepted eula version")
}
status = configlib.EULAStatusAccepted
}
return UpdateEULAAcceptance(status)
}
func promptForEULA() (bool, error) {
var eulaAccepted string
eulaPromptChoiceEnvVal := os.Getenv(constants.EULAPromptAnswer)
if eulaPromptChoiceEnvVal != "" {
return strings.EqualFold(eulaPromptChoiceEnvVal, "Yes"), nil
}
// prompt user and record their choice
err := component.Prompt(
&component.PromptConfig{
Message: eulaPromptMsg,
Options: []string{"Yes", "No"},
Default: "Yes",
},
&eulaAccepted,
)
if err != nil {
return false, errors.Wrapf(err, "prompt failed")
}
// Put a delimiter after the prompt as it can be followed by
// standard CLI output
fmt.Println("")
fmt.Println("==")
return strings.EqualFold(eulaAccepted, "Yes"), nil
}