Browse code

integration-cli: remove/rename err-returns and remove naked returns

Prevent accidentally shadowing these errors, which are used in defers, and
remove naked returns.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2024/01/03 08:27:52
Showing 2 changed files
... ...
@@ -263,17 +263,16 @@ func (s *DockerCLILogsSuite) TestLogsFollowSlowStdoutConsumer(c *testing.T) {
263 263
 // ConsumeWithSpeed reads chunkSize bytes from reader before sleeping
264 264
 // for interval duration. Returns total read bytes. Send true to the
265 265
 // stop channel to return before reading to EOF on the reader.
266
-func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
266
+func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, _ error) {
267 267
 	buffer := make([]byte, chunkSize)
268 268
 	for {
269
-		var readBytes int
270
-		readBytes, err = reader.Read(buffer)
269
+		readBytes, err := reader.Read(buffer)
271 270
 		n += readBytes
272 271
 		if err != nil {
273
-			if err == io.EOF {
274
-				err = nil
272
+			if err != io.EOF {
273
+				return n, err
275 274
 			}
276
-			return n, err
275
+			return n, nil
277 276
 		}
278 277
 		select {
279 278
 		case <-stop:
... ...
@@ -67,8 +67,9 @@ func RandomTmpDirPath(s string, platform string) string {
67 67
 // of each pipelined with the following (like cmd1 | cmd2 | cmd3 would do).
68 68
 // It returns the final output, the exitCode different from 0 and the error
69 69
 // if something bad happened.
70
+//
70 71
 // Deprecated: use icmd instead
71
-func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error) {
72
+func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, retErr error) {
72 73
 	if len(cmds) < 2 {
73 74
 		return "", errors.New("pipeline does not have multiple cmds")
74 75
 	}
... ...
@@ -77,6 +78,7 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error)
77 77
 	for i, cmd := range cmds {
78 78
 		if i > 0 {
79 79
 			prevCmd := cmds[i-1]
80
+			var err error
80 81
 			cmd.Stdin, err = prevCmd.StdoutPipe()
81 82
 			if err != nil {
82 83
 				return "", fmt.Errorf("cannot set stdout pipe for %s: %v", cmd.Path, err)
... ...
@@ -86,7 +88,7 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error)
86 86
 
87 87
 	// start all cmds except the last
88 88
 	for _, cmd := range cmds[:len(cmds)-1] {
89
-		if err = cmd.Start(); err != nil {
89
+		if err := cmd.Start(); err != nil {
90 90
 			return "", fmt.Errorf("starting %s failed with error: %v", cmd.Path, err)
91 91
 		}
92 92
 	}
... ...
@@ -95,12 +97,12 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error)
95 95
 		var pipeErrMsgs []string
96 96
 		// wait all cmds except the last to release their resources
97 97
 		for _, cmd := range cmds[:len(cmds)-1] {
98
-			if pipeErr := cmd.Wait(); pipeErr != nil {
99
-				pipeErrMsgs = append(pipeErrMsgs, fmt.Sprintf("command %s failed with error: %v", cmd.Path, pipeErr))
98
+			if err := cmd.Wait(); err != nil {
99
+				pipeErrMsgs = append(pipeErrMsgs, fmt.Sprintf("command %s failed with error: %v", cmd.Path, err))
100 100
 			}
101 101
 		}
102
-		if len(pipeErrMsgs) > 0 && err == nil {
103
-			err = fmt.Errorf("pipelineError from Wait: %v", strings.Join(pipeErrMsgs, ", "))
102
+		if len(pipeErrMsgs) > 0 && retErr == nil {
103
+			retErr = fmt.Errorf("pipelineError from Wait: %v", strings.Join(pipeErrMsgs, ", "))
104 104
 		}
105 105
 	}()
106 106