@@ -13,30 +13,35 @@ defmodule TP do
13
13
14
14
Stream . resource (
15
15
fn ->
16
- { :ok , chan } = SSHKit.Channel . open ( conn , [ ] )
17
- command = SSHKit.Context . build ( ctx , "tar -x" )
18
- :success = SSHKit.Channel . exec ( chan , command )
19
-
20
16
owner = self ( )
21
17
22
18
tarpipe = spawn ( fn ->
19
+ { :ok , chan } = SSHKit.Channel . open ( conn , [ ] )
20
+ command = SSHKit.Context . build ( ctx , "tar -x" )
21
+ :success = SSHKit.Channel . exec ( chan , command )
22
+
23
+ # TODO: What if command immediately exits or does not exist?
24
+ # IO.inspect(SSHKit.Channel.recv(chan, 1000))
25
+
23
26
{ :ok , tar } = :erl_tar . init ( chan , :write , fn
24
27
:position , { ^ chan , position } ->
25
28
# IO.inspect(position, label: "position")
26
29
{ :ok , 0 }
27
30
28
31
:write , { ^ chan , data } ->
29
32
# TODO: Send data in chunks based on channel window size?
30
- # IO.inspect(data, label: "write")
33
+ IO . inspect ( data , label: "write" )
31
34
# In case of failing upload, check command output:
32
35
# IO.inspect(SSHKit.Channel.recv(chan, 0))
33
36
chunk = to_binary ( data )
34
37
35
38
receive do
36
39
:cont ->
37
- :ok = SSHKit.Channel . send ( chan , chunk )
40
+ case SSHKit.Channel . send ( chan , chunk ) do
41
+ :ok -> send ( owner , { :write , chan , self ( ) , chunk } )
42
+ other -> send ( owner , { :error , chan , self ( ) , other } )
43
+ end
38
44
end
39
- send ( owner , { :write , chan , self ( ) , chunk } )
40
45
:ok
41
46
42
47
:close , ^ chan ->
@@ -48,24 +53,34 @@ defmodule TP do
48
53
49
54
:ok = :erl_tar . add ( tar , to_charlist ( source ) , to_charlist ( Path . basename ( source ) ) , [ ] )
50
55
:ok = :erl_tar . close ( tar )
56
+
57
+ :ok = SSHKit.Channel . close ( chan )
51
58
end )
52
59
53
- { chan , tarpipe }
60
+ tarpipe
54
61
end ,
55
- fn { chan , tarpipe } ->
62
+ fn tarpipe ->
56
63
send ( tarpipe , :cont )
57
64
58
65
receive do
59
- { :write , ^ chan , ^ tarpipe , data } ->
60
- { [ { :write , chan , data } ] , { chan , tarpipe } }
66
+ { :write , chan , ^ tarpipe , data } ->
67
+ { [ { :write , chan , data } ] , tarpipe }
68
+
69
+ { :close , chan , ^ tarpipe } ->
70
+ { :halt , tarpipe }
61
71
62
- { :close , ^ chan , ^ tarpipe } ->
63
- { :halt , { chan , tarpipe } }
72
+ { :error , chan , ^ tarpipe , error } ->
73
+ IO . inspect ( error , label: "received error" )
74
+ { :halt , tarpipe }
64
75
end
76
+
77
+ # case Tarpipe.proceed(tarpipe) do
78
+ # {:write, …} -> {[], tarpipe}
79
+ # {:error, …} -> raise
80
+ # end
65
81
end ,
66
- fn { chan , tarpipe } ->
67
- :ok = SSHKit.Channel . close ( chan )
68
- :ok = SSHKit.Channel . flush ( chan )
82
+ fn tarpipe ->
83
+ nil # :ok = Tarpipe.close(tarpipe)
69
84
end
70
85
)
71
86
end
0 commit comments