-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
121 lines (111 loc) · 2.78 KB
/
main.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/blackss2/utility/convert"
"github.com/blackss2/xlsx"
"go/parser"
"go/token"
"time"
"os"
"path/filepath"
"strings"
)
func main() {
list, err := getTotalImportList("./")
if err != nil {
panic(err)
}
excel := xlsx.NewFile()
sheetLicense, _ := excel.AddSheet("License")
sheetNotFound, _ := excel.AddSheet("Not Found")
goPath := os.Getenv("GOPATH")
for _, v := range list {
src := v[len(goPath)+5:]
url := fmt.Sprintf("https://libraries.io/go/%s", strings.Replace(src, `\`, "%2F", -1))
doc, err := goquery.NewDocument(url)
if err != nil {
println(err)
continue
}
src = strings.Replace(src, `\`, `/`, -1)
license := strings.TrimSpace(doc.Find(".col-md-8").Eq(1).Find("p").Eq(4).Text())
idx := strings.Index(license, "License: ")
if idx >= 0 {
r := sheetLicense.AddRow()
r.AddCell().SetString(src)
r.AddCell().SetString(license[idx+9:])
} else {
r := sheetNotFound.AddRow()
r.AddCell().SetString(src)
}
}
excel.Save(fmt.Sprintf("./License_%s.xlsx", strings.Replace(strings.Replace(strings.Replace(convert.String(time.Now())[:19], ":", "", -1), "-", "", -1), " ", "", -1)))
}
func getTotalImportList(path string) ([]string, error) {
importList, err := getImportList(path, nil)
if err != nil {
return nil, err
}
pathHash := make(map[string]bool)
for len(importList) > 0 {
subList := make([]string, 0)
for _, v := range importList {
list, err := getImportList(v, pathHash)
if err != nil {
return nil, err
}
if len(list) > 0 {
subList = append(subList, list...)
}
}
importList = subList
}
totalImportList := make([]string, 0)
for key, _ := range pathHash {
totalImportList = append(totalImportList, key)
}
convert.String(123)
return totalImportList, nil
}
func getImportList(path string, pathHash map[string]bool) ([]string, error) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return nil, err
}
importList := make([]string, 0)
isVisited := false
if pathHash != nil {
if _, has := pathHash[path]; has {
isVisited = true
}
}
if !isVisited {
if _, err := os.Stat(path); err == nil {
filepath.Walk(path, func(subpath string, finfo os.FileInfo, err error) error {
if !finfo.IsDir() {
ext := filepath.Ext(subpath)
if ext == ".go" {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, subpath, nil, 0)
if err != nil {
panic(err)
}
goPath := os.Getenv("GOPATH")
for _, v := range f.Imports {
Len := len(v.Path.Value)
src := fmt.Sprintf("%s/src/%s", goPath, v.Path.Value[1:Len-1])
importList = append(importList, src)
}
}
}
return nil
})
if pathHash != nil {
pathHash[path] = true
}
}
}
return importList, nil
}