@@ -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
1618const (
@@ -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-
6330type 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
7139func (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
10371func (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
116124func (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 ("\n Termination signal received. Cleaning up..." )
132+ c .withTerm . Term () .Printfln ("\n Termination 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
163171func (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
0 commit comments