Skip to content

Commit e282a5c

Browse files
committed
refactor: refactor error handling and add print statements in file operations
- Remove error handling code in `proxy.go` - Add `fmt.Println("success")` in `proxy.go` - Remove error handling code in `scp.go` - Add `fmt.Println("success")` in `scp.go` - Remove `fmt.Println("don is :", done, "stdout is :", stdout, "; stderr is :", stderr)` in `ssh.go` - Add `fmt.Println("don is :", done, "stdout is :", stdout, "; stderr is :", stderr)` in `ssh.go` - Remove `fmt.Println("err: " + err.Error())` and add `panic("err: " + err.Error())` in `stream.go` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 27994a7 commit e282a5c

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

example/proxy/proxy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ func main() {
2424
// Call Scp method with file you want to upload to remote server.
2525
// Please make sure the `tmp` floder exists.
2626
err := ssh.Scp("/root/source.csv", "/tmp/target.csv")
27-
28-
// Handle errors
2927
if err != nil {
3028
panic("Can't run remote command: " + err.Error())
31-
} else {
32-
fmt.Println("success")
3329
}
30+
fmt.Println("success")
3431
}

example/scp/scp.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ func main() {
1818
// Call Scp method with file you want to upload to remote server.
1919
// Please make sure the `tmp` floder exists.
2020
err := ssh.Scp("/root/source.csv", "/tmp/target.csv")
21-
2221
// Handle errors
2322
if err != nil {
2423
panic("Can't run remote command: " + err.Error())
25-
} else {
26-
fmt.Println("success")
2724
}
25+
fmt.Println("success")
2826
}

example/ssh/ssh.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func main() {
4848
// Handle errors
4949
if err != nil {
5050
panic("Can't run remote command: " + err.Error())
51-
} else {
52-
fmt.Println("don is :", done, "stdout is :", stdout, "; stderr is :", stderr)
5351
}
52+
fmt.Println("don is :", done, "stdout is :", stdout, "; stderr is :", stderr)
5453
}

example/stream/stream.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,29 @@ func main() {
2222
// Handle errors
2323
if err != nil {
2424
panic("Can't run remote command: " + err.Error())
25-
} else {
26-
// read from the output channel until the done signal is passed
27-
isTimeout := true
28-
loop:
29-
for {
30-
select {
31-
case isTimeout = <-doneChan:
32-
break loop
33-
case outline := <-stdoutChan:
34-
fmt.Println("out:", outline)
35-
case errline := <-stderrChan:
36-
fmt.Println("err:", errline)
37-
case err = <-errChan:
38-
}
25+
}
26+
// read from the output channel until the done signal is passed
27+
isTimeout := true
28+
loop:
29+
for {
30+
select {
31+
case isTimeout = <-doneChan:
32+
break loop
33+
case outline := <-stdoutChan:
34+
fmt.Println("out:", outline)
35+
case errline := <-stderrChan:
36+
fmt.Println("err:", errline)
37+
case err = <-errChan:
3938
}
39+
}
4040

41-
// get exit code or command error.
42-
if err != nil {
43-
fmt.Println("err: " + err.Error())
44-
}
41+
// get exit code or command error.
42+
if err != nil {
43+
panic("err: " + err.Error())
44+
}
4545

46-
// command time out
47-
if !isTimeout {
48-
fmt.Println("Error: command timeout")
49-
}
46+
// command time out
47+
if !isTimeout {
48+
fmt.Println("Error: command timeout")
5049
}
5150
}

0 commit comments

Comments
 (0)