-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
105 lines (88 loc) · 2.28 KB
/
module.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
package setlicense
import (
"context"
_ "embed"
"fmt"
"github.com/getsyncer/syncer-core/syncer/planner"
"github.com/getsyncer/syncer-core/drift"
"github.com/getsyncer/syncer-core/fxregistry"
"github.com/getsyncer/syncer-core/config"
"github.com/cresta/zapctx"
"github.com/getsyncer/syncer-core/files"
"go.uber.org/fx"
)
func init() {
fxregistry.Register(Module)
}
//go:embed Apache-2.0.LICENSE
var apacheLicense string
type Config struct {
License string `yaml:"license"`
}
type Syncer struct {
logger *zapctx.Logger
}
func New(logger *zapctx.Logger) *Syncer {
return &Syncer{
logger: logger,
}
}
const Name = config.Name("setlicense")
func (l *Syncer) DetectDrift(ctx context.Context, runData *drift.RunData) (*files.System[*files.StateWithChangeReason], error) {
var ret files.System[*files.StateWithChangeReason]
var cfg Config
if err := runData.RunConfig.Decode(&cfg); err != nil {
return nil, err
}
if cfg.License == "" {
l.logger.Debug(ctx, "running license logic with no license set in config")
if err := ret.Add("LICENSE", &files.StateWithChangeReason{
State: files.State{
FileExistence: files.FileExistenceAbsent,
},
ChangeReason: &files.ChangeReason{
Reason: "Missing license",
},
}); err != nil {
return nil, fmt.Errorf("unable to add file for deletion %s: %w", "LICENSE", err)
}
return &ret, nil
}
licenseToText := map[string]string{
"Apache-2.0": apacheLicense,
}
licenseText, ok := licenseToText[cfg.License]
if !ok {
return nil, fmt.Errorf("unknown license %s", cfg.License)
}
if err := ret.Add("LICENSE", &files.StateWithChangeReason{
State: files.State{
Mode: 0644,
Contents: []byte(licenseText),
FileExistence: files.FileExistencePresent,
},
ChangeReason: &files.ChangeReason{
Reason: "license text",
},
}); err != nil {
return nil, fmt.Errorf("unable to add file %s: %w", "LICENSE", err)
}
return &ret, nil
}
func (l *Syncer) Name() config.Name {
return Name
}
func (l *Syncer) Priority() drift.Priority {
return drift.PriorityNormal
}
var _ drift.Detector = &Syncer{}
var Module = fx.Module("setlicense",
fx.Provide(
fx.Annotate(
New,
fx.As(new(drift.Detector)),
fx.ResultTags(`group:"syncers"`),
),
),
planner.FxOption(planner.WithFilesAllowedNoMagicString("LICENSE")),
)