/
file.go
178 lines (148 loc) · 3.49 KB
/
file.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package config
import (
"io/ioutil"
"strings"
"github.com/apex/log"
"github.com/urfave/cli"
yaml "gopkg.in/yaml.v2"
"github.com/fossas/fossa-cli/cmd/fossa/flags"
v1 "github.com/fossas/fossa-cli/config/file.v1"
"github.com/fossas/fossa-cli/errors"
"github.com/fossas/fossa-cli/files"
"github.com/fossas/fossa-cli/module"
)
// File defines the minimum interface for usage as a configuration file. We use
// an interface here in anticipation of new versions of configuration files,
// which will likely be implemented as different structs.
type File interface {
GetVersion() int
APIKey() string
Server() string
Title() string
Fetcher() string
Project() string
Branch() string
Revision() string
ProjectURL() string
JIRAProjectKey() string
Link() string
Team() string
Policy() string
Modules() []module.Module
}
type NoFile struct{}
func (_ NoFile) GetVersion() int {
return 0
}
func (_ NoFile) APIKey() string {
return ""
}
func (_ NoFile) Server() string {
return ""
}
func (_ NoFile) Title() string {
return ""
}
func (_ NoFile) Fetcher() string {
return ""
}
func (_ NoFile) Project() string {
return ""
}
func (_ NoFile) Branch() string {
return ""
}
func (_ NoFile) ProjectURL() string {
return ""
}
func (_ NoFile) JIRAProjectKey() string {
return ""
}
func (_ NoFile) Link() string {
return ""
}
func (_ NoFile) Team() string {
return ""
}
func (_ NoFile) Policy() string {
return ""
}
func (_ NoFile) Revision() string {
return ""
}
func (_ NoFile) Modules() []module.Module {
return []module.Module{}
}
// InitFile writes the current configuration to the current configuration file
// path.
func InitFile(modules []module.Module) File {
// Construct module configs.
var configs []v1.ModuleProperties
for _, m := range modules {
configs = append(configs, v1.ModuleProperties{
Name: m.Name,
Type: m.Type.String(),
BuildTarget: m.BuildTarget,
Path: m.Dir,
Options: m.Options,
Ignore: m.Ignore,
})
}
// Construct configuration file.
return v1.File{
Version: 2,
CLI: v1.CLIProperties{
Server: Endpoint(),
Fetcher: Fetcher(),
Project: Project(),
},
Analyze: v1.AnalyzeProperties{
Modules: configs,
},
}
}
// ReadFile reads the configuration file specified by CLI flags.
func ReadFile(c *cli.Context) (File, string, error) {
// Find a configuration file if one exists.
flag := ctx.String(flags.Config)
log.WithField("filename", flag).Debug("reading configuration file")
filename, err := TryFiles(flag, ".fossa.yml", ".fossa.yaml")
if err == ErrFileNotFound {
return NoFile{}, ".fossa.yml", nil
}
if err != nil {
return NoFile{}, ".fossa.yml", err
}
// Try to unmarshal the configuration file into a known config file version.
data, err := files.Read(filename)
if err != nil {
return NoFile{}, filename, err
}
file, err := v1.New(data)
if err != nil {
return NoFile{}, filename, err
}
return file, filename, err
}
func WriteFile(modules []module.Module) error {
file := InitFile(modules)
// Write file with header.
data, err := yaml.Marshal(file)
if err != nil {
return err
}
configHeader := []byte(
strings.Join([]string{
"# Generated by FOSSA CLI (https://github.com/fossas/fossa-cli)",
"# Visit https://fossa.com to learn more",
"",
"",
}, "\n"))
return ioutil.WriteFile(Filepath(), append(configHeader, data...), 0777)
}
func UpdateFile(modules []module.Module) error {
return errors.NotImplementedError()
}
func ExistsFile() (bool, error) {
return files.Exists(Filepath())
}