-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
init.go
66 lines (59 loc) · 1.63 KB
/
init.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
package initpolicy
import (
"fmt"
"github.com/aquaproj/aqua/v2/pkg/osfile"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/logrus-error/logerr"
)
const configTemplate = `---
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/policy.json
# aqua Policy
# https://aquaproj.github.io/
registries:
# Example
# - name: local
# type: local
# path: registry.yaml
# - name: aqua-registry
# type: github_content
# repo_owner: aquaproj
# repo_name: aqua-registry
# ref: semver(">= 3.0.0") # ref is optional
# path: registry.yaml
- type: standard
ref: semver(">= 3.0.0")
packages:
# Example
# - registry: local # allow all packages in the Registry
# - name: cli/cli # allow only a specific package. The default value of registry is "standard"
# - name: cli/cli
# version: semver(">= 2.0.0") # version is optional
- registry: standard
`
type Controller struct {
fs afero.Fs
}
func New(fs afero.Fs) *Controller {
return &Controller{
fs: fs,
}
}
func (c *Controller) Init(logE *logrus.Entry, cfgFilePath string) error {
if cfgFilePath == "" {
cfgFilePath = "aqua-policy.yaml"
}
if _, err := c.fs.Stat(cfgFilePath); err == nil {
// configuration file already exists, then do nothing.
logE.WithFields(logrus.Fields{
"policy_file_path": cfgFilePath,
}).Info("policy file already exists")
return nil
}
if err := afero.WriteFile(c.fs, cfgFilePath, []byte(configTemplate), osfile.FilePermission); err != nil {
return fmt.Errorf("write a policy file: %w", logerr.WithFields(err, logrus.Fields{
"policy_file_path": cfgFilePath,
}))
}
return nil
}