-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
37 lines (29 loc) · 759 Bytes
/
input.go
File metadata and controls
37 lines (29 loc) · 759 Bytes
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
package main
import (
"flag"
"fmt"
"net/url"
)
type Input struct {
URL url.URL
SearchDepth int
}
// ReadInput reads and validates command line input for URL and search depth
func ReadInput() (*Input, error) {
// set depth flag
depth := flag.Int("n", 3, "Specifies the depth links will be crawled from the input URL.")
// read CLI input
flag.Parse()
// read URL argument
arg := flag.Arg(0)
url, err := url.Parse(arg)
if err != nil {
return nil, fmt.Errorf("unable to parse input to URL: %v", err)
}
// validate search depth
if *depth > 5 || *depth < 1 {
return nil, fmt.Errorf("invalid search depth provided (%d), must be between 1 and 5", *depth)
}
var input = &Input{URL: *url, SearchDepth: *depth}
return input, nil
}