Skip to content

Commit 069e944

Browse files
Base opts refactoring
1 parent 5ee3c15 commit 069e944

File tree

9 files changed

+161
-159
lines changed

9 files changed

+161
-159
lines changed

cmd/aem/crypto.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"github.com/spf13/cobra"
55
"github.com/wttech/aemc/pkg"
6-
"github.com/wttech/aemc/pkg/common"
76
"github.com/wttech/aemc/pkg/common/mapsx"
87
)
98

@@ -59,8 +58,9 @@ func (c *CLI) cryptoSetupCmd() *cobra.Command {
5958
}
6059
},
6160
}
62-
cmd.Flags().String("hmac-file", common.LibDir+"/crypto/data/hmac", "Path to file 'hmac'")
63-
cmd.Flags().String("master-file", common.LibDir+"/crypto/data/master", "Path to file 'master'")
61+
libDir := c.config.Values().GetString("base.lib_dir")
62+
cmd.Flags().String("hmac-file", libDir+"/crypto/data/hmac", "Path to file 'hmac'")
63+
cmd.Flags().String("master-file", libDir+"/crypto/data/master", "Path to file 'master'")
6464
return cmd
6565
}
6666

pkg/base/base.go pkg/base.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
package base
1+
package pkg
22

33
import (
4-
"github.com/wttech/aemc/pkg/cfg"
54
"github.com/wttech/aemc/pkg/common/pathx"
65
)
76

8-
type Opts struct {
9-
config *cfg.Config
7+
type BaseOpts struct {
8+
aem *AEM
109

1110
LibDir string
1211
TmpDir string
1312
ToolDir string
1413
CacheDir string
1514
}
1615

17-
func NewOpts(config *cfg.Config) *Opts {
18-
cv := config.Values()
16+
func NewBaseOpts(aem *AEM) *BaseOpts {
17+
cv := aem.config.Values()
1918

20-
return &Opts{
21-
config: config,
19+
return &BaseOpts{
20+
aem: aem,
2221

2322
LibDir: cv.GetString("base.lib_dir"),
2423
TmpDir: cv.GetString("base.tmp_dir"),
@@ -27,11 +26,7 @@ func NewOpts(config *cfg.Config) *Opts {
2726
}
2827
}
2928

30-
func (o *Opts) Config() *cfg.Config {
31-
return o.config
32-
}
33-
34-
func (o *Opts) PrepareWithChanged() (bool, error) {
29+
func (o *BaseOpts) PrepareWithChanged() (bool, error) {
3530
changed := false
3631
dirs := []string{o.LibDir, o.TmpDir, o.ToolDir, o.CacheDir}
3732
for _, dir := range dirs {

pkg/content/content.go pkg/content/editor.go

+17-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/samber/lo"
66
log "github.com/sirupsen/logrus"
77
"github.com/spf13/cast"
8-
"github.com/wttech/aemc/pkg/base"
8+
"github.com/wttech/aemc/pkg/cfg"
99
"github.com/wttech/aemc/pkg/common/pathx"
1010
"github.com/wttech/aemc/pkg/common/stringsx"
1111
"io/fs"
@@ -39,22 +39,18 @@ func init() {
3939
fileWithNamespacePatternRegex = regexp.MustCompile(FileWithNamespacePattern)
4040
}
4141

42-
type Manager struct {
43-
baseOpts *base.Opts
44-
42+
type Editor struct {
4543
FilesDeleted []PathRule
4644
FilesFlattened []string
4745
PropertiesSkipped []PathRule
4846
MixinTypesSkipped []PathRule
4947
NamespacesSkipped bool
5048
}
5149

52-
func NewManager(baseOpts *base.Opts) *Manager {
53-
cv := baseOpts.Config().Values()
54-
55-
return &Manager{
56-
baseOpts: baseOpts,
50+
func NewEditor(config *cfg.Config) *Editor {
51+
cv := config.Values()
5752

53+
return &Editor{
5854
FilesDeleted: determinePathRules(cv.Get("content.clean.files_deleted")),
5955
FilesFlattened: cv.GetStringSlice("content.clean.files_flattened"),
6056
PropertiesSkipped: determinePathRules(cv.Get("content.clean.properties_skipped")),
@@ -63,7 +59,7 @@ func NewManager(baseOpts *base.Opts) *Manager {
6359
}
6460
}
6561

66-
func (c Manager) Clean(path string) error {
62+
func (c Editor) Clean(path string) error {
6763
if pathx.IsDir(path) {
6864
log.Infof("cleaning directory '%s'", path)
6965
if err := c.cleanDotContents(path); err != nil {
@@ -107,13 +103,13 @@ func eachFiles(root string, processFileFunc func(string) error) error {
107103
})
108104
}
109105

110-
func (c Manager) cleanDotContents(root string) error {
106+
func (c Editor) cleanDotContents(root string) error {
111107
return eachFiles(root, func(path string) error {
112108
return c.cleanDotContentFile(path)
113109
})
114110
}
115111

116-
func (c Manager) cleanDotContentFile(path string) error {
112+
func (c Editor) cleanDotContentFile(path string) error {
117113
if !strings.HasSuffix(path, XmlFileSuffix) {
118114
return nil
119115
}
@@ -127,7 +123,7 @@ func (c Manager) cleanDotContentFile(path string) error {
127123
return writeLines(path, outputLines)
128124
}
129125

130-
func (c Manager) filterLines(path string, lines []string) []string {
126+
func (c Editor) filterLines(path string, lines []string) []string {
131127
var result []string
132128
for _, line := range lines {
133129
flag, processedLine := c.lineProcess(path, line)
@@ -148,7 +144,7 @@ func (c Manager) filterLines(path string, lines []string) []string {
148144
return c.cleanNamespaces(path, result)
149145
}
150146

151-
func (c Manager) cleanNamespaces(path string, lines []string) []string {
147+
func (c Editor) cleanNamespaces(path string, lines []string) []string {
152148
if !c.NamespacesSkipped {
153149
return lines
154150
}
@@ -183,7 +179,7 @@ func (c Manager) cleanNamespaces(path string, lines []string) []string {
183179
return result
184180
}
185181

186-
func (c Manager) lineProcess(path string, line string) (bool, string) {
182+
func (c Editor) lineProcess(path string, line string) (bool, string) {
187183
groups := propPatternRegex.FindStringSubmatch(line)
188184
if strings.TrimSpace(line) == "" {
189185
return true, ""
@@ -197,7 +193,7 @@ func (c Manager) lineProcess(path string, line string) (bool, string) {
197193
return false, line
198194
}
199195

200-
func (c Manager) normalizeMixins(path string, line string, propValue string, lineSuffix string) (bool, string) {
196+
func (c Editor) normalizeMixins(path string, line string, propValue string, lineSuffix string) (bool, string) {
201197
normalizedValue := strings.Trim(propValue, "[]")
202198
var resultValues []string
203199
for _, value := range strings.Split(normalizedValue, ",") {
@@ -211,13 +207,13 @@ func (c Manager) normalizeMixins(path string, line string, propValue string, lin
211207
return false, strings.ReplaceAll(line, normalizedValue, strings.Join(resultValues, ","))
212208
}
213209

214-
func (c Manager) flattenFiles(root string) error {
210+
func (c Editor) flattenFiles(root string) error {
215211
return eachFiles(root, func(path string) error {
216212
return c.flattenFile(path)
217213
})
218214
}
219215

220-
func (c Manager) flattenFile(path string) error {
216+
func (c Editor) flattenFile(path string) error {
221217
if !matchString(path, c.FilesFlattened) {
222218
return nil
223219
}
@@ -231,23 +227,23 @@ func (c Manager) flattenFile(path string) error {
231227
return os.Rename(path, dest)
232228
}
233229

234-
func (c Manager) deleteFiles(root string) error {
230+
func (c Editor) deleteFiles(root string) error {
235231
return eachFiles(root, func(path string) error {
236232
return c.DeleteFile(path, func() bool {
237233
return matchAnyRule(path, path, c.FilesDeleted)
238234
})
239235
})
240236
}
241237

242-
func (c Manager) DeleteDir(path string) error {
238+
func (c Editor) DeleteDir(path string) error {
243239
if !pathx.Exists(path) {
244240
return nil
245241
}
246242
log.Infof("deleting directory '%s'", path)
247243
return os.RemoveAll(path)
248244
}
249245

250-
func (c Manager) DeleteFile(path string, allowedFunc func() bool) error {
246+
func (c Editor) DeleteFile(path string, allowedFunc func() bool) error {
251247
if !pathx.Exists(path) || allowedFunc != nil && !allowedFunc() {
252248
return nil
253249
}

pkg/content_manager.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,18 @@ const (
1515
)
1616

1717
type ContentManager struct {
18-
aem *AEM
19-
contentManager *content.Manager
18+
aem *AEM
19+
editor *content.Editor
2020
}
2121

2222
func NewContentManager(aem *AEM) *ContentManager {
23-
return &ContentManager{
24-
aem: aem,
25-
contentManager: content.NewManager(aem.baseOpts),
26-
}
27-
}
28-
29-
func (cm *ContentManager) tmpDir() string {
30-
if cm.aem.Detached() {
31-
return os.TempDir()
32-
}
33-
return cm.aem.baseOpts.TmpDir
23+
result := &ContentManager{aem: aem}
24+
result.editor = content.NewEditor(result.aem.config)
25+
return result
3426
}
3527

3628
func (cm *ContentManager) Clean(path string) error {
37-
return cm.contentManager.Clean(path)
29+
return cm.editor.Clean(path)
3830
}
3931

4032
func (cm *ContentManager) Download(instance *Instance, localFile string, clean bool, opts PackageCreateOpts) error {
@@ -65,7 +57,7 @@ func (cm *ContentManager) PullDir(instance *Instance, dir string, clean bool, re
6557
return err
6658
}
6759
if replace {
68-
if err := cm.contentManager.DeleteDir(dir); err != nil {
60+
if err := cm.editor.DeleteDir(dir); err != nil {
6961
return err
7062
}
7163
}
@@ -89,7 +81,7 @@ func (cm *ContentManager) PullFile(instance *Instance, file string, clean bool,
8981
}
9082
syncFile := DetermineSyncFile(workDir, file)
9183
if file != syncFile || replace {
92-
if err := cm.contentManager.DeleteFile(file, nil); err != nil {
84+
if err := cm.editor.DeleteFile(file, nil); err != nil {
9385
return err
9486
}
9587
}
@@ -194,3 +186,10 @@ func (cm *ContentManager) pushContent(instances []Instance, pkgFile string) erro
194186
}
195187
return nil
196188
}
189+
190+
func (cm *ContentManager) tmpDir() string {
191+
if cm.aem.Detached() {
192+
return os.TempDir()
193+
}
194+
return cm.aem.baseOpts.TmpDir
195+
}

pkg/facade.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
package pkg
33

44
import (
5-
"github.com/wttech/aemc/pkg/base"
65
"github.com/wttech/aemc/pkg/cfg"
7-
"github.com/wttech/aemc/pkg/project"
86
"io"
97
"os"
108
"os/exec"
@@ -14,8 +12,8 @@ import (
1412
type AEM struct {
1513
output io.Writer
1614
config *cfg.Config
17-
project *project.Project
18-
baseOpts *base.Opts
15+
project *Project
16+
baseOpts *BaseOpts
1917

2018
vendorManager *VendorManager
2119
instanceManager *InstanceManager
@@ -30,8 +28,8 @@ func NewAEM(config *cfg.Config) *AEM {
3028
result := new(AEM)
3129
result.output = os.Stdout
3230
result.config = config
33-
result.project = project.New(result.config)
34-
result.baseOpts = base.NewOpts(result.config)
31+
result.project = NewProject(result)
32+
result.baseOpts = NewBaseOpts(result)
3533
result.vendorManager = NewVendorManager(result)
3634
result.instanceManager = NewInstanceManager(result)
3735
result.contentManager = NewContentManager(result)
@@ -55,7 +53,7 @@ func (a *AEM) Config() *cfg.Config {
5553
return a.config
5654
}
5755

58-
func (a *AEM) BaseOpts() *base.Opts {
56+
func (a *AEM) BaseOpts() *BaseOpts {
5957
return a.baseOpts
6058
}
6159

@@ -71,7 +69,7 @@ func (a *AEM) ContentManager() *ContentManager {
7169
return a.contentManager
7270
}
7371

74-
func (a *AEM) Project() *project.Project {
72+
func (a *AEM) Project() *Project {
7573
return a.project
7674
}
7775

0 commit comments

Comments
 (0)