Browse code

Small if err cleaning

Signed-off-by: Antonio Murdaca <me@runcom.ninja>

Antonio Murdaca authored on 2015/04/27 01:50:25
Showing 29 changed files
... ...
@@ -173,7 +173,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
173 173
 			includes = append(includes, ".dockerignore", *dockerfileName)
174 174
 		}
175 175
 
176
-		if err = utils.ValidateContextDirectory(root, excludes); err != nil {
176
+		if err := utils.ValidateContextDirectory(root, excludes); err != nil {
177 177
 			return fmt.Errorf("Error checking context is accessible: '%s'. Please check permissions and try again.", err)
178 178
 		}
179 179
 		options := &archive.TarOptions{
... ...
@@ -31,8 +31,7 @@ func (cli *DockerCli) CmdDiff(args ...string) error {
31 31
 	}
32 32
 
33 33
 	changes := []types.ContainerChange{}
34
-	err = json.NewDecoder(rdr).Decode(&changes)
35
-	if err != nil {
34
+	if err := json.NewDecoder(rdr).Decode(&changes); err != nil {
36 35
 		return err
37 36
 	}
38 37
 
... ...
@@ -30,8 +30,7 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
30 30
 	}
31 31
 
32 32
 	history := []types.ImageHistory{}
33
-	err = json.NewDecoder(rdr).Decode(&history)
34
-	if err != nil {
33
+	if err := json.NewDecoder(rdr).Decode(&history); err != nil {
35 34
 		return err
36 35
 	}
37 36
 
... ...
@@ -92,8 +92,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
92 92
 	}
93 93
 
94 94
 	containers := []types.Container{}
95
-	err = json.NewDecoder(rdr).Decode(&containers)
96
-	if err != nil {
95
+	if err := json.NewDecoder(rdr).Decode(&containers); err != nil {
97 96
 		return err
98 97
 	}
99 98
 
... ...
@@ -37,8 +37,7 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
37 37
 			encounteredError = fmt.Errorf("Error: failed to remove one or more images")
38 38
 		} else {
39 39
 			dels := []types.ImageDelete{}
40
-			err = json.NewDecoder(rdr).Decode(&dels)
41
-			if err != nil {
40
+			if err := json.NewDecoder(rdr).Decode(&dels); err != nil {
42 41
 				fmt.Fprintf(cli.err, "%s\n", err)
43 42
 				encounteredError = fmt.Errorf("Error: failed to remove one or more images")
44 43
 				continue
... ...
@@ -51,8 +51,7 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
51 51
 	}
52 52
 
53 53
 	results := ByStars{}
54
-	err = json.NewDecoder(rdr).Decode(&results)
55
-	if err != nil {
54
+	if err := json.NewDecoder(rdr).Decode(&results); err != nil {
56 55
 		return err
57 56
 	}
58 57
 
... ...
@@ -31,8 +31,7 @@ func (cli *DockerCli) CmdTop(args ...string) error {
31 31
 	}
32 32
 
33 33
 	procList := types.ContainerProcessList{}
34
-	err = json.NewDecoder(stream).Decode(&procList)
35
-	if err != nil {
34
+	if err := json.NewDecoder(stream).Decode(&procList); err != nil {
36 35
 		return err
37 36
 	}
38 37
 
... ...
@@ -107,8 +107,7 @@ func MatchesContentType(contentType, expectedType string) bool {
107 107
 // LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
108 108
 // otherwise generates a new one
109 109
 func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
110
-	err := os.MkdirAll(filepath.Dir(trustKeyPath), 0700)
111
-	if err != nil {
110
+	if err := os.MkdirAll(filepath.Dir(trustKeyPath), 0700); err != nil {
112 111
 		return nil, err
113 112
 	}
114 113
 	trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
... ...
@@ -277,8 +277,7 @@ func (s *Server) postContainersKill(eng *engine.Engine, version version.Version,
277 277
 	if vars == nil {
278 278
 		return fmt.Errorf("Missing parameter")
279 279
 	}
280
-	err := parseForm(r)
281
-	if err != nil {
280
+	if err := parseForm(r); err != nil {
282 281
 		return err
283 282
 	}
284 283
 
... ...
@@ -289,7 +288,7 @@ func (s *Server) postContainersKill(eng *engine.Engine, version version.Version,
289 289
 	if sigStr := vars["signal"]; sigStr != "" {
290 290
 		// Check if we passed the signal as a number:
291 291
 		// The largest legal signal is 31, so let's parse on 5 bits
292
-		sig, err = strconv.ParseUint(sigStr, 10, 5)
292
+		sig, err := strconv.ParseUint(sigStr, 10, 5)
293 293
 		if err != nil {
294 294
 			// The signal is not a number, treat it as a string (either like
295 295
 			// "KILL" or like "SIGKILL")
... ...
@@ -301,7 +300,7 @@ func (s *Server) postContainersKill(eng *engine.Engine, version version.Version,
301 301
 		}
302 302
 	}
303 303
 
304
-	if err = s.daemon.ContainerKill(name, sig); err != nil {
304
+	if err := s.daemon.ContainerKill(name, sig); err != nil {
305 305
 		return err
306 306
 	}
307 307
 
... ...
@@ -148,8 +148,15 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowDecomp
148 148
 	// do the copy (e.g. hash value if cached).  Don't actually do
149 149
 	// the copy until we've looked at all src files
150 150
 	for _, orig := range args[0 : len(args)-1] {
151
-		err := calcCopyInfo(b, cmdName, &copyInfos, orig, dest, allowRemote, allowDecompression)
152
-		if err != nil {
151
+		if err := calcCopyInfo(
152
+			b,
153
+			cmdName,
154
+			&copyInfos,
155
+			orig,
156
+			dest,
157
+			allowRemote,
158
+			allowDecompression,
159
+		); err != nil {
153 160
 			return err
154 161
 		}
155 162
 	}
... ...
@@ -166,8 +166,7 @@ func (configFile *ConfigFile) Save() error {
166 166
 		return err
167 167
 	}
168 168
 
169
-	err = ioutil.WriteFile(configFile.filename, data, 0600)
170
-	if err != nil {
169
+	if err := ioutil.WriteFile(configFile.filename, data, 0600); err != nil {
171 170
 		return err
172 171
 	}
173 172
 
... ...
@@ -149,8 +149,7 @@ func (container *Container) toDisk() error {
149 149
 		return err
150 150
 	}
151 151
 
152
-	err = ioutil.WriteFile(pth, data, 0666)
153
-	if err != nil {
152
+	if err := ioutil.WriteFile(pth, data, 0666); err != nil {
154 153
 		return err
155 154
 	}
156 155
 
... ...
@@ -1181,8 +1181,7 @@ func tempDir(rootDir string) (string, error) {
1181 1181
 	if tmpDir = os.Getenv("DOCKER_TMPDIR"); tmpDir == "" {
1182 1182
 		tmpDir = filepath.Join(rootDir, "tmp")
1183 1183
 	}
1184
-	err := os.MkdirAll(tmpDir, 0700)
1185
-	return tmpDir, err
1184
+	return tmpDir, os.MkdirAll(tmpDir, 0700)
1186 1185
 }
1187 1186
 
1188 1187
 func checkKernel() error {
... ...
@@ -214,8 +214,7 @@ func (d *Daemon) ContainerExecStart(execName string, stdin io.ReadCloser, stdout
214 214
 	// the exitStatus) even after the cmd is done running.
215 215
 
216 216
 	go func() {
217
-		err := container.Exec(execConfig)
218
-		if err != nil {
217
+		if err := container.Exec(execConfig); err != nil {
219 218
 			execErr <- fmt.Errorf("Cannot run exec command %s in container %s: %s", execName, container.ID, err)
220 219
 		}
221 220
 	}()
... ...
@@ -218,7 +218,7 @@ func (devices *DeviceSet) ensureImage(name string, size int64) (string, error) {
218 218
 		}
219 219
 		defer file.Close()
220 220
 
221
-		if err = file.Truncate(size); err != nil {
221
+		if err := file.Truncate(size); err != nil {
222 222
 			return "", err
223 223
 		}
224 224
 	}
... ...
@@ -697,7 +697,7 @@ func (devices *DeviceSet) setupBaseImage() error {
697 697
 
698 698
 	logrus.Debugf("Creating filesystem on base device-mapper thin volume")
699 699
 
700
-	if err = devices.activateDeviceIfNeeded(info); err != nil {
700
+	if err := devices.activateDeviceIfNeeded(info); err != nil {
701 701
 		return err
702 702
 	}
703 703
 
... ...
@@ -706,7 +706,7 @@ func (devices *DeviceSet) setupBaseImage() error {
706 706
 	}
707 707
 
708 708
 	info.Initialized = true
709
-	if err = devices.saveMetadata(info); err != nil {
709
+	if err := devices.saveMetadata(info); err != nil {
710 710
 		info.Initialized = false
711 711
 		return err
712 712
 	}
... ...
@@ -1099,14 +1099,14 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error {
1099 1099
 	// If we didn't just create the data or metadata image, we need to
1100 1100
 	// load the transaction id and migrate old metadata
1101 1101
 	if !createdLoopback {
1102
-		if err = devices.initMetaData(); err != nil {
1102
+		if err := devices.initMetaData(); err != nil {
1103 1103
 			return err
1104 1104
 		}
1105 1105
 	}
1106 1106
 
1107 1107
 	// Right now this loads only NextDeviceId. If there is more metadata
1108 1108
 	// down the line, we might have to move it earlier.
1109
-	if err = devices.loadDeviceSetMetaData(); err != nil {
1109
+	if err := devices.loadDeviceSetMetaData(); err != nil {
1110 1110
 		return err
1111 1111
 	}
1112 1112
 
... ...
@@ -1528,8 +1528,7 @@ func (devices *DeviceSet) MetadataDevicePath() string {
1528 1528
 
1529 1529
 func (devices *DeviceSet) getUnderlyingAvailableSpace(loopFile string) (uint64, error) {
1530 1530
 	buf := new(syscall.Statfs_t)
1531
-	err := syscall.Statfs(loopFile, buf)
1532
-	if err != nil {
1531
+	if err := syscall.Statfs(loopFile, buf); err != nil {
1533 1532
 		logrus.Warnf("Couldn't stat loopfile filesystem %v: %v", loopFile, err)
1534 1533
 		return 0, err
1535 1534
 	}
... ...
@@ -348,9 +348,8 @@ func (graph *Graph) Delete(name string) error {
348 348
 	tmp, err := graph.Mktemp("")
349 349
 	graph.idIndex.Delete(id)
350 350
 	if err == nil {
351
-		err = os.Rename(graph.ImageRoot(id), tmp)
352
-		// On err make tmp point to old dir and cleanup unused tmp dir
353
-		if err != nil {
351
+		if err := os.Rename(graph.ImageRoot(id), tmp); err != nil {
352
+			// On err make tmp point to old dir and cleanup unused tmp dir
354 353
 			os.RemoveAll(tmp)
355 354
 			tmp = graph.ImageRoot(id)
356 355
 		}
... ...
@@ -537,8 +537,7 @@ func (s *TagStore) pullV2Tag(r *registry.Session, out io.Writer, endpoint *regis
537 537
 				di.err <- downloadFunc(di)
538 538
 			}(&downloads[i])
539 539
 		} else {
540
-			err := downloadFunc(&downloads[i])
541
-			if err != nil {
540
+			if err := downloadFunc(&downloads[i]); err != nil {
542 541
 				return false, err
543 542
 			}
544 543
 		}
... ...
@@ -548,8 +547,7 @@ func (s *TagStore) pullV2Tag(r *registry.Session, out io.Writer, endpoint *regis
548 548
 	for i := len(downloads) - 1; i >= 0; i-- {
549 549
 		d := &downloads[i]
550 550
 		if d.err != nil {
551
-			err := <-d.err
552
-			if err != nil {
551
+			if err := <-d.err; err != nil {
553 552
 				return false, err
554 553
 			}
555 554
 		}
... ...
@@ -367,8 +367,7 @@ func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, o
367 367
 			logrus.Debugf("Pushing layer: %s", layer.ID)
368 368
 
369 369
 			if layer.Config != nil && metadata.Image != layer.ID {
370
-				err = runconfig.Merge(&metadata, layer.Config)
371
-				if err != nil {
370
+				if err := runconfig.Merge(&metadata, layer.Config); err != nil {
372 371
 					return err
373 372
 				}
374 373
 			}
... ...
@@ -268,8 +268,7 @@ func NewImgJSON(src []byte) (*Image, error) {
268 268
 func ValidateID(id string) error {
269 269
 	validHex := regexp.MustCompile(`^([a-f0-9]{64})$`)
270 270
 	if ok := validHex.MatchString(id); !ok {
271
-		err := fmt.Errorf("image ID '%s' is invalid", id)
272
-		return err
271
+		return fmt.Errorf("image ID '%s' is invalid", id)
273 272
 	}
274 273
 	return nil
275 274
 }
... ...
@@ -176,7 +176,7 @@ func (s *DockerSuite) TestContainerApiStartVolumesFrom(c *check.C) {
176 176
 		c.Fatal(out, err)
177 177
 	}
178 178
 
179
-	name := "testing"
179
+	name := "TestContainerApiStartDupVolumeBinds"
180 180
 	config := map[string]interface{}{
181 181
 		"Image":   "busybox",
182 182
 		"Volumes": map[string]struct{}{volPath: {}},
... ...
@@ -620,7 +620,7 @@ func (s *DockerSuite) TestContainerApiCommit(c *check.C) {
620 620
 		c.Fatal(err, out)
621 621
 	}
622 622
 
623
-	name := "testcommit" + stringid.GenerateRandomID()
623
+	name := "TestContainerApiCommit"
624 624
 	status, b, err := sockRequest("POST", "/commit?repo="+name+"&testtag=tag&container="+cName, nil)
625 625
 	c.Assert(status, check.Equals, http.StatusCreated)
626 626
 	c.Assert(err, check.IsNil)
... ...
@@ -842,12 +842,12 @@ func (s *DockerSuite) TestStartWithTooLowMemoryLimit(c *check.C) {
842 842
 }
843 843
 
844 844
 func (s *DockerSuite) TestContainerApiRename(c *check.C) {
845
-	runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
845
+	runCmd := exec.Command(dockerBinary, "run", "--name", "TestContainerApiRename", "-d", "busybox", "sh")
846 846
 	out, _, err := runCommandWithOutput(runCmd)
847 847
 	c.Assert(err, check.IsNil)
848 848
 
849 849
 	containerID := strings.TrimSpace(out)
850
-	newName := "new_name" + stringid.GenerateRandomID()
850
+	newName := "TestContainerApiRenameNew"
851 851
 	statusCode, _, err := sockRequest("POST", "/containers/"+containerID+"/rename?name="+newName, nil)
852 852
 
853 853
 	// 204 No Content is expected, not 200
... ...
@@ -169,8 +169,7 @@ func runCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode in
169 169
 }
170 170
 
171 171
 func unmarshalJSON(data []byte, result interface{}) error {
172
-	err := json.Unmarshal(data, result)
173
-	if err != nil {
172
+	if err := json.Unmarshal(data, result); err != nil {
174 173
 		return err
175 174
 	}
176 175
 
... ...
@@ -65,8 +65,7 @@ import (
65 65
 func (mj *JSONLog) MarshalJSON() ([]byte, error) {
66 66
 	var buf bytes.Buffer
67 67
 	buf.Grow(1024)
68
-	err := mj.MarshalJSONBuf(&buf)
69
-	if err != nil {
68
+	if err := mj.MarshalJSONBuf(&buf); err != nil {
70 69
 		return nil, err
71 70
 	}
72 71
 	return buf.Bytes(), nil
... ...
@@ -486,8 +486,7 @@ func (f *FlagSet) Set(name, value string) error {
486 486
 	if !ok {
487 487
 		return fmt.Errorf("no such flag -%v", name)
488 488
 	}
489
-	err := flag.Value.Set(value)
490
-	if err != nil {
489
+	if err := flag.Value.Set(value); err != nil {
491 490
 		return err
492 491
 	}
493 492
 	if f.actual == nil {
... ...
@@ -12,8 +12,7 @@ import (
12 12
 // Throws an error if the file does not exist
13 13
 func Lstat(path string) (*Stat_t, error) {
14 14
 	s := &syscall.Stat_t{}
15
-	err := syscall.Lstat(path, s)
16
-	if err != nil {
15
+	if err := syscall.Lstat(path, s); err != nil {
17 16
 		return nil, err
18 17
 	}
19 18
 	return fromStatT(s)
... ...
@@ -20,8 +20,7 @@ func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
20 20
 // Throws an error if the file does not exist
21 21
 func Stat(path string) (*Stat_t, error) {
22 22
 	s := &syscall.Stat_t{}
23
-	err := syscall.Stat(path, s)
24
-	if err != nil {
23
+	if err := syscall.Stat(path, s); err != nil {
25 24
 		return nil, err
26 25
 	}
27 26
 	return fromStatT(s)
... ...
@@ -17,8 +17,7 @@ type conn struct {
17 17
 
18 18
 func (c *conn) Read(b []byte) (int, error) {
19 19
 	if c.timeout > 0 {
20
-		err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout))
21
-		if err != nil {
20
+		if err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout)); err != nil {
22 21
 			return 0, err
23 22
 		}
24 23
 	}
... ...
@@ -597,8 +597,7 @@ func (r *Session) SearchRepositories(term string) (*SearchResults, error) {
597 597
 		return nil, httputils.NewHTTPRequestError(fmt.Sprintf("Unexpected status code %d", res.StatusCode), res)
598 598
 	}
599 599
 	result := new(SearchResults)
600
-	err = json.NewDecoder(res.Body).Decode(result)
601
-	return result, err
600
+	return result, json.NewDecoder(res.Body).Decode(result)
602 601
 }
603 602
 
604 603
 func (r *Session) GetAuthConfig(withPasswd bool) *cliconfig.AuthConfig {
... ...
@@ -387,10 +387,8 @@ func (r *Session) GetV2RemoteTags(ep *Endpoint, imageName string, auth *RequestA
387 387
 		return nil, httputils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to fetch for %s", res.StatusCode, imageName), res)
388 388
 	}
389 389
 
390
-	decoder := json.NewDecoder(res.Body)
391 390
 	var remote remoteTags
392
-	err = decoder.Decode(&remote)
393
-	if err != nil {
391
+	if err := json.NewDecoder(res.Body).Decode(&remote); err != nil {
394 392
 		return nil, fmt.Errorf("Error while decoding the http response: %s", err)
395 393
 	}
396 394
 	return remote.Tags, nil
... ...
@@ -62,8 +62,7 @@ func NewTrustStore(path string) (*TrustStore, error) {
62 62
 		baseEndpoints: endpoints,
63 63
 	}
64 64
 
65
-	err = t.reload()
66
-	if err != nil {
65
+	if err := t.reload(); err != nil {
67 66
 		return nil, err
68 67
 	}
69 68
 
... ...
@@ -170,8 +169,7 @@ func (t *TrustStore) fetch() {
170 170
 			continue
171 171
 		}
172 172
 		// TODO check if value differs
173
-		err = ioutil.WriteFile(path.Join(t.path, bg+".json"), b, 0600)
174
-		if err != nil {
173
+		if err := ioutil.WriteFile(path.Join(t.path, bg+".json"), b, 0600); err != nil {
175 174
 			logrus.Infof("Error writing trust graph statement: %s", err)
176 175
 		}
177 176
 		fetchCount++
... ...
@@ -180,8 +178,7 @@ func (t *TrustStore) fetch() {
180 180
 
181 181
 	if fetchCount > 0 {
182 182
 		go func() {
183
-			err := t.reload()
184
-			if err != nil {
183
+			if err := t.reload(); err != nil {
185 184
 				logrus.Infof("Reload of trust graph failed: %s", err)
186 185
 			}
187 186
 		}()