/
config.go
87 lines (70 loc) · 2.16 KB
/
config.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
Title string `yaml:"title"`
Copyright string `yaml:"copyright"`
Description string `yaml:"description"`
Reviewers []string `yaml:"reviewers"`
Search []string `yaml:"search"`
IncludeDevDependencies bool `yaml:"includeDevDependencies"`
Name string `yaml:"-"`
Path string `yaml:"-"`
GHToken string `yaml:"-"`
GoFiles []string `yaml:"-"`
JSFIles []string `yaml:"-"`
}
func (c *Config) NoticeDirPath() string {
return fmt.Sprintf("%s/.notice", c.Path)
}
func (c *Config) NoticeWorkPath() string {
return fmt.Sprintf("%s/.notice-work", c.Path)
}
func (c *Config) NoticeFilePath() string {
return fmt.Sprintf("%s/NOTICE.txt", c.Path)
}
func (c *Config) determineRepoFiles() {
for _, search := range c.Search {
if strings.Contains(search, "package.json") {
JSFile, _ := filepath.Abs(filepath.Join(c.Path, search))
c.JSFIles = append(c.JSFIles, JSFile)
}
if strings.Contains(search, "go.mod") {
goFile, _ := filepath.Abs(filepath.Join(c.Path, search))
c.GoFiles = append(c.GoFiles, goFile)
}
}
}
func newConfig() *Config {
repositoryPath := flag.String("p", ".", "Repository Path")
githubToken := flag.String("t", "", "Github Authentication Token")
configFilePath := flag.String("c", "", "Configuration File Path")
flag.Parse()
if len(*configFilePath) == 0 || len(*repositoryPath) == 0 {
fmt.Println("Usage: main.go -p path -t token -c configFile")
flag.PrintDefaults()
os.Exit(1)
}
content, err := os.ReadFile(*configFilePath)
if err != nil {
log.Fatalf("%s - Configuration file error! %v", *repositoryPath, err)
}
// Path always exist, no need to check error
repoFullPath, _ := filepath.Abs(*repositoryPath)
config := &Config{
Path: repoFullPath,
GHToken: *githubToken,
}
if err = yaml.Unmarshal(content, config); err != nil {
log.Fatalf("%s - Configuration file error! %v", *repositoryPath, err)
}
config.determineRepoFiles()
return config
}