Skip to content

Commit 91f99e8

Browse files
committed
update logs
1 parent 4d264b0 commit 91f99e8

File tree

9 files changed

+136
-102
lines changed

9 files changed

+136
-102
lines changed

compose/builder.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111

1212
"github.com/go-git/go-git/v5"
1313
"github.com/go-git/go-git/v5/plumbing/object"
14-
"github.com/launchrctl/launchr"
1514
"github.com/stevenle/topsort"
15+
16+
"github.com/launchrctl/launchr/pkg/action"
1617
)
1718

1819
const (
@@ -122,15 +123,15 @@ func identifyStrategy(name string) (mergeStrategyType, mergeStrategyTarget) {
122123

123124
// Builder struct, provides methods to merge packages into build
124125
type Builder struct {
126+
withLogger action.WithLogger
127+
withTerm action.WithTerm
128+
125129
platformDir string
126130
targetDir string
127131
sourceDir string
128132
skipNotVersioned bool
129133
logConflicts bool
130134
packages []*Package
131-
132-
log *launchr.Slog
133-
term *launchr.Terminal
134135
}
135136

136137
type fsEntry struct {
@@ -143,14 +144,14 @@ type fsEntry struct {
143144

144145
func createBuilder(c *Composer, targetDir, sourceDir string, packages []*Package) *Builder {
145146
return &Builder{
147+
c.withLogger,
148+
c.withTerm,
146149
c.pwd,
147150
targetDir,
148151
sourceDir,
149152
c.options.SkipNotVersioned,
150153
c.options.ConflictsVerbosity,
151154
packages,
152-
c.log,
153-
c.term,
154155
}
155156
}
156157

@@ -181,7 +182,7 @@ func getVersionedMap(gitDir string) (map[string]bool, error) {
181182
}
182183

183184
func (b *Builder) build(ctx context.Context) error {
184-
b.term.Println("Creating composition...")
185+
b.withTerm.Term().Println("Creating composition...")
185186
err := EnsureDirExists(b.targetDir)
186187
if err != nil {
187188
return err
@@ -257,7 +258,7 @@ func (b *Builder) build(ctx context.Context) error {
257258
targetsMap := getTargetsMap(b.packages)
258259

259260
if b.logConflicts {
260-
b.term.Info().Printf("Conflicting files:\n")
261+
b.withTerm.Term().Info().Printf("Conflicting files:\n")
261262
}
262263

263264
for i := 0; i < len(items); i++ {
@@ -349,7 +350,7 @@ func (b *Builder) logConflictResolve(resolveto mergeConflictResolve, path, pkgNa
349350
return
350351
}
351352

352-
b.term.Info().Printfln("[%s] - %s > Selected from %s", pkgName, path, entry.From)
353+
b.withTerm.Term().Info().Printfln("[%s] - %s > Selected from %s", pkgName, path, entry.From)
353354
}
354355

355356
func getTargetsMap(packages []*Package) map[string]string {

compose/compose.go

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"syscall"
1111

1212
"github.com/launchrctl/keyring"
13+
1314
"github.com/launchrctl/launchr"
15+
"github.com/launchrctl/launchr/pkg/action"
1416
)
1517

1618
const (
@@ -25,47 +27,13 @@ var (
2527
errComposeBadStructure = errors.New("incorrect mapping for plasma-compose.yaml, ensure structure is correct")
2628
)
2729

28-
// Composer stores compose definition
29-
type Composer struct {
30-
pwd string
31-
options *ComposerOptions
32-
compose *YamlCompose
33-
k keyring.Keyring
34-
log *launchr.Slog
35-
term *launchr.Terminal
36-
}
37-
38-
// ComposerOptions - list of possible composer options
39-
type ComposerOptions struct {
40-
Clean bool
41-
WorkingDir string
42-
SkipNotVersioned bool
43-
ConflictsVerbosity bool
44-
Interactive bool
45-
}
46-
47-
// CreateComposer instance
48-
func CreateComposer(pwd string, opts ComposerOptions, k keyring.Keyring, log *launchr.Slog, term *launchr.Terminal) (*Composer, error) {
49-
config, err := Lookup(os.DirFS(pwd))
50-
if err != nil {
51-
return nil, err
52-
}
53-
54-
for _, dep := range config.Dependencies {
55-
if dep.Source.Tag != "" {
56-
term.Warning().Printfln("found deprecated field `tag` in `%s` dependency. Use `ref` field for tags or branches.", dep.Name)
57-
}
58-
}
59-
60-
return &Composer{pwd, &opts, config, k, log, term}, nil
61-
}
62-
6330
type keyringWrapper struct {
31+
action.WithLogger
32+
action.WithTerm
33+
6434
keyringService keyring.Keyring
6535
interactive bool
6636
shouldUpdate bool
67-
log *launchr.Slog
68-
term *launchr.Terminal
6937
}
7038

7139
func (kw *keyringWrapper) getForURL(url string) (keyring.CredentialsItem, error) {
@@ -74,7 +42,7 @@ func (kw *keyringWrapper) getForURL(url string) (keyring.CredentialsItem, error)
7442
if errors.Is(errGet, keyring.ErrEmptyPass) {
7543
return ci, errGet
7644
} else if !errors.Is(errGet, keyring.ErrNotFound) {
77-
kw.log.Debug(errGet.Error())
45+
kw.WithLogger.Log().Debug(errGet.Error())
7846
return ci, errors.New("the keyring is malformed or wrong passphrase provided")
7947
}
8048

@@ -102,7 +70,7 @@ func (kw *keyringWrapper) getForURL(url string) (keyring.CredentialsItem, error)
10270

10371
func (kw *keyringWrapper) fillCredentials(ci keyring.CredentialsItem) (keyring.CredentialsItem, error) {
10472
if ci.URL != "" {
105-
kw.term.Printfln("Please add login and password for URL - %s", ci.URL)
73+
kw.WithTerm.Term().Printfln("Please add login and password for URL - %s", ci.URL)
10674
}
10775
err := keyring.RequestCredentialsFromTty(&ci)
10876
if err != nil {
@@ -112,6 +80,46 @@ func (kw *keyringWrapper) fillCredentials(ci keyring.CredentialsItem) (keyring.C
11280
return ci, nil
11381
}
11482

83+
// Composer stores compose definition
84+
type Composer struct {
85+
withLogger action.WithLogger
86+
withTerm action.WithTerm
87+
88+
pwd string
89+
options *ComposerOptions
90+
compose *YamlCompose
91+
k keyring.Keyring
92+
}
93+
94+
// ComposerOptions - list of possible composer options
95+
type ComposerOptions struct {
96+
Clean bool
97+
WorkingDir string
98+
SkipNotVersioned bool
99+
ConflictsVerbosity bool
100+
Interactive bool
101+
}
102+
103+
// CreateComposer instance
104+
func CreateComposer(pwd string, opts ComposerOptions, k keyring.Keyring) (*Composer, error) {
105+
config, err := Lookup(os.DirFS(pwd))
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
return &Composer{pwd: pwd, options: &opts, compose: config, k: k}, nil
111+
}
112+
113+
// SetLogger sets logger for composer.
114+
func (c *Composer) SetLogger(logger *launchr.Logger) {
115+
c.withLogger.SetLogger(logger)
116+
}
117+
118+
// SetTerm sets terminal for composer.
119+
func (c *Composer) SetTerm(term *launchr.Terminal) {
120+
c.withTerm.SetTerm(term)
121+
}
122+
115123
// RunInstall on Composer
116124
func (c *Composer) RunInstall() error {
117125
ctx, cancel := context.WithCancel(context.Background())
@@ -121,7 +129,7 @@ func (c *Composer) RunInstall() error {
121129

122130
go func() {
123131
<-signalChan
124-
c.term.Printfln("\nTermination signal received. Cleaning up...")
132+
c.withTerm.Term().Printfln("\nTermination signal received. Cleaning up...")
125133
// cleanup dir
126134
_, _, _ = c.prepareInstall(false)
127135

@@ -141,9 +149,9 @@ func (c *Composer) RunInstall() error {
141149
keyringService: c.getKeyring(),
142150
shouldUpdate: false,
143151
interactive: c.options.Interactive,
144-
log: c.log,
145-
term: c.term,
146152
}
153+
kw.WithLogger = c.withLogger
154+
kw.WithTerm = c.withTerm
147155
dm := CreateDownloadManager(kw)
148156
packages, err := dm.Download(ctx, c.getCompose(), packagesDir)
149157
if err != nil {
@@ -161,17 +169,23 @@ func (c *Composer) RunInstall() error {
161169
}
162170

163171
func (c *Composer) prepareInstall(clean bool) (string, string, error) {
172+
for _, dep := range c.compose.Dependencies {
173+
if dep.Source.Tag != "" {
174+
c.withTerm.Term().Warning().Printfln("found deprecated field `tag` in `%s` dependency. Use `ref` field for tags or branches.", dep.Name)
175+
}
176+
}
177+
164178
buildPath := c.getPath(BuildDir)
165179
packagesPath := c.getPath(c.options.WorkingDir)
166180

167-
c.term.Printfln("Cleaning build dir: %s", BuildDir)
181+
c.withTerm.Term().Printfln("Cleaning build dir: %s", BuildDir)
168182
err := os.RemoveAll(buildPath)
169183
if err != nil {
170184
return "", "", err
171185
}
172186

173187
if clean {
174-
c.term.Printfln("Cleaning packages dir: %s", packagesPath)
188+
c.withTerm.Term().Printfln("Cleaning packages dir: %s", packagesPath)
175189
err = os.RemoveAll(packagesPath)
176190
if err != nil {
177191
return "", "", err

compose/downloadManager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (m DownloadManager) downloadPackage(ctx context.Context, pkg *Package, targ
140140
if err != nil {
141141
errRemove := os.RemoveAll(downloadPath)
142142
if errRemove != nil {
143-
m.kw.log.Debug("error cleaning package folder", "path", downloadPath, "err", err)
143+
m.kw.WithLogger.Log().Debug("error cleaning package folder", "path", downloadPath, "err", err)
144144
}
145145
}
146146

compose/forms.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99

1010
"dario.cat/mergo"
1111
"github.com/charmbracelet/huh"
12-
"github.com/launchrctl/launchr"
12+
13+
"github.com/launchrctl/launchr/pkg/action"
1314
)
1415

1516
// RawStrategies represents collection of submitted flags for strategies.
@@ -18,8 +19,14 @@ type RawStrategies struct {
1819
Paths []string
1920
}
2021

22+
// FormsAction provides form operations with compose file.
23+
type FormsAction struct {
24+
action.WithLogger
25+
action.WithTerm
26+
}
27+
2128
// AddPackage adds a new package to plasma-compose.
22-
func AddPackage(doCreate bool, newDependency *Dependency, rawStrategies *RawStrategies, dir string, _ *launchr.Slog, term *launchr.Terminal) error {
29+
func (f *FormsAction) AddPackage(doCreate bool, newDependency *Dependency, rawStrategies *RawStrategies, dir string) error {
2330
config, err := Lookup(os.DirFS(dir))
2431
if err != nil {
2532
if !errors.Is(err, errComposeNotExists) {
@@ -55,7 +62,7 @@ func AddPackage(doCreate bool, newDependency *Dependency, rawStrategies *RawStra
5562
return err
5663
}
5764

58-
err = processStrategiesForm(newDependency)
65+
err = f.processStrategiesForm(newDependency)
5966
if err != nil {
6067
return err
6168
}
@@ -73,15 +80,15 @@ func AddPackage(doCreate bool, newDependency *Dependency, rawStrategies *RawStra
7380

7481
sanitizeDependency(newDependency)
7582
config.Dependencies = append(config.Dependencies, *newDependency)
76-
term.Println("Saving plasma-compose...")
83+
f.WithTerm.Term().Println("Saving plasma-compose...")
7784
sortPackages(config)
7885
err = writeComposeYaml(config)
7986

8087
return err
8188
}
8289

8390
// UpdatePackage updates a single package in plasma-compose.
84-
func UpdatePackage(dependency *Dependency, rawStrategies *RawStrategies, dir string, _ *launchr.Slog, term *launchr.Terminal) error {
91+
func (f *FormsAction) UpdatePackage(dependency *Dependency, rawStrategies *RawStrategies, dir string) error {
8592
config, err := Lookup(os.DirFS(dir))
8693
if err != nil {
8794
return err
@@ -114,15 +121,15 @@ func UpdatePackage(dependency *Dependency, rawStrategies *RawStrategies, dir str
114121
}
115122

116123
sanitizeDependency(toUpdate)
117-
term.Println("Saving plasma-compose...")
124+
f.WithTerm.Term().Println("Saving plasma-compose...")
118125
sortPackages(config)
119126
err = writeComposeYaml(config)
120127

121128
return err
122129
}
123130

124131
// UpdatePackages updates packages in plasma-compose in interactive way.
125-
func UpdatePackages(dir string, _ *launchr.Slog, term *launchr.Terminal) error {
132+
func (f *FormsAction) UpdatePackages(dir string) error {
126133
config, err := Lookup(os.DirFS(dir))
127134
if err != nil {
128135
return err
@@ -162,7 +169,7 @@ func UpdatePackages(dir string, _ *launchr.Slog, term *launchr.Terminal) error {
162169
return err
163170
}
164171

165-
err = processStrategiesForm(selectedDep)
172+
err = f.processStrategiesForm(selectedDep)
166173
if err != nil {
167174
return err
168175
}
@@ -178,7 +185,7 @@ func UpdatePackages(dir string, _ *launchr.Slog, term *launchr.Terminal) error {
178185
}
179186
}
180187

181-
term.Println("Saving plasma-compose...")
188+
f.WithTerm.Term().Println("Saving plasma-compose...")
182189
var newDeps []Dependency
183190
for _, dep := range packagesMap {
184191
newDeps = append(newDeps, *dep)
@@ -192,7 +199,7 @@ func UpdatePackages(dir string, _ *launchr.Slog, term *launchr.Terminal) error {
192199
}
193200

194201
// DeletePackages removes packages plasma-compose.
195-
func DeletePackages(packages []string, dir string, _ *launchr.Slog, term *launchr.Terminal) error {
202+
func (f *FormsAction) DeletePackages(packages []string, dir string) error {
196203
config, err := Lookup(os.DirFS(dir))
197204
if err != nil {
198205
return err
@@ -238,18 +245,18 @@ OUTER:
238245
}
239246

240247
if saveRequired {
241-
term.Println("Updating plasma-compose...")
248+
f.WithTerm.Term().Println("Updating plasma-compose...")
242249
config.Dependencies = dependencies
243250
sortPackages(config)
244251
err = writeComposeYaml(config)
245252
} else {
246-
term.Println("Nothing to update, quiting")
253+
f.WithTerm.Term().Println("Nothing to update, quiting")
247254
}
248255

249256
return err
250257
}
251258

252-
func processStrategiesForm(dependency *Dependency) error {
259+
func (f *FormsAction) processStrategiesForm(dependency *Dependency) error {
253260
var addStrategies bool
254261
err := huh.NewConfirm().
255262
Title("Would you like to add strategies?").

0 commit comments

Comments
 (0)