Skip to content

Commit

Permalink
Templated output migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tstromberg committed Jul 17, 2019
1 parent 24ad1d8 commit 3d7c657
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 57 deletions.
5 changes: 3 additions & 2 deletions cmd/minikube/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/exit"
)

Expand Down Expand Up @@ -69,10 +70,10 @@ var completionCmd = &cobra.Command{
Long: longDescription,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
exit.Usage("Usage: minikube completion SHELL")
exit.UsageT("Usage: minikube completion SHELL")
}
if args[0] != "bash" && args[0] != "zsh" {
exit.Usage("Sorry, completion support is not yet implemented for %q", args[0])
exit.UsageT("Sorry, completion support is not yet implemented for {{.name}}", console.Arg{"name": args[0]})
} else if args[0] == "bash" {
err := GenerateBashCompletion(os.Stdout, cmd.Parent())
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/minikube/cmd/config/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ var addonsEnableCmd = &cobra.Command{
addon := args[0]
err := Set(addon, "true")
if err != nil {
console.Fatal("enable failed: %v", err)
} else {
console.Success("%s was successfully enabled", addon)
exit.WithError("enable failed", err)
}
console.SuccessT("{{.addonName}} was successfully enabled", console.Arg{"addonName": addon})
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func EnableOrDisableAddon(name string, val string) error {

cfg, err := config.Load()
if err != nil && !os.IsNotExist(err) {
exit.WithCode(exit.Data, "Unable to load config: %v", err)
exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", console.Arg{"error": err})
}

data := assets.GenerateTemplateData(cfg.KubernetesConfig)
Expand Down Expand Up @@ -158,7 +158,7 @@ func enableOrDisableAddonInternal(addon *assets.Addon, cmd command.Runner, data
var err error
// check addon status before enabling/disabling it
if err := isAddonAlreadySet(addon, enable); err != nil {
console.ErrStyle(console.Conflict, "%v", err)
console.ErrT(console.Conflict, "{{.error}}", console.Arg{"error": err})
os.Exit(0)
}

Expand Down
21 changes: 10 additions & 11 deletions cmd/minikube/cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var dashboardCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cc, err := pkg_config.Load()
if err != nil && !os.IsNotExist(err) {
console.ErrLn("Error loading profile config: %v", err)
exit.WithError("Error loading profile config", err)
}

api, err := machine.NewAPIClient()
Expand All @@ -76,8 +76,7 @@ var dashboardCmd = &cobra.Command{
if _, err = api.Load(pkg_config.GetMachineName()); err != nil {
switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
console.OutStyle(console.Meh, "%q cluster does not exist", pkg_config.GetMachineName())
os.Exit(exit.Unavailable)
exit.WithCodeT(exit.Unavailable, "{{.name}} cluster does not exist", console.Arg{"name": pkg_config.GetMachineName()})
default:
exit.WithError("Error getting cluster", err)
}
Expand All @@ -100,7 +99,7 @@ var dashboardCmd = &cobra.Command{
dashboardStatus, _ := dashboardAddon.IsEnabled()
if !dashboardStatus {
// Send status messages to stderr for folks re-using this output.
console.ErrStyle(console.Enabling, "Enabling dashboard ...")
console.ErrT(console.Enabling, "Enabling dashboard ...")
// Enable the dashboard add-on
err = configcmd.Set("dashboard", "true")
if err != nil {
Expand All @@ -110,29 +109,29 @@ var dashboardCmd = &cobra.Command{

ns := "kube-system"
svc := "kubernetes-dashboard"
console.ErrStyle(console.Verifying, "Verifying dashboard health ...")
console.ErrT(console.Verifying, "Verifying dashboard health ...")
if err = util.RetryAfter(180, func() error { return service.CheckService(ns, svc) }, 1*time.Second); err != nil {
exit.WithCode(exit.Unavailable, "%s:%s is not running: %v", ns, svc, err)
exit.WithCodeT(exit.Unavailable, "dashboard service is not running: {{.error}}", console.Arg{"error": err})
}

console.ErrStyle(console.Launch, "Launching proxy ...")
console.ErrT(console.Launch, "Launching proxy ...")
p, hostPort, err := kubectlProxy(kubectl)
if err != nil {
exit.WithError("kubectl proxy", err)
}
url := dashboardURL(hostPort, ns, svc)

console.ErrStyle(console.Verifying, "Verifying proxy health ...")
console.ErrT(console.Verifying, "Verifying proxy health ...")
if err = util.RetryAfter(60, func() error { return checkURL(url) }, 1*time.Second); err != nil {
exit.WithCode(exit.Unavailable, "%s is not responding properly: %v", url, err)
exit.WithCodeT(exit.Unavailable, "{{.url}} is not accessible: {{.error}}", console.Arg{"url": url, "error": err})
}

if dashboardURLMode {
console.OutLn(url)
} else {
console.ErrStyle(console.Celebrate, "Opening %s in your default browser...", url)
console.ErrT(console.Celebrate, "Opening %s in your default browser...", url)
if err = browser.OpenURL(url); err != nil {
console.Failure("failed to open browser: %v", err)
exit.WithCodeT(exit.Software, "failed to open browser: {{.error}}", console.Arg{"error": err})
}
}

Expand Down
40 changes: 19 additions & 21 deletions cmd/minikube/cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ var mountCmd = &cobra.Command{
mountString := args[0]
idx := strings.LastIndex(mountString, ":")
if idx == -1 { // no ":" was present
exit.Usage(`mount argument %q must be in form: <source directory>:<target directory>`, mountString)
exit.UsageT(`mount argument "{{.value}}" must be in form: <source directory>:<target directory>`, console.Arg{"value": amountString})
}
hostPath := mountString[:idx]
vmPath := mountString[idx+1:]
if _, err := os.Stat(hostPath); err != nil {
if os.IsNotExist(err) {
exit.WithCode(exit.NoInput, "Cannot find directory %s for mount", hostPath)
exit.WithCodeT(exit.NoInput, "Cannot find directory {{.path}} for mount", console.Arg{"path": hostPath})
} else {
exit.WithError("stat failed", err)
}
}
if len(vmPath) == 0 || !strings.HasPrefix(vmPath, "/") {
exit.Usage("Target directory %q must be an absolute path", vmPath)
exit.UsageT("Target directory {{.path}} must be an absolute path", console.Arg{"path": vmPath})
}
var debugVal int
if glog.V(1) {
Expand Down Expand Up @@ -142,29 +142,27 @@ var mountCmd = &cobra.Command{
cfg.Options[parts[0]] = parts[1]
}

console.OutStyle(console.Mounting, "Mounting host path %s into VM as %s ...", hostPath, vmPath)
console.OutStyle(console.MountOptions, "Mount options:")
console.OutStyle(console.Option, "Type: %s", cfg.Type)
console.OutStyle(console.Option, "UID: %s", cfg.UID)
console.OutStyle(console.Option, "GID: %s", cfg.GID)
console.OutStyle(console.Option, "Version: %s", cfg.Version)
console.OutStyle(console.Option, "MSize: %d", cfg.MSize)
console.OutStyle(console.Option, "Mode: %o (%s)", cfg.Mode, cfg.Mode)
console.OutStyle(console.Option, "Options: %s", cfg.Options)
console.OutT(console.Mounting, "Mounting host path {{.sourcePath}} into VM as {{.destinationPath}} ...", console.Arg{"sourcePath": hostPath, "destinationPath": vmPath})
console.OutT(console.Option, "Mount type: {{.name}}", console.Arg{"type": cfg.Type})
console.OutT(console.Option, "User ID: {{.userID}}", console.Arg{"userID", cfg.UID})
console.OutT(console.Option, "Group ID: {{.groupID}}", console.Arg{"groupID", cfg.GID})
console.OutT(console.Option, "Version: {{.version}}", console.Arg{"version", cfg.Version})
console.OutT(console.Option, "Message Size: {{.size}}", console.Arg{"size", cfg.MSize})
console.OutT(console.Option, "Permissions: {{.octalMode}} ({{.writtenMode}})", cfg.Mode, cfg.Mode)
console.OutT(console.Option, "Options: {{.options}}", cfg.Options)

// An escape valve to allow future hackers to try NFS, VirtFS, or other FS types.
if !supportedFilesystems[cfg.Type] {
console.OutLn("")
console.OutStyle(console.WarningType, "%s is not yet a supported filesystem. We will try anyways!", cfg.Type)
console.OutT(console.WarningType, "{{.type}} is not yet a supported filesystem. We will try anyways!", console.Arg{"type": cfg.Type})
}

var wg sync.WaitGroup
if cfg.Type == nineP {
wg.Add(1)
go func() {
console.OutStyle(console.Fileserver, "Userspace file server: ")
console.OutT(console.Fileserver, "Userspace file server: ")
ufs.StartServer(net.JoinHostPort(ip.String(), strconv.Itoa(port)), debugVal, hostPath)
console.OutStyle(console.Stopped, "Userspace file server is shutdown")
console.OutT(console.Stopped, "Userspace file server is shutdown")
wg.Done()
}()
}
Expand All @@ -180,22 +178,22 @@ var mountCmd = &cobra.Command{
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range c {
console.OutStyle(console.Unmount, "Unmounting %s ...", vmPath)
console.OutT(console.Unmount, "Unmounting {{.path}} ...", console.Arg{"path": vmPath})
err := cluster.Unmount(runner, vmPath)
if err != nil {
console.ErrStyle(console.FailureType, "Failed unmount: %v", err)
console.ErrT(console.FailureType, "Failed unmount: {{.error}}", console.Arg{"error": err})
}
exit.WithCode(exit.Interrupted, "Exiting due to %s signal", sig)
exit.WithCode(exit.Interrupted, "Received {{.name}} signal", console.Arg{"name": sig})
}
}()

err = cluster.Mount(runner, ip.String(), vmPath, cfg)
if err != nil {
exit.WithError("mount failed", err)
}
console.OutStyle(console.SuccessType, "Successfully mounted %s to %s", hostPath, vmPath)
console.OutT(console.SuccessType, "Successfully mounted {{.sourcePath}} to {{.destinationPath}}", console.Arg{"sourcePath": hostPath, "destinationPath": vmPath})
console.OutLn("")
console.OutStyle(console.Notice, "NOTE: This process must stay alive for the mount to be accessible ...")
console.OutT(console.Notice, "NOTE: This process must stay alive for the mount to be accessible ...")
wg.Wait()
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ var serviceListCmd = &cobra.Command{
defer api.Close()
serviceURLs, err := service.GetServiceURLs(api, serviceListNamespace, serviceURLTemplate)
if err != nil {
console.Fatal("Failed to get service URL: %v", err)
console.ErrStyle(console.Notice, "Check that minikube is running and that you have specified the correct namespace (-n flag) if required.")
console.FatalT("Failed to get service URL: {{.error}}", console.Arg{"error": err})
console.ErrT(console.Notice, "Check that minikube is running and that you have specified the correct namespace (-n flag) if required.")
os.Exit(exit.Unavailable)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (k *Bootstrapper) WaitCluster(k8s config.KubernetesConfig) error {
// by a CNI plugin which is usually started after minikube has been brought
// up. Otherwise, minikube won't start, as "k8s-app" pods are not ready.
componentsOnly := k8s.NetworkPlugin == "cni"
console.OutStyle(console.WaitingPods, "Verifying:")
console.OutT(console.WaitingPods, "Verifying:")
client, err := util.GetClient()
if err != nil {
return errors.Wrap(err, "k8s client")
Expand Down Expand Up @@ -483,7 +483,7 @@ func (k *Bootstrapper) UpdateCluster(cfg config.KubernetesConfig) error {
_, images := constants.GetKubeadmCachedImages(cfg.ImageRepository, cfg.KubernetesVersion)
if cfg.ShouldLoadCachedImages {
if err := machine.LoadImages(k.c, images, constants.ImageCacheDir); err != nil {
console.Failure("Unable to load cached images: %v", err)
console.FailureT("Unable to load cached images: {{.error}}", console.Arg{"error": err})
}
}
r, err := cruntime.New(cruntime.Config{Type: cfg.ContainerRuntime, Socket: cfg.CRISocket})
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/console/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var styles = map[StyleEnum]style{
Issues: {Prefix: "⁉️ "},
Issue: {Prefix: " ▪ ", LowPrefix: lowIndent}, // Indented bullet
Check: {Prefix: "✔️ "},
Celebration: {Prefix: "🎉 "},

// Specialized purpose styles
ISODownload: {Prefix: "💿 "},
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/console/style_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
Happy StyleEnum = iota
SuccessType
FailureType
Celebration
Conflict
FatalType
Notice
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/machine/cache_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func CacheBinary(binary, version, osName, archName string) (string, error) {
options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version, osName, archName)
options.ChecksumHash = crypto.SHA1

console.OutStyle(console.FileDownload, "Downloading %s %s", binary, version)
console.OutT(console.FileDownload, "Downloading {{.name}} {{.version}}", console.Arg{"name": binary, "version": version})
if err := download.ToFile(url, targetFilepath, options); err != nil {
return "", errors.Wrapf(err, "Error downloading %s %s", binary, version)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/minikube/machine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/docker/machine/libmachine/version"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/registry"
Expand Down Expand Up @@ -269,7 +270,7 @@ func registerDriver(driverName string) {
def, err := registry.Driver(driverName)
if err != nil {
if err == registry.ErrDriverNotFound {
exit.Usage("unsupported driver: %s", driverName)
exit.UsageT("unsupported driver: {{.name}}", console.Arg{"name": driverName})
}
exit.WithError("error getting driver", err)
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/minikube/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ func MaybePrintUpdateText(url string, lastUpdatePath string) {
if err := writeTimeToFile(lastUpdateCheckFilePath, time.Now().UTC()); err != nil {
glog.Errorf("write time failed: %v", err)
}
console.ErrStyle(console.WarningType, `There is a newer version of minikube available (%s%s). Download it here:
%s%s
To disable this notification, run the following:
minikube config set WantUpdateNotification false
`,
version.VersionPrefix, latestVersion, updateLinkPrefix, latestVersion)
url := fmt.Sprintf("%s/%s", updateLinkPrefix, latestVersion)
console.ErrT(console.WarningType, `minikube {{.version}} is available! Download it: {{.url}}`, console.Arg{"version": latestVersion, "url": url})
console.OutT(console.Tip, "To disable this notice, run: 'minikube config set WantUpdateNotification false'")
}
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/minikube/problem/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package problem

import (
"fmt"
"regexp"

"k8s.io/minikube/pkg/minikube/console"
Expand Down Expand Up @@ -52,21 +53,21 @@ type match struct {

// Display problem metadata to the console
func (p *Problem) Display() {
console.ErrStyle(console.FailureType, "Error: [%s] %v", p.ID, p.Err)
console.ErrStyle(console.Tip, "Advice: %s", translate.T(p.Advice))
console.ErrT(console.FailureType, "Error: [{{.id}}] {{.error}}", console.Arg{"id": p.ID, "error": p.Err})
console.ErrT(console.Tip, "Suggestion: {{.advice}}", console.Arg{"advice": translate.T(p.Advice)})
if p.URL != "" {
console.ErrStyle(console.Documentation, "Documentation: %s", p.URL)
console.ErrT(console.Documentation, "Documentation: {{.url}}", p.URL)
}
if len(p.Issues) == 0 {
return
}
console.ErrStyle(console.Issues, "Related issues:")
console.ErrT(console.Issues, "Related issues:")
issues := p.Issues
if len(issues) > 3 {
issues = issues[0:3]
}
for _, i := range issues {
console.ErrStyle(console.Issue, "%s/%d", issueBase, i)
console.ErrT(console.Issue, "{{.url}}", console.Arg{"url": fmt.Sprintf("%s/%s", issueBase, i)})
}
}

Expand Down

0 comments on commit 3d7c657

Please sign in to comment.