Skip to content

Commit c0df911

Browse files
committed
cli: add integration tests
1 parent 4cad502 commit c0df911

4 files changed

Lines changed: 436 additions & 49 deletions

File tree

cli/cli.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
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+
4957
func (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
},

cli/me.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cli
22

33
import (
44
"fmt"
5-
"os"
5+
"io"
66
"strings"
77
"time"
88

@@ -12,13 +12,13 @@ import (
1212
"github.com/anywherelan/awl/api/apiclient"
1313
)
1414

15-
func printStatus(api *apiclient.Client) error {
15+
func printStatus(api *apiclient.Client, w io.Writer) error {
1616
stats, err := api.PeerInfo()
1717
if err != nil {
1818
return err
1919
}
2020

21-
table := tablewriter.NewWriter(os.Stdout)
21+
table := tablewriter.NewWriter(w)
2222
table.AppendBulk([][]string{
2323
{"Download rate", fmt.Sprintf("%s (%s)", stats.NetworkStatsInIECUnits.RateIn, stats.NetworkStatsInIECUnits.TotalIn)},
2424
{"Upload rate", fmt.Sprintf("%s (%s)", stats.NetworkStatsInIECUnits.RateOut, stats.NetworkStatsInIECUnits.TotalOut)},
@@ -44,55 +44,55 @@ func formatWorkingStatus(working bool) string {
4444
return "not working"
4545
}
4646

47-
func printPeerId(api *apiclient.Client) error {
47+
func printPeerId(api *apiclient.Client, w io.Writer) error {
4848
info, err := api.PeerInfo()
4949
if err != nil {
5050
return err
5151
}
52-
fmt.Printf("your peer id: %s\n", info.PeerID)
52+
fmt.Fprintf(w, "your peer id: %s\n", info.PeerID)
5353

54-
qrterminal.GenerateHalfBlock(info.PeerID, qrterminal.M, os.Stdout)
54+
qrterminal.GenerateHalfBlock(info.PeerID, qrterminal.M, w)
5555

5656
return nil
5757
}
5858

59-
func renameMe(api *apiclient.Client, newName string) error {
59+
func renameMe(api *apiclient.Client, newName string, w io.Writer) error {
6060
err := api.UpdateMySettings(newName)
6161
if err != nil {
6262
return err
6363
}
6464

65-
fmt.Println("my peer name updated successfully")
65+
fmt.Fprintln(w, "my peer name updated successfully")
6666

6767
return nil
6868
}
6969

70-
func listProxies(api *apiclient.Client) error {
70+
func listProxies(api *apiclient.Client, w io.Writer) error {
7171
proxies, err := api.ListAvailableProxies()
7272
if err != nil {
7373
return err
7474
}
7575

7676
if len(proxies) == 0 {
77-
fmt.Println("no available proxies")
77+
fmt.Fprintln(w, "no available proxies")
7878
return nil
7979
}
8080

81-
fmt.Println("Proxies:")
81+
fmt.Fprintln(w, "Proxies:")
8282
for _, proxy := range proxies {
83-
fmt.Printf("- peer name: %s | peer id: %s\n", proxy.PeerName, proxy.PeerID)
83+
fmt.Fprintf(w, "- peer name: %s | peer id: %s\n", proxy.PeerName, proxy.PeerID)
8484
}
8585

8686
return nil
8787
}
8888

89-
func setProxy(api *apiclient.Client, peerID string) error {
89+
func setProxy(api *apiclient.Client, peerID string, w io.Writer) error {
9090
err := api.UpdateProxySettings(peerID)
9191
if err != nil {
9292
return err
9393
}
9494

95-
fmt.Println("proxy settings updated successfully")
95+
fmt.Fprintln(w, "proxy settings updated successfully")
9696

9797
return nil
9898
}

cli/peers.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cli
33
import (
44
"errors"
55
"fmt"
6-
"os"
6+
"io"
77
"strconv"
88
"strings"
99

@@ -14,7 +14,7 @@ import (
1414
"github.com/anywherelan/awl/entity"
1515
)
1616

17-
func printPeersStatus(api *apiclient.Client, format string) error {
17+
func printPeersStatus(api *apiclient.Client, format string, w io.Writer) error {
1818
const (
1919
TableFormatRowNumber = "n"
2020
TableFormatPeer = "p"
@@ -43,7 +43,7 @@ func printPeersStatus(api *apiclient.Client, format string) error {
4343
return fmt.Errorf("format flag is incorrect: format should contain at leest 1 char")
4444
}
4545

46-
table := tablewriter.NewWriter(os.Stdout)
46+
table := tablewriter.NewWriter(w)
4747

4848
headers := make([]string, 0, len(format))
4949
columns := make([]string, 0, len(format))
@@ -134,17 +134,17 @@ func printPeersStatus(api *apiclient.Client, format string) error {
134134
return nil
135135
}
136136

137-
func printFriendRequests(api *apiclient.Client) error {
137+
func printFriendRequests(api *apiclient.Client, w io.Writer) error {
138138
authRequests, err := api.AuthRequests()
139139
if err != nil {
140140
return err
141141
}
142142
if len(authRequests) == 0 {
143-
fmt.Println("you have no incoming requests")
143+
fmt.Fprintln(w, "you have no incoming requests")
144144
return nil
145145
}
146146
for _, req := range authRequests {
147-
fmt.Printf("Name: '%s' peerID: %s suggestedIP: %s\n", req.Name, req.PeerID, req.SuggestedIP)
147+
fmt.Fprintf(w, "Name: '%s' peerID: %s suggestedIP: %s\n", req.Name, req.PeerID, req.SuggestedIP)
148148
}
149149

150150
return nil
@@ -168,7 +168,7 @@ func getPeerIdByAlias(api *apiclient.Client, alias string) (string, error) {
168168
return "", fmt.Errorf("can't find peer with name \"%s\"", alias)
169169
}
170170

171-
func addPeer(api *apiclient.Client, peerID, alias, ipAddr string) error {
171+
func addPeer(api *apiclient.Client, peerID, alias, ipAddr string, w io.Writer) error {
172172
authRequests, err := api.AuthRequests()
173173
if err != nil {
174174
return err
@@ -186,29 +186,29 @@ func addPeer(api *apiclient.Client, peerID, alias, ipAddr string) error {
186186
return err
187187
}
188188

189-
fmt.Println("user added to friends list successfully")
189+
fmt.Fprintln(w, "user added to friends list successfully")
190190
return nil
191191
}
192192

193193
err = api.SendFriendRequest(peerID, alias, ipAddr)
194194
if err != nil {
195195
return err
196196
}
197-
fmt.Println("friend request sent successfully")
197+
fmt.Fprintln(w, "friend request sent successfully")
198198
return nil
199199
}
200200

201-
func removePeer(api *apiclient.Client, peerID string) error {
201+
func removePeer(api *apiclient.Client, peerID string, w io.Writer) error {
202202
err := api.RemovePeer(peerID)
203203
if err != nil {
204204
return err
205205
}
206206

207-
fmt.Println("peer removed successfully")
207+
fmt.Fprintln(w, "peer removed successfully")
208208
return nil
209209
}
210210

211-
func changePeerAlias(api *apiclient.Client, peerID, newAlias string) error {
211+
func changePeerAlias(api *apiclient.Client, peerID, newAlias string, w io.Writer) error {
212212
pcfg, err := api.KnownPeerConfig(peerID)
213213
if err != nil {
214214
return err
@@ -225,11 +225,11 @@ func changePeerAlias(api *apiclient.Client, peerID, newAlias string) error {
225225
return err
226226
}
227227

228-
fmt.Println("peer name updated successfully")
228+
fmt.Fprintln(w, "peer name updated successfully")
229229
return nil
230230
}
231231

232-
func changePeerDomain(api *apiclient.Client, peerID, newDomain string) error {
232+
func changePeerDomain(api *apiclient.Client, peerID, newDomain string, w io.Writer) error {
233233
pcfg, err := api.KnownPeerConfig(peerID)
234234
if err != nil {
235235
return err
@@ -246,11 +246,11 @@ func changePeerDomain(api *apiclient.Client, peerID, newDomain string) error {
246246
return err
247247
}
248248

249-
fmt.Println("peer domain name updated successfully")
249+
fmt.Fprintln(w, "peer domain name updated successfully")
250250
return nil
251251
}
252252

253-
func changePeerIP(api *apiclient.Client, peerID, newIP string) error {
253+
func changePeerIP(api *apiclient.Client, peerID, newIP string, w io.Writer) error {
254254
pcfg, err := api.KnownPeerConfig(peerID)
255255
if err != nil {
256256
return err
@@ -267,11 +267,11 @@ func changePeerIP(api *apiclient.Client, peerID, newIP string) error {
267267
return err
268268
}
269269

270-
fmt.Println("peer IP address updated successfully")
270+
fmt.Fprintln(w, "peer IP address updated successfully")
271271
return nil
272272
}
273273

274-
func setAllowUsingAsExitNode(api *apiclient.Client, peerID string, allow bool) error {
274+
func setAllowUsingAsExitNode(api *apiclient.Client, peerID string, allow bool, w io.Writer) error {
275275
pcfg, err := api.KnownPeerConfig(peerID)
276276
if err != nil {
277277
return err
@@ -288,6 +288,6 @@ func setAllowUsingAsExitNode(api *apiclient.Client, peerID string, allow bool) e
288288
return err
289289
}
290290

291-
fmt.Println("AllowUsingAsExitNode config updated successfully")
291+
fmt.Fprintln(w, "AllowUsingAsExitNode config updated successfully")
292292
return nil
293293
}

0 commit comments

Comments
 (0)