55 "encoding/json"
66 "errors"
77 "fmt"
8+ "io"
89 "os"
910 "path"
1011 "runtime"
@@ -46,6 +47,13 @@ func New(updateType update.ApplicationType) *Application {
4647 return app
4748}
4849
50+ // RunWithWriter runs CLI commands with the given args, writing output to w.
51+ // Suitable for use in tests. Unlike Run(), it does not read os.Args or call os.Exit.
52+ func (a * Application ) RunWithWriter (args []string , w io.Writer ) error {
53+ a .cliapp .Writer = w
54+ return a .cliapp .Run (args )
55+ }
56+
4957func (a * Application ) Run () {
5058 if len (os .Args ) == 1 {
5159 return
@@ -129,15 +137,15 @@ func (a *Application) init() {
129137 Usage : "Print your server status, network stats" ,
130138 Before : a .initApiConnection ,
131139 Action : func (c * cli.Context ) error {
132- return printStatus (a .api )
140+ return printStatus (a .api , c . App . Writer )
133141 },
134142 },
135143 {
136144 Name : "id" ,
137145 Usage : "Print your peer id" ,
138146 Before : a .initApiConnection ,
139147 Action : func (c * cli.Context ) error {
140- return printPeerId (a .api )
148+ return printPeerId (a .api , c . App . Writer )
141149 },
142150 },
143151 {
@@ -152,15 +160,15 @@ func (a *Application) init() {
152160 },
153161 Before : a .initApiConnection ,
154162 Action : func (c * cli.Context ) error {
155- return renameMe (a .api , c .String ("name" ))
163+ return renameMe (a .api , c .String ("name" ), c . App . Writer )
156164 },
157165 },
158166 {
159167 Name : "list_proxies" ,
160168 Usage : "Prints list of available SOCKS5 proxies" ,
161169 Before : a .initApiConnection ,
162170 Action : func (c * cli.Context ) error {
163- return listProxies (a .api )
171+ return listProxies (a .api , c . App . Writer )
164172 },
165173 },
166174 {
@@ -182,7 +190,7 @@ func (a *Application) init() {
182190 return a .initApiAndPeerId (c , false )
183191 },
184192 Action : func (c * cli.Context ) error {
185- return setProxy (a .api , c .String ("pid" ))
193+ return setProxy (a .api , c .String ("pid" ), c . App . Writer )
186194 },
187195 },
188196 },
@@ -207,15 +215,15 @@ func (a *Application) init() {
207215 },
208216 Before : a .initApiConnection ,
209217 Action : func (c * cli.Context ) error {
210- return printPeersStatus (a .api , c .String ("format" ))
218+ return printPeersStatus (a .api , c .String ("format" ), c . App . Writer )
211219 },
212220 },
213221 {
214222 Name : "requests" ,
215223 Usage : "Print all incoming friend requests" ,
216224 Before : a .initApiConnection ,
217225 Action : func (c * cli.Context ) error {
218- return printFriendRequests (a .api )
226+ return printFriendRequests (a .api , c . App . Writer )
219227 },
220228 },
221229 {
@@ -240,7 +248,7 @@ func (a *Application) init() {
240248 },
241249 Before : a .initApiConnection ,
242250 Action : func (c * cli.Context ) error {
243- return addPeer (a .api , c .String ("pid" ), c .String ("name" ), c .String ("ip" ))
251+ return addPeer (a .api , c .String ("pid" ), c .String ("name" ), c .String ("ip" ), c . App . Writer )
244252 },
245253 },
246254 {
@@ -260,7 +268,7 @@ func (a *Application) init() {
260268 },
261269 Before : a .initApiAndPeerIdRequired ,
262270 Action : func (c * cli.Context ) error {
263- return removePeer (a .api , c .String ("pid" ))
271+ return removePeer (a .api , c .String ("pid" ), c . App . Writer )
264272 },
265273 },
266274 {
@@ -285,7 +293,7 @@ func (a *Application) init() {
285293 },
286294 Before : a .initApiAndPeerIdRequired ,
287295 Action : func (c * cli.Context ) error {
288- return changePeerAlias (a .api , c .String ("pid" ), c .String ("new_name" ))
296+ return changePeerAlias (a .api , c .String ("pid" ), c .String ("new_name" ), c . App . Writer )
289297 },
290298 },
291299 {
@@ -310,7 +318,7 @@ func (a *Application) init() {
310318 },
311319 Before : a .initApiAndPeerIdRequired ,
312320 Action : func (c * cli.Context ) error {
313- return changePeerDomain (a .api , c .String ("pid" ), c .String ("domain" ))
321+ return changePeerDomain (a .api , c .String ("pid" ), c .String ("domain" ), c . App . Writer )
314322 },
315323 },
316324 {
@@ -335,7 +343,7 @@ func (a *Application) init() {
335343 },
336344 Before : a .initApiAndPeerIdRequired ,
337345 Action : func (c * cli.Context ) error {
338- return changePeerIP (a .api , c .String ("pid" ), c .String ("ip" ))
346+ return changePeerIP (a .api , c .String ("pid" ), c .String ("ip" ), c . App . Writer )
339347 },
340348 },
341349 {
@@ -360,7 +368,7 @@ func (a *Application) init() {
360368 },
361369 Before : a .initApiAndPeerIdRequired ,
362370 Action : func (c * cli.Context ) error {
363- return setAllowUsingAsExitNode (a .api , c .String ("pid" ), c .Bool ("allow" ))
371+ return setAllowUsingAsExitNode (a .api , c .String ("pid" ), c .Bool ("allow" ), c . App . Writer )
364372 },
365373 },
366374 },
@@ -388,7 +396,7 @@ func (a *Application) init() {
388396 if err != nil {
389397 return err
390398 }
391- fmt .Println ( logs )
399+ fmt .Fprintln ( c . App . Writer , logs )
392400
393401 return nil
394402 },
@@ -397,7 +405,7 @@ func (a *Application) init() {
397405 Name : "p2p_info" ,
398406 Usage : "Prints p2p debug info" ,
399407 Before : a .initApiConnection ,
400- Action : func (* cli.Context ) error {
408+ Action : func (c * cli.Context ) error {
401409 debugInfo , err := a .api .P2pDebugInfo ()
402410 if err != nil {
403411 return err
@@ -407,7 +415,7 @@ func (a *Application) init() {
407415 if err != nil {
408416 return err
409417 }
410- fmt .Println ( string (bytes ))
418+ fmt .Fprintln ( c . App . Writer , string (bytes ))
411419
412420 return nil
413421 },
0 commit comments