-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (70 loc) · 1.35 KB
/
main.go
File metadata and controls
82 lines (70 loc) · 1.35 KB
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
package main
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
)
type Context struct {
Url url.URL
Dir string
Depth int
Cache map[string]struct{}
}
func main() {
args := os.Args[1:]
if len(args) < 2 {
fmt.Println("Usage: sourcerer [name] ...[urls]")
return
}
cwd, err := os.Getwd()
if err != nil {
Error(0, "Failed to get current path", err)
return
}
name := args[0]
dir := filepath.Join(cwd, name)
if err := os.Mkdir(dir, os.ModePerm); err != nil {
if !os.IsExist(err) {
Error(0, "Failed to create output directory", err)
return
}
children, err := os.ReadDir(dir)
if err == nil && len(children) > 0 {
Error(0, "Cannot write into existing non-empty directory")
return
}
}
for _, arg := range args[1:] {
if strings.HasPrefix(arg, "http") {
url, err := url.Parse(arg)
if err != nil {
fmt.Println("Error parsing url:", err)
return
}
ctx := Context{
Dir: dir,
Url: *url,
Cache: make(map[string]struct{}),
Depth: 0,
}
fromUrl(ctx)
} else if _, err := os.Stat(arg); err == nil {
url := url.URL{
Scheme: "file",
Host: "sourcerer",
Path: arg,
}
ctx := Context{
Dir: dir,
Url: url,
Cache: make(map[string]struct{}),
Depth: 0,
}
fromUrl(ctx)
} else {
Warn(0, "Unrecognized arg, expected URL or file", arg)
}
}
}