Skip to content

Commit a001631

Browse files
committed
pga-create: separate options for discover and repack subcommands so if the dump should be read from stdin you must use --stdin option
Signed-off-by: Manuel Carmona <[email protected]>
1 parent 3725c14 commit a001631

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

PublicGitArchive/pga-create/cmd/pga-create/discover.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ const (
2626
)
2727

2828
type discoverCommand struct {
29-
URL string `short:"l" long:"url" description:"Link to GHTorrent MySQL dump in tar.gz format. If empty (default), read from stdin if available or find the most recent dump at GHTORRENT_MYSQL ?= http://ghtorrent-downloads.ewi.tudelft.nl/mysql/."`
29+
URL string `short:"l" long:"url" description:"Link to GHTorrent MySQL dump in tar.gz format. If empty (default), it find the most recent dump at GHTORRENT_MYSQL ?= http://ghtorrent-downloads.ewi.tudelft.nl/mysql/."`
30+
Stdin bool `long:"stdin" description:"read GHTorrent MySQL dump from stdin"`
3031
Stars string `short:"s" long:"stars" default:"data/stars.gz" description:"Output path for the file with the numbers of stars per repository."`
3132
Languages string `short:"g" long:"languages" default:"data/languages.gz" description:"Output path for the gzipped file with the mapping between languages and repositories. May be empty - will be skipped then."`
3233
Repositories string `short:"r" long:"repositories" default:"data/repositories.gz" description:"Output path for the gzipped file with the repository names and identifiers."`
3334
}
3435

3536
func (c *discoverCommand) Execute(args []string) error {
36-
discoverRepos(discoveryParameters{
37+
discoverRepos(&discoveryParameters{
38+
Stdin: c.Stdin,
3739
URL: c.URL,
3840
StarsPath: c.Stars,
3941
LanguagesPath: c.Languages,
@@ -44,6 +46,7 @@ func (c *discoverCommand) Execute(args []string) error {
4446
}
4547

4648
type discoveryParameters struct {
49+
Stdin bool
4750
URL string
4851
StarsPath string
4952
LanguagesPath string
@@ -259,7 +262,7 @@ func findMostRecentMySQLDump(root string) string {
259262
return ghturl.ResolveReference(dumpurl).String()
260263
}
261264

262-
func discoverRepos(params discoveryParameters) {
265+
func discoverRepos(params *discoveryParameters) {
263266
startTime := time.Now()
264267

265268
for _, p := range []string{
@@ -279,7 +282,7 @@ func discoverRepos(params discoveryParameters) {
279282
spin.Start()
280283
defer spin.Stop()
281284

282-
inputFile := dumpReader(params.URL, spin)
285+
inputFile := dumpReader(params.Stdin, params.URL, spin)
283286
defer inputFile.Close()
284287

285288
var totalRead int64
@@ -347,8 +350,8 @@ func discoverRepos(params discoveryParameters) {
347350
humanize.Bytes(uint64(totalRead)), humanize.Bytes(uint64(processed)), time.Since(startTime))
348351
}
349352

350-
func dumpReader(url string, spin *spinner.Spinner) io.ReadCloser {
351-
if url == "" {
353+
func dumpReader(stdin bool, url string, spin *spinner.Spinner) io.ReadCloser {
354+
if stdin {
352355
fi, err := os.Stdin.Stat()
353356
if err != nil {
354357
fail("checking stat on stdin", err)
@@ -357,7 +360,9 @@ func dumpReader(url string, spin *spinner.Spinner) io.ReadCloser {
357360
if fi.Mode()&os.ModeNamedPipe != 0 {
358361
return os.Stdin
359362
}
363+
}
360364

365+
if url == "" {
361366
envURL := os.Getenv("GHTORRENT_MYSQL")
362367
if envURL == "" {
363368
envURL = defaultGhtorrentMySQL

PublicGitArchive/pga-create/cmd/pga-create/repack.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@ import (
1515
)
1616

1717
type repackCommand struct {
18-
URL string `short:"l" long:"url" description:"Link to GHTorrent MySQL dump in tar.gz format. If empty (default), read from stdin if possible or find the most recent dump at GHTORRENT_MYSQL ?= http://ghtorrent-downloads.ewi.tudelft.nl/mysql/."`
18+
URL string `short:"l" long:"url" description:"Link to GHTorrent MySQL dump in tar.gz format. If empty (default), it find the most recent dump at GHTORRENT_MYSQL ?= http://ghtorrent-downloads.ewi.tudelft.nl/mysql/."`
19+
Stdin bool `long:"stdin" description:"read GHTorrent MySQL dump from stdin"`
1920
Output string `short:"o" long:"output" required:"true" description:"output file"`
2021
}
2122

2223
func (c *repackCommand) Execute(args []string) error {
23-
repack(c.URL, c.Output)
24+
repack(c.Stdin, c.URL, c.Output)
2425

2526
return nil
2627
}
2728

28-
func repack(url, output string) {
29+
func repack(stdin bool, url, output string) {
2930
startTime := time.Now()
3031
spin := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
3132
spin.Start()
3233
defer spin.Stop()
3334

34-
inputFile := dumpReader(url, spin)
35+
inputFile := dumpReader(stdin, url, spin)
3536
var totalRead int64
3637
inputFile = trackingReader{RealReader: inputFile, Callback: func(n int) {
3738
totalRead += int64(n)

0 commit comments

Comments
 (0)