Skip to content

Commit 5354b7f

Browse files
committed
Support reading local JSON from stdin
Fixes #19
1 parent c9903d3 commit 5354b7f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ func main() {
131131
Usage: "Use an alternative server list from remote JSON file",
132132
},
133133
&cli.StringFlag{
134-
Name: defs.OptionLocalJSON,
135-
Usage: "Use an alternative server list from local JSON file",
134+
Name: defs.OptionLocalJSON,
135+
Usage: "Use an alternative server list from local JSON file,\n" +
136+
"\tor read from stdin with \"--" + defs.OptionLocalJSON + " -\".",
136137
},
137138
&cli.StringFlag{
138139
Name: defs.OptionSource,

speedtest/speedtest.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"io/ioutil"
1011
"net"
1112
"net/http"
13+
"os"
1214
"strings"
1315
"sync"
1416
"time"
@@ -211,9 +213,16 @@ func SpeedTest(c *cli.Context) error {
211213
var servers []defs.Server
212214
var err error
213215
if str := c.String(defs.OptionLocalJSON); str != "" {
214-
// load server list from local JSON file
215-
log.Infof("Using local JSON server list: %s", str)
216-
servers, err = getLocalServers(c.Bool(defs.OptionSecure), str, c.IntSlice(defs.OptionExclude), c.IntSlice(defs.OptionServer), !c.Bool(defs.OptionList))
216+
switch str {
217+
case "-":
218+
// load server list from stdin
219+
log.Info("Using local JSON server list from stdin")
220+
servers, err = getLocalServersReader(c.Bool(defs.OptionSecure), os.Stdin, c.IntSlice(defs.OptionExclude), c.IntSlice(defs.OptionServer), !c.Bool(defs.OptionList))
221+
default:
222+
// load server list from local JSON file
223+
log.Infof("Using local JSON server list: %s", str)
224+
servers, err = getLocalServers(c.Bool(defs.OptionSecure), str, c.IntSlice(defs.OptionExclude), c.IntSlice(defs.OptionServer), !c.Bool(defs.OptionList))
225+
}
217226
} else {
218227
// fetch the server list JSON and parse it into the `servers` array
219228
serverUrl := serverListUrl
@@ -370,10 +379,13 @@ func getServerList(forceHTTPS bool, serverList string, excludes, specific []int,
370379
return preprocessServers(servers, forceHTTPS, excludes, specific, filter)
371380
}
372381

373-
// getLocalServers loads the server JSON from a local file
374-
func getLocalServers(forceHTTPS bool, jsonFile string, excludes, specific []int, filter bool) ([]defs.Server, error) {
382+
// getLocalServersReader loads the server JSON from an io.Reader
383+
func getLocalServersReader(forceHTTPS bool, reader io.ReadCloser, excludes, specific []int, filter bool) ([]defs.Server, error) {
384+
defer reader.Close()
385+
375386
var servers []defs.Server
376-
b, err := ioutil.ReadFile(jsonFile)
387+
388+
b, err := ioutil.ReadAll(reader)
377389
if err != nil {
378390
return nil, err
379391
}
@@ -385,6 +397,15 @@ func getLocalServers(forceHTTPS bool, jsonFile string, excludes, specific []int,
385397
return preprocessServers(servers, forceHTTPS, excludes, specific, filter)
386398
}
387399

400+
// getLocalServers loads the server JSON from a local file
401+
func getLocalServers(forceHTTPS bool, jsonFile string, excludes, specific []int, filter bool) ([]defs.Server, error) {
402+
f, err := os.OpenFile(jsonFile, os.O_RDONLY, 0644)
403+
if err != nil {
404+
return nil, err
405+
}
406+
return getLocalServersReader(forceHTTPS, f, excludes, specific, filter)
407+
}
408+
388409
// preprocessServers makes some needed modifications to the servers fetched
389410
func preprocessServers(servers []defs.Server, forceHTTPS bool, excludes, specific []int, filter bool) ([]defs.Server, error) {
390411
for i := range servers {

0 commit comments

Comments
 (0)