-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (87 loc) · 2.21 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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
)
func main() {
fmt.Printf("Comply © 2018-Present Lea Anthony. All rights reserved.\n\n")
// Get current working directory
cwd, err := os.Getwd()
CheckError(err)
// Check vendor exists and is a directory
vendorDir := filepath.Join(cwd, "vendor")
stat, err := os.Stat(vendorDir)
if err != nil {
Fatal("The vendor directory does not exist")
}
if !stat.IsDir() {
Fatal("vendor is not a directory")
}
// Calculate license dir
licenseDir := filepath.Join(cwd, "licenses")
// If dir already exists, remove it
err = os.RemoveAll(licenseDir)
CheckError(err)
// Find license files in vendor directory
licenseFiles := []string{}
filepath.Walk(vendorDir, func(path string, f os.FileInfo, _ error) error {
if !f.IsDir() {
match, err := regexp.MatchString("(?i)LICENSE*", f.Name())
if err == nil && match {
// Strip vendor dir
localDir := filepath.Dir(path)[len(vendorDir)+1:]
dirToCreate := filepath.Join(licenseDir, localDir)
err = os.MkdirAll(dirToCreate, 0755)
CheckError(err)
dstFile := filepath.Join(dirToCreate, f.Name())
err = Copy(path, dstFile)
CheckError(err)
licenseFiles = append(licenseFiles, filepath.Join(localDir, f.Name()))
}
}
return nil
})
if len(licenseFiles) > 0 {
fmt.Printf("Copied %d license file(s) into '%s':\n\n", len(licenseFiles), licenseDir)
for _, name := range licenseFiles {
fmt.Printf(" %s\n", name)
}
fmt.Println()
} else {
fmt.Printf("No license files found.\n\n")
}
fmt.Println("Thanks for using Comply! I hope it was useful for you.")
}
// Fatal prints the given error message then exists
func Fatal(msg string) {
fmt.Printf("Fatal: %s\n", msg)
os.Exit(1)
}
// CheckError handles the case when the given error is not null
func CheckError(err error) {
if err != nil {
Fatal(err.Error())
}
}
// Copy the src file to the dst file
// Credit: https://stackoverflow.com/a/21061062
func Copy(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}