فهرست منبع

Shorten printed Windows paths on docker help cmd

This makes use of `%USERPROFILE%` as a substitute for
`~` on Windows and prints shorter strings for default
cert paths etc.

Also removes string escaping/quotes around default
path values printed in `docker help` command as they
are not really necessary and adds double backslashes
(\\) on windows.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
Ahmet Alp Balkan 10 سال پیش
والد
کامیت
1c9b37cb96
2فایلهای تغییر یافته به همراه11 افزوده شده و 24 حذف شده
  1. 7 14
      integration-cli/docker_cli_help_test.go
  2. 4 10
      pkg/mflag/flag.go

+ 7 - 14
integration-cli/docker_cli_help_test.go

@@ -1,23 +1,19 @@
 package main
 package main
 
 
 import (
 import (
-	"os"
 	"os/exec"
 	"os/exec"
-	"runtime"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"unicode"
 	"unicode"
+
+	"github.com/docker/docker/pkg/homedir"
 )
 )
 
 
 func TestMainHelpWidth(t *testing.T) {
 func TestMainHelpWidth(t *testing.T) {
 	// Make sure main help text fits within 80 chars and that
 	// Make sure main help text fits within 80 chars and that
 	// on non-windows system we use ~ when possible (to shorten things)
 	// on non-windows system we use ~ when possible (to shorten things)
 
 
-	var home string
-	if runtime.GOOS != "windows" {
-		home = os.Getenv("HOME")
-	}
-
+	home := homedir.Get()
 	helpCmd := exec.Command(dockerBinary, "help")
 	helpCmd := exec.Command(dockerBinary, "help")
 	out, ec, err := runCommandWithOutput(helpCmd)
 	out, ec, err := runCommandWithOutput(helpCmd)
 	if err != nil || ec != 0 {
 	if err != nil || ec != 0 {
@@ -27,9 +23,10 @@ func TestMainHelpWidth(t *testing.T) {
 	for _, line := range lines {
 	for _, line := range lines {
 		if len(line) > 80 {
 		if len(line) > 80 {
 			t.Fatalf("Line is too long(%d chars):\n%s", len(line), line)
 			t.Fatalf("Line is too long(%d chars):\n%s", len(line), line)
+
 		}
 		}
 		if home != "" && strings.Contains(line, home) {
 		if home != "" && strings.Contains(line, home) {
-			t.Fatalf("Line should use ~ instead of %q:\n%s", home, line)
+			t.Fatalf("Line should use '%q' instead of %q:\n%s", homedir.GetShortcutString(), home, line)
 		}
 		}
 	}
 	}
 	logDone("help - verify main width")
 	logDone("help - verify main width")
@@ -39,11 +36,7 @@ func TestCmdHelpWidth(t *testing.T) {
 	// Make sure main help text fits within 80 chars and that
 	// Make sure main help text fits within 80 chars and that
 	// on non-windows system we use ~ when possible (to shorten things)
 	// on non-windows system we use ~ when possible (to shorten things)
 
 
-	var home string
-	if runtime.GOOS != "windows" {
-		home = os.Getenv("HOME")
-	}
-
+	home := homedir.Get()
 	// Pull the list of commands from the "Commands:" section of docker help
 	// Pull the list of commands from the "Commands:" section of docker help
 	helpCmd := exec.Command(dockerBinary, "help")
 	helpCmd := exec.Command(dockerBinary, "help")
 	out, ec, err := runCommandWithOutput(helpCmd)
 	out, ec, err := runCommandWithOutput(helpCmd)
@@ -82,7 +75,7 @@ func TestCmdHelpWidth(t *testing.T) {
 				t.Fatalf("Help for %q is too long(%d chars):\n%s", command, len(line), line)
 				t.Fatalf("Help for %q is too long(%d chars):\n%s", command, len(line), line)
 			}
 			}
 			if home != "" && strings.Contains(line, home) {
 			if home != "" && strings.Contains(line, home) {
-				t.Fatalf("Help for %q should use ~ instead of %q on:\n%s", command, home, line)
+				t.Fatalf("Help for %q should use home shortcut instead of %q on:\n%s", command, home, line)
 			}
 			}
 		}
 		}
 	}
 	}

+ 4 - 10
pkg/mflag/flag.go

@@ -86,12 +86,13 @@ import (
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"os"
 	"os"
-	"runtime"
 	"sort"
 	"sort"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"text/tabwriter"
 	"text/tabwriter"
 	"time"
 	"time"
+
+	"github.com/docker/docker/pkg/homedir"
 )
 )
 
 
 // ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
 // ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
@@ -504,16 +505,9 @@ func Set(name, value string) error {
 // otherwise, the default values of all defined flags in the set.
 // otherwise, the default values of all defined flags in the set.
 func (f *FlagSet) PrintDefaults() {
 func (f *FlagSet) PrintDefaults() {
 	writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0)
 	writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0)
-	var home string
-	if runtime.GOOS != "windows" {
-		home = os.Getenv("HOME")
-	}
+	home := homedir.Get()
 	f.VisitAll(func(flag *Flag) {
 	f.VisitAll(func(flag *Flag) {
 		format := "  -%s=%s"
 		format := "  -%s=%s"
-		if _, ok := flag.Value.(*stringValue); ok {
-			// put quotes on the value
-			format = "  -%s=%q"
-		}
 		names := []string{}
 		names := []string{}
 		for _, name := range flag.Names {
 		for _, name := range flag.Names {
 			if name[0] != '#' {
 			if name[0] != '#' {
@@ -524,7 +518,7 @@ func (f *FlagSet) PrintDefaults() {
 			val := flag.DefValue
 			val := flag.DefValue
 
 
 			if home != "" && strings.HasPrefix(val, home) {
 			if home != "" && strings.HasPrefix(val, home) {
-				val = "~" + val[len(home):]
+				val = homedir.GetShortcutString() + val[len(home):]
 			}
 			}
 
 
 			fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)
 			fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)