Browse code

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 authored on 2015/02/19 06:32:42
Showing 2 changed files
... ...
@@ -1,23 +1,19 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"os"
5 4
 	"os/exec"
6
-	"runtime"
7 5
 	"strings"
8 6
 	"testing"
9 7
 	"unicode"
8
+
9
+	"github.com/docker/docker/pkg/homedir"
10 10
 )
11 11
 
12 12
 func TestMainHelpWidth(t *testing.T) {
13 13
 	// Make sure main help text fits within 80 chars and that
14 14
 	// on non-windows system we use ~ when possible (to shorten things)
15 15
 
16
-	var home string
17
-	if runtime.GOOS != "windows" {
18
-		home = os.Getenv("HOME")
19
-	}
20
-
16
+	home := homedir.Get()
21 17
 	helpCmd := exec.Command(dockerBinary, "help")
22 18
 	out, ec, err := runCommandWithOutput(helpCmd)
23 19
 	if err != nil || ec != 0 {
... ...
@@ -27,9 +23,10 @@ func TestMainHelpWidth(t *testing.T) {
27 27
 	for _, line := range lines {
28 28
 		if len(line) > 80 {
29 29
 			t.Fatalf("Line is too long(%d chars):\n%s", len(line), line)
30
+
30 31
 		}
31 32
 		if home != "" && strings.Contains(line, home) {
32
-			t.Fatalf("Line should use ~ instead of %q:\n%s", home, line)
33
+			t.Fatalf("Line should use '%q' instead of %q:\n%s", homedir.GetShortcutString(), home, line)
33 34
 		}
34 35
 	}
35 36
 	logDone("help - verify main width")
... ...
@@ -39,11 +36,7 @@ func TestCmdHelpWidth(t *testing.T) {
39 39
 	// Make sure main help text fits within 80 chars and that
40 40
 	// on non-windows system we use ~ when possible (to shorten things)
41 41
 
42
-	var home string
43
-	if runtime.GOOS != "windows" {
44
-		home = os.Getenv("HOME")
45
-	}
46
-
42
+	home := homedir.Get()
47 43
 	// Pull the list of commands from the "Commands:" section of docker help
48 44
 	helpCmd := exec.Command(dockerBinary, "help")
49 45
 	out, ec, err := runCommandWithOutput(helpCmd)
... ...
@@ -82,7 +75,7 @@ func TestCmdHelpWidth(t *testing.T) {
82 82
 				t.Fatalf("Help for %q is too long(%d chars):\n%s", command, len(line), line)
83 83
 			}
84 84
 			if home != "" && strings.Contains(line, home) {
85
-				t.Fatalf("Help for %q should use ~ instead of %q on:\n%s", command, home, line)
85
+				t.Fatalf("Help for %q should use home shortcut instead of %q on:\n%s", command, home, line)
86 86
 			}
87 87
 		}
88 88
 	}
... ...
@@ -86,12 +86,13 @@ import (
86 86
 	"fmt"
87 87
 	"io"
88 88
 	"os"
89
-	"runtime"
90 89
 	"sort"
91 90
 	"strconv"
92 91
 	"strings"
93 92
 	"text/tabwriter"
94 93
 	"time"
94
+
95
+	"github.com/docker/docker/pkg/homedir"
95 96
 )
96 97
 
97 98
 // 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 {
504 504
 // otherwise, the default values of all defined flags in the set.
505 505
 func (f *FlagSet) PrintDefaults() {
506 506
 	writer := tabwriter.NewWriter(f.Out(), 20, 1, 3, ' ', 0)
507
-	var home string
508
-	if runtime.GOOS != "windows" {
509
-		home = os.Getenv("HOME")
510
-	}
507
+	home := homedir.Get()
511 508
 	f.VisitAll(func(flag *Flag) {
512 509
 		format := "  -%s=%s"
513
-		if _, ok := flag.Value.(*stringValue); ok {
514
-			// put quotes on the value
515
-			format = "  -%s=%q"
516
-		}
517 510
 		names := []string{}
518 511
 		for _, name := range flag.Names {
519 512
 			if name[0] != '#' {
... ...
@@ -524,7 +518,7 @@ func (f *FlagSet) PrintDefaults() {
524 524
 			val := flag.DefValue
525 525
 
526 526
 			if home != "" && strings.HasPrefix(val, home) {
527
-				val = "~" + val[len(home):]
527
+				val = homedir.GetShortcutString() + val[len(home):]
528 528
 			}
529 529
 
530 530
 			fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)