1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
// URLs implements a breadth-first search webcrawler based on the
// example given in section 8.6 of The Go Programming Language.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/urfave/cli/v3"
)
const shortcodeFilename = "urls.csv"
func main() {
if os.Getenv("GOEXPERIMENT") != "goroutineleakprofile" {
log.Fatal("Missing GOEXPERIMENT=goroutineleakprofile environment setting")
}
// Setting shorfile helps especially for when we log errors
// without returning them.
log.SetFlags(log.LstdFlags | log.Lshortfile)
cmd := &cli.Command{
Usage: "A configurable web crawler",
Commands: []*cli.Command{
{
Name: "get",
Usage: "Crawl the target",
MutuallyExclusiveFlags: []cli.MutuallyExclusiveFlags{
{
Required: true,
Flags: [][]cli.Flag{
{
&cli.StringFlag{
Name: "shortcode",
Usage: "Specify target using a shortcode",
},
},
{
&cli.StringFlag{
Name: "url",
Usage: "Specify target using a URL",
},
},
},
},
},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "concurrency",
Aliases: []string{"c"},
Usage: "Allowable number of concurrent URL fetches",
// Default concurrency setting is 1.
Value: 1,
},
&cli.IntFlag{
Name: "maxurls",
Aliases: []string{"m"},
Usage: "Maximum number of URLs to collect",
DefaultText: "no limit",
},
&cli.IntFlag{
Name: "depth",
Aliases: []string{"d"},
Usage: "Maximum URL depth",
DefaultText: "no limit",
},
},
Action: run,
},
{
Name: "shortcode",
Usage: "Configure shortcodes",
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("shortcode!")
return nil
},
},
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
fmt.Printf("\n%v\n", err)
os.Exit(1)
}
}
|