Add urfave/cli app

This commit is contained in:
Richard Patel
2018-10-28 17:59:46 +01:00
parent b1c40767e0
commit bfd7302be8
3 changed files with 71 additions and 20 deletions

64
main.go
View File

@@ -2,27 +2,77 @@ package main
import (
"context"
"github.com/urfave/cli"
"net/url"
"os"
"strings"
"time"
)
var app = cli.App {
Name: "oddb-go",
Usage: "OD-Database Go crawler",
Version: "0.1",
BashComplete: cli.DefaultAppComplete,
Writer: os.Stdout,
Compiled: buildDate,
Commands: []cli.Command{
{
Name: "crawl",
Usage: "Crawl a list of URLs",
ArgsUsage: "[site, site, ...]",
Action: cmdCrawler,
},
},
}
func init() {
prepareConfig()
}
func main() {
app.Run(os.Args)
}
func cmdCrawler(clic *cli.Context) error {
readConfig()
if clic.NArg() == 0 {
cli.ShowCommandHelpAndExit(clic, "crawl", 1)
}
args := clic.Args()
remotes := make([]*OD, len(args))
for i, arg := range args {
// https://github.com/golang/go/issues/19779
if !strings.Contains(arg, "://") {
arg = "http://" + arg
}
u, err := url.Parse(arg)
if !strings.HasSuffix(u.Path, "/") {
u.Path += "/"
}
if err != nil { return err }
remotes[i] = &OD{ BaseUri: *u }
}
c := context.Background()
remotes := make(chan *OD)
go Schedule(c, remotes)
inRemotes := make(chan *OD)
go Schedule(c, inRemotes)
u, _ := url.Parse("http://mine.terorie.com:420/")
remote := NewRemoteDir(*u)
globalWait.Add(1)
remotes <- remote
for _, remote := range remotes {
globalWait.Add(1)
inRemotes <- remote
}
// Wait for all jobs to finish
globalWait.Wait()
return nil
}
var buildDate = time.Date(
2018, 10, 28,
17, 10, 0, 0,
time.UTC)