Browse code

golint for cliconfig

- fully capitalize HTTP in HTTPHeaders
- comment for CONFIGFILE
- camelcase and privatize oldConfigfile, defaultIndexserver
- remove unused var errConfigFileMissing
- comments for methods and functions throughout
- external references to renamed variables changed

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>

Morgan Bauer authored on 2015/07/21 07:39:07
Showing 6 changed files
... ...
@@ -145,7 +145,7 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.Rea
145 145
 
146 146
 	// Add CLI Config's HTTP Headers BEFORE we set the Docker headers
147 147
 	// then the user can't change OUR headers
148
-	for k, v := range cli.configFile.HttpHeaders {
148
+	for k, v := range cli.configFile.HTTPHeaders {
149 149
 		req.Header.Set(k, v)
150 150
 	}
151 151
 
... ...
@@ -72,7 +72,7 @@ func (cli *DockerCli) clientRequest(method, path string, in io.Reader, headers m
72 72
 
73 73
 	// Add CLI Config's HTTP Headers BEFORE we set the Docker headers
74 74
 	// then the user can't change OUR headers
75
-	for k, v := range cli.configFile.HttpHeaders {
75
+	for k, v := range cli.configFile.HTTPHeaders {
76 76
 		req.Header.Set(k, v)
77 77
 	}
78 78
 
... ...
@@ -3,7 +3,6 @@ package cliconfig
3 3
 import (
4 4
 	"encoding/base64"
5 5
 	"encoding/json"
6
-	"errors"
7 6
 	"fmt"
8 7
 	"io/ioutil"
9 8
 	"os"
... ...
@@ -15,19 +14,18 @@ import (
15 15
 )
16 16
 
17 17
 const (
18
-	// Where we store the config file
19
-	CONFIGFILE     = "config.json"
20
-	OLD_CONFIGFILE = ".dockercfg"
18
+	// ConfigFile is the name of config file
19
+	ConfigFileName = "config.json"
20
+	oldConfigfile  = ".dockercfg"
21 21
 
22 22
 	// This constant is only used for really old config files when the
23 23
 	// URL wasn't saved as part of the config file and it was just
24 24
 	// assumed to be this value.
25
-	DEFAULT_INDEXSERVER = "https://index.docker.io/v1/"
25
+	defaultIndexserver = "https://index.docker.io/v1/"
26 26
 )
27 27
 
28 28
 var (
29
-	configDir            = os.Getenv("DOCKER_CONFIG")
30
-	ErrConfigFileMissing = errors.New("The Auth config file is missing")
29
+	configDir = os.Getenv("DOCKER_CONFIG")
31 30
 )
32 31
 
33 32
 func init() {
... ...
@@ -36,15 +34,17 @@ func init() {
36 36
 	}
37 37
 }
38 38
 
39
+// ConfigDir returns the directory the configuration file is stored in
39 40
 func ConfigDir() string {
40 41
 	return configDir
41 42
 }
42 43
 
44
+// SetConfigDir sets the directory the configuration file is stored in
43 45
 func SetConfigDir(dir string) {
44 46
 	configDir = dir
45 47
 }
46 48
 
47
-// Registry Auth Info
49
+// AuthConfig contains authorization information for connecting to a Registry
48 50
 type AuthConfig struct {
49 51
 	Username      string `json:"username,omitempty"`
50 52
 	Password      string `json:"password,omitempty"`
... ...
@@ -53,22 +53,24 @@ type AuthConfig struct {
53 53
 	ServerAddress string `json:"serveraddress,omitempty"`
54 54
 }
55 55
 
56
-// ~/.docker/config.json file info
56
+// ConfigFile ~/.docker/config.json file info
57 57
 type ConfigFile struct {
58 58
 	AuthConfigs map[string]AuthConfig `json:"auths"`
59
-	HttpHeaders map[string]string     `json:"HttpHeaders,omitempty"`
59
+	HTTPHeaders map[string]string     `json:"HttpHeaders,omitempty"`
60 60
 	filename    string                // Note: not serialized - for internal use only
61 61
 }
62 62
 
63
+// NewConfigFile initilizes an empty configuration file for the given filename 'fn'
63 64
 func NewConfigFile(fn string) *ConfigFile {
64 65
 	return &ConfigFile{
65 66
 		AuthConfigs: make(map[string]AuthConfig),
66
-		HttpHeaders: make(map[string]string),
67
+		HTTPHeaders: make(map[string]string),
67 68
 		filename:    fn,
68 69
 	}
69 70
 }
70 71
 
71
-// load up the auth config information and return values
72
+// Load reads the configuration files in the given directory, and sets up
73
+// the auth config information and return values.
72 74
 // FIXME: use the internal golang config parser
73 75
 func Load(configDir string) (*ConfigFile, error) {
74 76
 	if configDir == "" {
... ...
@@ -77,7 +79,7 @@ func Load(configDir string) (*ConfigFile, error) {
77 77
 
78 78
 	configFile := ConfigFile{
79 79
 		AuthConfigs: make(map[string]AuthConfig),
80
-		filename:    filepath.Join(configDir, CONFIGFILE),
80
+		filename:    filepath.Join(configDir, ConfigFileName),
81 81
 	}
82 82
 
83 83
 	// Try happy path first - latest config file
... ...
@@ -110,8 +112,7 @@ func Load(configDir string) (*ConfigFile, error) {
110 110
 	}
111 111
 
112 112
 	// Can't find latest config file so check for the old one
113
-	confFile := filepath.Join(homedir.Get(), OLD_CONFIGFILE)
114
-
113
+	confFile := filepath.Join(homedir.Get(), oldConfigfile)
115 114
 	if _, err := os.Stat(confFile); err != nil {
116 115
 		return &configFile, nil //missing file is not an error
117 116
 	}
... ...
@@ -140,8 +141,8 @@ func Load(configDir string) (*ConfigFile, error) {
140 140
 			return &configFile, fmt.Errorf("Invalid Auth config file")
141 141
 		}
142 142
 		authConfig.Email = origEmail[1]
143
-		authConfig.ServerAddress = DEFAULT_INDEXSERVER
144
-		configFile.AuthConfigs[DEFAULT_INDEXSERVER] = authConfig
143
+		authConfig.ServerAddress = defaultIndexserver
144
+		configFile.AuthConfigs[defaultIndexserver] = authConfig
145 145
 	} else {
146 146
 		for k, authConfig := range configFile.AuthConfigs {
147 147
 			authConfig.Username, authConfig.Password, err = DecodeAuth(authConfig.Auth)
... ...
@@ -156,12 +157,13 @@ func Load(configDir string) (*ConfigFile, error) {
156 156
 	return &configFile, nil
157 157
 }
158 158
 
159
+// Save encodes and writes out all the authorization information
159 160
 func (configFile *ConfigFile) Save() error {
160 161
 	// Encode sensitive data into a new/temp struct
161 162
 	tmpAuthConfigs := make(map[string]AuthConfig, len(configFile.AuthConfigs))
162 163
 	for k, authConfig := range configFile.AuthConfigs {
163 164
 		authCopy := authConfig
164
-
165
+		// encode and save the authstring, while blanking out the original fields
165 166
 		authCopy.Auth = EncodeAuth(&authCopy)
166 167
 		authCopy.Username = ""
167 168
 		authCopy.Password = ""
... ...
@@ -189,11 +191,12 @@ func (configFile *ConfigFile) Save() error {
189 189
 	return nil
190 190
 }
191 191
 
192
-func (config *ConfigFile) Filename() string {
193
-	return config.filename
192
+// Filename returns the name of the configuration file
193
+func (configFile *ConfigFile) Filename() string {
194
+	return configFile.filename
194 195
 }
195 196
 
196
-// create a base64 encoded auth string to store in config
197
+// EncodeAuth creates a base64 encoded string to containing authorization information
197 198
 func EncodeAuth(authConfig *AuthConfig) string {
198 199
 	authStr := authConfig.Username + ":" + authConfig.Password
199 200
 	msg := []byte(authStr)
... ...
@@ -202,7 +205,7 @@ func EncodeAuth(authConfig *AuthConfig) string {
202 202
 	return string(encoded)
203 203
 }
204 204
 
205
-// decode the auth string
205
+// DecodeAuth decodes a base64 encoded string and returns username and password
206 206
 func DecodeAuth(authStr string) (string, string, error) {
207 207
 	decLen := base64.StdEncoding.DecodedLen(len(authStr))
208 208
 	decoded := make([]byte, decLen)
209 209
deleted file mode 100644
... ...
@@ -1,157 +0,0 @@
1
-package cliconfig
2
-
3
-import (
4
-	"io/ioutil"
5
-	"os"
6
-	"path/filepath"
7
-	"runtime"
8
-	"strings"
9
-	"testing"
10
-
11
-	"github.com/docker/docker/pkg/homedir"
12
-)
13
-
14
-func TestMissingFile(t *testing.T) {
15
-	tmpHome, _ := ioutil.TempDir("", "config-test")
16
-
17
-	config, err := Load(tmpHome)
18
-	if err != nil {
19
-		t.Fatalf("Failed loading on missing file: %q", err)
20
-	}
21
-
22
-	// Now save it and make sure it shows up in new form
23
-	err = config.Save()
24
-	if err != nil {
25
-		t.Fatalf("Failed to save: %q", err)
26
-	}
27
-
28
-	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
29
-	if !strings.Contains(string(buf), `"auths":`) {
30
-		t.Fatalf("Should have save in new form: %s", string(buf))
31
-	}
32
-}
33
-
34
-func TestSaveFileToDirs(t *testing.T) {
35
-	tmpHome, _ := ioutil.TempDir("", "config-test")
36
-
37
-	tmpHome += "/.docker"
38
-
39
-	config, err := Load(tmpHome)
40
-	if err != nil {
41
-		t.Fatalf("Failed loading on missing file: %q", err)
42
-	}
43
-
44
-	// Now save it and make sure it shows up in new form
45
-	err = config.Save()
46
-	if err != nil {
47
-		t.Fatalf("Failed to save: %q", err)
48
-	}
49
-
50
-	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
51
-	if !strings.Contains(string(buf), `"auths":`) {
52
-		t.Fatalf("Should have save in new form: %s", string(buf))
53
-	}
54
-}
55
-
56
-func TestEmptyFile(t *testing.T) {
57
-	tmpHome, _ := ioutil.TempDir("", "config-test")
58
-	fn := filepath.Join(tmpHome, CONFIGFILE)
59
-	ioutil.WriteFile(fn, []byte(""), 0600)
60
-
61
-	_, err := Load(tmpHome)
62
-	if err == nil {
63
-		t.Fatalf("Was supposed to fail")
64
-	}
65
-}
66
-
67
-func TestEmptyJson(t *testing.T) {
68
-	tmpHome, _ := ioutil.TempDir("", "config-test")
69
-	fn := filepath.Join(tmpHome, CONFIGFILE)
70
-	ioutil.WriteFile(fn, []byte("{}"), 0600)
71
-
72
-	config, err := Load(tmpHome)
73
-	if err != nil {
74
-		t.Fatalf("Failed loading on empty json file: %q", err)
75
-	}
76
-
77
-	// Now save it and make sure it shows up in new form
78
-	err = config.Save()
79
-	if err != nil {
80
-		t.Fatalf("Failed to save: %q", err)
81
-	}
82
-
83
-	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
84
-	if !strings.Contains(string(buf), `"auths":`) {
85
-		t.Fatalf("Should have save in new form: %s", string(buf))
86
-	}
87
-}
88
-
89
-func TestOldJson(t *testing.T) {
90
-	if runtime.GOOS == "windows" {
91
-		return
92
-	}
93
-
94
-	tmpHome, _ := ioutil.TempDir("", "config-test")
95
-	defer os.RemoveAll(tmpHome)
96
-
97
-	homeKey := homedir.Key()
98
-	homeVal := homedir.Get()
99
-
100
-	defer func() { os.Setenv(homeKey, homeVal) }()
101
-	os.Setenv(homeKey, tmpHome)
102
-
103
-	fn := filepath.Join(tmpHome, OLD_CONFIGFILE)
104
-	js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
105
-	ioutil.WriteFile(fn, []byte(js), 0600)
106
-
107
-	config, err := Load(tmpHome)
108
-	if err != nil {
109
-		t.Fatalf("Failed loading on empty json file: %q", err)
110
-	}
111
-
112
-	ac := config.AuthConfigs["https://index.docker.io/v1/"]
113
-	if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
114
-		t.Fatalf("Missing data from parsing:\n%q", config)
115
-	}
116
-
117
-	// Now save it and make sure it shows up in new form
118
-	err = config.Save()
119
-	if err != nil {
120
-		t.Fatalf("Failed to save: %q", err)
121
-	}
122
-
123
-	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
124
-	if !strings.Contains(string(buf), `"auths":`) ||
125
-		!strings.Contains(string(buf), "user@example.com") {
126
-		t.Fatalf("Should have save in new form: %s", string(buf))
127
-	}
128
-}
129
-
130
-func TestNewJson(t *testing.T) {
131
-	tmpHome, _ := ioutil.TempDir("", "config-test")
132
-	fn := filepath.Join(tmpHome, CONFIGFILE)
133
-	js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
134
-	ioutil.WriteFile(fn, []byte(js), 0600)
135
-
136
-	config, err := Load(tmpHome)
137
-	if err != nil {
138
-		t.Fatalf("Failed loading on empty json file: %q", err)
139
-	}
140
-
141
-	ac := config.AuthConfigs["https://index.docker.io/v1/"]
142
-	if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
143
-		t.Fatalf("Missing data from parsing:\n%q", config)
144
-	}
145
-
146
-	// Now save it and make sure it shows up in new form
147
-	err = config.Save()
148
-	if err != nil {
149
-		t.Fatalf("Failed to save: %q", err)
150
-	}
151
-
152
-	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, CONFIGFILE))
153
-	if !strings.Contains(string(buf), `"auths":`) ||
154
-		!strings.Contains(string(buf), "user@example.com") {
155
-		t.Fatalf("Should have save in new form: %s", string(buf))
156
-	}
157
-}
158 1
new file mode 100644
... ...
@@ -0,0 +1,157 @@
0
+package cliconfig
1
+
2
+import (
3
+	"io/ioutil"
4
+	"os"
5
+	"path/filepath"
6
+	"runtime"
7
+	"strings"
8
+	"testing"
9
+
10
+	"github.com/docker/docker/pkg/homedir"
11
+)
12
+
13
+func TestMissingFile(t *testing.T) {
14
+	tmpHome, _ := ioutil.TempDir("", "config-test")
15
+
16
+	config, err := Load(tmpHome)
17
+	if err != nil {
18
+		t.Fatalf("Failed loading on missing file: %q", err)
19
+	}
20
+
21
+	// Now save it and make sure it shows up in new form
22
+	err = config.Save()
23
+	if err != nil {
24
+		t.Fatalf("Failed to save: %q", err)
25
+	}
26
+
27
+	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
28
+	if !strings.Contains(string(buf), `"auths":`) {
29
+		t.Fatalf("Should have save in new form: %s", string(buf))
30
+	}
31
+}
32
+
33
+func TestSaveFileToDirs(t *testing.T) {
34
+	tmpHome, _ := ioutil.TempDir("", "config-test")
35
+
36
+	tmpHome += "/.docker"
37
+
38
+	config, err := Load(tmpHome)
39
+	if err != nil {
40
+		t.Fatalf("Failed loading on missing file: %q", err)
41
+	}
42
+
43
+	// Now save it and make sure it shows up in new form
44
+	err = config.Save()
45
+	if err != nil {
46
+		t.Fatalf("Failed to save: %q", err)
47
+	}
48
+
49
+	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
50
+	if !strings.Contains(string(buf), `"auths":`) {
51
+		t.Fatalf("Should have save in new form: %s", string(buf))
52
+	}
53
+}
54
+
55
+func TestEmptyFile(t *testing.T) {
56
+	tmpHome, _ := ioutil.TempDir("", "config-test")
57
+	fn := filepath.Join(tmpHome, ConfigFileName)
58
+	ioutil.WriteFile(fn, []byte(""), 0600)
59
+
60
+	_, err := Load(tmpHome)
61
+	if err == nil {
62
+		t.Fatalf("Was supposed to fail")
63
+	}
64
+}
65
+
66
+func TestEmptyJson(t *testing.T) {
67
+	tmpHome, _ := ioutil.TempDir("", "config-test")
68
+	fn := filepath.Join(tmpHome, ConfigFileName)
69
+	ioutil.WriteFile(fn, []byte("{}"), 0600)
70
+
71
+	config, err := Load(tmpHome)
72
+	if err != nil {
73
+		t.Fatalf("Failed loading on empty json file: %q", err)
74
+	}
75
+
76
+	// Now save it and make sure it shows up in new form
77
+	err = config.Save()
78
+	if err != nil {
79
+		t.Fatalf("Failed to save: %q", err)
80
+	}
81
+
82
+	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
83
+	if !strings.Contains(string(buf), `"auths":`) {
84
+		t.Fatalf("Should have save in new form: %s", string(buf))
85
+	}
86
+}
87
+
88
+func TestOldJson(t *testing.T) {
89
+	if runtime.GOOS == "windows" {
90
+		return
91
+	}
92
+
93
+	tmpHome, _ := ioutil.TempDir("", "config-test")
94
+	defer os.RemoveAll(tmpHome)
95
+
96
+	homeKey := homedir.Key()
97
+	homeVal := homedir.Get()
98
+
99
+	defer func() { os.Setenv(homeKey, homeVal) }()
100
+	os.Setenv(homeKey, tmpHome)
101
+
102
+	fn := filepath.Join(tmpHome, oldConfigfile)
103
+	js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
104
+	ioutil.WriteFile(fn, []byte(js), 0600)
105
+
106
+	config, err := Load(tmpHome)
107
+	if err != nil {
108
+		t.Fatalf("Failed loading on empty json file: %q", err)
109
+	}
110
+
111
+	ac := config.AuthConfigs["https://index.docker.io/v1/"]
112
+	if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
113
+		t.Fatalf("Missing data from parsing:\n%q", config)
114
+	}
115
+
116
+	// Now save it and make sure it shows up in new form
117
+	err = config.Save()
118
+	if err != nil {
119
+		t.Fatalf("Failed to save: %q", err)
120
+	}
121
+
122
+	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
123
+	if !strings.Contains(string(buf), `"auths":`) ||
124
+		!strings.Contains(string(buf), "user@example.com") {
125
+		t.Fatalf("Should have save in new form: %s", string(buf))
126
+	}
127
+}
128
+
129
+func TestNewJson(t *testing.T) {
130
+	tmpHome, _ := ioutil.TempDir("", "config-test")
131
+	fn := filepath.Join(tmpHome, ConfigFileName)
132
+	js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } } }`
133
+	ioutil.WriteFile(fn, []byte(js), 0600)
134
+
135
+	config, err := Load(tmpHome)
136
+	if err != nil {
137
+		t.Fatalf("Failed loading on empty json file: %q", err)
138
+	}
139
+
140
+	ac := config.AuthConfigs["https://index.docker.io/v1/"]
141
+	if ac.Email != "user@example.com" || ac.Username != "joejoe" || ac.Password != "hello" {
142
+		t.Fatalf("Missing data from parsing:\n%q", config)
143
+	}
144
+
145
+	// Now save it and make sure it shows up in new form
146
+	err = config.Save()
147
+	if err != nil {
148
+		t.Fatalf("Failed to save: %q", err)
149
+	}
150
+
151
+	buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName))
152
+	if !strings.Contains(string(buf), `"auths":`) ||
153
+		!strings.Contains(string(buf), "user@example.com") {
154
+		t.Fatalf("Should have save in new form: %s", string(buf))
155
+	}
156
+}
... ...
@@ -34,7 +34,7 @@ func setupTempConfigFile() (*cliconfig.ConfigFile, error) {
34 34
 	if err != nil {
35 35
 		return nil, err
36 36
 	}
37
-	root = filepath.Join(root, cliconfig.CONFIGFILE)
37
+	root = filepath.Join(root, cliconfig.ConfigFileName)
38 38
 	configFile := cliconfig.NewConfigFile(root)
39 39
 
40 40
 	for _, registry := range []string{"testIndex", INDEXSERVER} {