Skip to content

Commit b40011b

Browse files
Add T versions of the console convenience functions (#4796)
* Add T versions of the console convenience functions * more changes * Make OutT and ErrT more convenient and add WithCodeT: * add UsageT * make extract * translate messages in displayError
1 parent 24ad1d8 commit b40011b

File tree

8 files changed

+65
-18
lines changed

8 files changed

+65
-18
lines changed

cmd/minikube/cmd/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func runDelete(cmd *cobra.Command, args []string) {
6969
if err = cluster.DeleteHost(api); err != nil {
7070
switch err := errors.Cause(err).(type) {
7171
case mcnerror.ErrHostDoesNotExist:
72-
console.OutStyle(console.Meh, "%q cluster does not exist", profile)
72+
console.OutT(console.Meh, `"{{.name}}" cluster does not exist`, console.Arg{"name": profile})
7373
default:
7474
exit.WithError("Failed to delete cluster", err)
7575
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
77
github.com/Parallels/docker-machine-parallels v1.3.0
88
github.com/Sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2 // indirect
9+
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
910
github.com/blang/semver v3.5.0+incompatible
1011
github.com/c4milo/gotoolkit v0.0.0-20170318115440-bcc06269efa9 // indirect
1112
github.com/cloudfoundry-attic/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
@@ -18,6 +19,7 @@ require (
1819
github.com/elazarl/goproxy/ext v0.0.0-20190421051319-9d40249d3c2f // indirect
1920
github.com/fatih/color v1.7.0 // indirect
2021
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680 // indirect
22+
github.com/go-ole/go-ole v1.2.4 // indirect
2123
github.com/gogo/protobuf v0.0.0-20170330071051-c0656edd0d9e // indirect
2224
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
2325
github.com/golang/glog v0.0.0-20141105023935-44145f04b68c

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/Parallels/docker-machine-parallels v1.3.0 h1:RG1fyf3v1GwXMCeHRiZkB4tL
55
github.com/Parallels/docker-machine-parallels v1.3.0/go.mod h1:HCOMm3Hulq/xuEVQMyZOuQlA+dSZpFY5kdCTZWjMVis=
66
github.com/Sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2 h1:k1A7eIeUk6rnX2yuagwljW/pDezkK8oSpvPumT9zdZY=
77
github.com/Sirupsen/logrus v0.0.0-20170822132746-89742aefa4b2/go.mod h1:rmk17hk6i8ZSAJkSDa7nOxamrG+SP4P0mm+DAvExv4U=
8+
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
9+
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
810
github.com/blang/semver v3.5.0+incompatible h1:CGxCgetQ64DKk7rdZ++Vfnb1+ogGNnB17OJKJXD2Cfs=
911
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
1012
github.com/c4milo/gotoolkit v0.0.0-20170318115440-bcc06269efa9 h1:+ziP/wVJWuAORkjv7386TRidVKY57X0bXBZFMeFlW+U=
@@ -36,6 +38,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
3638
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3739
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680 h1:ZktWZesgun21uEDrwW7iEV1zPCGQldM2atlJZ3TdvVM=
3840
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
41+
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
42+
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
3943
github.com/gogo/protobuf v0.0.0-20170330071051-c0656edd0d9e h1:ago6fNuQ6IhszPsXkeU7qRCyfsIX7L67WDybsAPkLl8=
4044
github.com/gogo/protobuf v0.0.0-20170330071051-c0656edd0d9e/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
4145
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4=

pkg/minikube/console/console.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func OutStyle(style StyleEnum, format string, a ...interface{}) {
6767
}
6868

6969
// OutT writes a stylized and templated message to stdout
70-
func OutT(style StyleEnum, format string, a map[string]interface{}) {
71-
outStyled := applyTemplateFormatting(style, useColor, format, a)
70+
func OutT(style StyleEnum, format string, a ...Arg) {
71+
outStyled := applyTemplateFormatting(style, useColor, format, a...)
7272
Out(outStyled)
7373
}
7474

@@ -96,8 +96,8 @@ func ErrStyle(style StyleEnum, format string, a ...interface{}) {
9696
}
9797

9898
// ErrT writes a stylized and templated error message to stderr
99-
func ErrT(style StyleEnum, format string, a map[string]interface{}) {
100-
errStyled := applyTemplateFormatting(style, useColor, format, a)
99+
func ErrT(style StyleEnum, format string, a ...Arg) {
100+
errStyled := applyTemplateFormatting(style, useColor, format, a...)
101101
Err(errStyled)
102102
}
103103

@@ -138,6 +138,26 @@ func Failure(format string, a ...interface{}) {
138138
ErrStyle(FailureType, format, a...)
139139
}
140140

141+
// SuccessT is a shortcut for writing a templated success message to stdout
142+
func SuccessT(format string, a ...Arg) {
143+
OutT(SuccessType, format, a...)
144+
}
145+
146+
// FatalT is a shortcut for writing a templated fatal message to stderr
147+
func FatalT(format string, a ...Arg) {
148+
ErrT(FatalType, format, a...)
149+
}
150+
151+
// WarningT is a shortcut for writing a templated warning message to stderr
152+
func WarningT(format string, a ...Arg) {
153+
ErrT(WarningType, format, a...)
154+
}
155+
156+
// FailureT is a shortcut for writing a templated failure message to stderr
157+
func FailureT(format string, a ...Arg) {
158+
ErrT(FailureType, format, a...)
159+
}
160+
141161
// SetOutFile configures which writer standard output goes to.
142162
func SetOutFile(w fdWriter) {
143163
glog.Infof("Setting OutFile to fd %d ...", w.Fd())

pkg/minikube/console/style.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func applyStyle(style StyleEnum, useColor bool, format string) string {
151151
return applyPrefix(s.Prefix, format)
152152
}
153153

154-
func applyTemplateFormatting(style StyleEnum, useColor bool, format string, a map[string]interface{}) string {
154+
func applyTemplateFormatting(style StyleEnum, useColor bool, format string, a ...Arg) string {
155155
format = applyStyle(style, useColor, format)
156156

157157
var buf bytes.Buffer
@@ -160,8 +160,10 @@ func applyTemplateFormatting(style StyleEnum, useColor bool, format string, a ma
160160
glog.Infof("Initializing template failed. Returning raw string.")
161161
return format
162162
}
163-
164-
err = t.Execute(&buf, a)
163+
if len(a) == 0 {
164+
a[0] = Arg{}
165+
}
166+
err = t.Execute(&buf, a[0])
165167
if err != nil {
166168
glog.Infof("Executing template failed. Returning raw string.")
167169
return format

pkg/minikube/exit/exit.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/golang/glog"
2626
"k8s.io/minikube/pkg/minikube/console"
2727
"k8s.io/minikube/pkg/minikube/problem"
28+
"k8s.io/minikube/pkg/minikube/translate"
2829
)
2930

3031
// Exit codes based on sysexits(3)
@@ -50,6 +51,12 @@ func Usage(format string, a ...interface{}) {
5051
os.Exit(BadUsage)
5152
}
5253

54+
// UsageT outputs a templated usage error and exits with error code 64
55+
func UsageT(format string, a ...console.Arg) {
56+
console.ErrT(console.Usage, format, a...)
57+
os.Exit(BadUsage)
58+
}
59+
5360
// WithCode outputs a fatal error message and exits with a supplied error code.
5461
func WithCode(code int, format string, a ...interface{}) {
5562
// use Warning because Error will display a duplicate message to stderr
@@ -58,6 +65,12 @@ func WithCode(code int, format string, a ...interface{}) {
5865
os.Exit(code)
5966
}
6067

68+
// WithCodeT outputs a templated fatal error message and exits with the supplied error code.
69+
func WithCodeT(code int, format string, a ...console.Arg) {
70+
console.FatalT(format, a...)
71+
os.Exit(code)
72+
}
73+
6174
// WithError outputs an error and exits.
6275
func WithError(msg string, err error) {
6376
p := problem.FromError(err, runtime.GOOS)
@@ -71,11 +84,11 @@ func WithError(msg string, err error) {
7184
// WithProblem outputs info related to a known problem and exits.
7285
func WithProblem(msg string, p *problem.Problem) {
7386
console.Err("\n")
74-
console.Fatal(msg)
87+
console.FatalT(msg)
7588
p.Display()
7689
console.Err("\n")
77-
console.ErrStyle(console.Sad, "If the above advice does not help, please let us know: ")
78-
console.ErrStyle(console.URL, "https://github.com/kubernetes/minikube/issues/new/choose")
90+
console.ErrT(console.Sad, "If the above advice does not help, please let us know: ")
91+
console.ErrT(console.URL, "https://github.com/kubernetes/minikube/issues/new/choose")
7992
os.Exit(Config)
8093
}
8194

@@ -84,12 +97,12 @@ func WithLogEntries(msg string, err error, entries map[string][]string) {
8497
displayError(msg, err)
8598

8699
for name, lines := range entries {
87-
console.OutStyle(console.FailureType, "Problems detected in %q:", name)
100+
console.OutT(console.FailureType, "Problems detected in {{.entry}}:", console.Arg{"entry": name})
88101
if len(lines) > MaxLogEntries {
89102
lines = lines[:MaxLogEntries]
90103
}
91104
for _, l := range lines {
92-
console.OutStyle(console.LogEntry, l)
105+
console.OutT(console.LogEntry, l)
93106
}
94107
}
95108
os.Exit(Software)
@@ -99,8 +112,8 @@ func displayError(msg string, err error) {
99112
// use Warning because Error will display a duplicate message to stderr
100113
glog.Warningf(fmt.Sprintf("%s: %v", msg, err))
101114
console.Err("\n")
102-
console.Fatal("%s: %v", msg, err)
115+
console.FatalT("{{.msg}}: {{.err}}", console.Arg{"msg": translate.T(msg), "err": err})
103116
console.Err("\n")
104-
console.ErrStyle(console.Sad, "Sorry that minikube crashed. If this was unexpected, we would love to hear from you:")
105-
console.ErrStyle(console.URL, "https://github.com/kubernetes/minikube/issues/new/choose")
117+
console.ErrT(console.Sad, "Sorry that minikube crashed. If this was unexpected, we would love to hear from you:")
118+
console.ErrT(console.URL, "https://github.com/kubernetes/minikube/issues/new/choose")
106119
}

translations/fr-FR.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"\"{{.name}}\" cluster does not exist": "",
23
"%q VM does not exist, nothing to stop": "",
34
"%q cluster does not exist": "",
45
"%q host does not exist, unable to show an IP": "",
@@ -148,6 +149,7 @@
148149
"Please upgrade the 'docker-machine-driver-kvm2'. %s": "",
149150
"Powering off %q via SSH ...": "",
150151
"Problems detected in %q:": "",
152+
"Problems detected in {{.entry}}:": "",
151153
"Pulling images ...": "Extrayant les images ... ",
152154
"Re-run 'minikube start' with --alsologtostderr -v=8 to see the VM driver error message": "",
153155
"Re-using the currently running %s VM for %q ...": "",
@@ -274,5 +276,6 @@
274276
"usage: minikube config unset PROPERTY_NAME": "",
275277
"usage: minikube delete": "",
276278
"usage: minikube profile [MINIKUBE_PROFILE_NAME]": "",
277-
"zsh completion failed": ""
279+
"zsh completion failed": "",
280+
"{{.msg}}: {{.err}}": ""
278281
}

translations/zh-CN.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"\"{{.name}}\" cluster does not exist": "",
23
"%q VM does not exist, nothing to stop": "",
34
"%q cluster does not exist": "",
45
"%q host does not exist, unable to show an IP": "",
@@ -148,6 +149,7 @@
148149
"Please upgrade the 'docker-machine-driver-kvm2'. %s": "",
149150
"Powering off %q via SSH ...": "",
150151
"Problems detected in %q:": "",
152+
"Problems detected in {{.entry}}:": "",
151153
"Pulling images ...": "拉取镜像 ...",
152154
"Re-run 'minikube start' with --alsologtostderr -v=8 to see the VM driver error message": "",
153155
"Re-using the currently running %s VM for %q ...": "",
@@ -274,5 +276,6 @@
274276
"usage: minikube config unset PROPERTY_NAME": "",
275277
"usage: minikube delete": "",
276278
"usage: minikube profile [MINIKUBE_PROFILE_NAME]": "",
277-
"zsh completion failed": ""
279+
"zsh completion failed": "",
280+
"{{.msg}}: {{.err}}": ""
278281
}

0 commit comments

Comments
 (0)