@@ -2,13 +2,10 @@ package launchr
22
33import (
44 "errors"
5- "fmt"
65 "io"
76 "os"
8- "reflect"
97
108 "github.com/launchrctl/launchr/internal/launchr"
11- "github.com/launchrctl/launchr/pkg/action"
129 _ "github.com/launchrctl/launchr/plugins" // include default plugins
1310)
1411
@@ -20,11 +17,11 @@ type appImpl struct {
2017 // FS related.
2118 mFS []ManagedFS
2219 workDir string
23- cfgDir string
2420
2521 // Services.
2622 streams Streams
27- services map [ServiceInfo ]Service
23+ mask * SensitiveMask
24+ services * ServiceManager
2825 pluginMngr PluginManager
2926}
3027
@@ -40,46 +37,20 @@ func (app *appImpl) SetStreams(s Streams) { app.streams = s }
4037func (app * appImpl ) RegisterFS (fs ManagedFS ) { app .mFS = append (app .mFS , fs ) }
4138func (app * appImpl ) GetRegisteredFS () []ManagedFS { return app .mFS }
4239
43- func (app * appImpl ) SensitiveWriter (w io.Writer ) io.Writer {
44- return NewMaskingWriter (w , app .SensitiveMask ())
45- }
46- func (app * appImpl ) SensitiveMask () * SensitiveMask { return launchr .GlobalSensitiveMask () }
40+ func (app * appImpl ) SensitiveWriter (w io.Writer ) io.Writer { return app .SensitiveMask ().MaskWriter (w ) }
41+ func (app * appImpl ) SensitiveMask () * SensitiveMask { return app .mask }
4742
4843func (app * appImpl ) RootCmd () * Command { return app .cmd }
4944func (app * appImpl ) CmdEarlyParsed () launchr.CmdEarlyParsed { return app .earlyCmd }
5045
46+ func (app * appImpl ) Services () * ServiceManager { return app .services }
47+
5148func (app * appImpl ) AddService (s Service ) {
52- info := s .ServiceInfo ()
53- launchr .InitServiceInfo (& info , s )
54- if _ , ok := app .services [info ]; ok {
55- panic (fmt .Errorf ("service %s already exists, review your code" , info ))
56- }
57- app .services [info ] = s
49+ app .services .Add (s )
5850}
5951
6052func (app * appImpl ) GetService (v any ) {
61- // Check v is a pointer and implements [Service] to set a value later.
62- t := reflect .TypeOf (v )
63- isPtr := t != nil && t .Kind () == reflect .Pointer
64- var stype reflect.Type
65- if isPtr {
66- stype = t .Elem ()
67- }
68-
69- // v must be [Service] but can't equal it because all elements implement it
70- // and the first value will always be returned.
71- intService := reflect .TypeOf ((* Service )(nil )).Elem ()
72- if ! isPtr || ! stype .Implements (intService ) || stype == intService {
73- panic (fmt .Errorf ("argument must be a pointer to a type (interface) implementing Service, %q given" , t ))
74- }
75- for _ , srv := range app .services {
76- st := reflect .TypeOf (srv )
77- if st .AssignableTo (stype ) {
78- reflect .ValueOf (v ).Elem ().Set (reflect .ValueOf (srv ))
79- return
80- }
81- }
82- panic (fmt .Sprintf ("service %q does not exist" , stype ))
53+ app .services .Get (v )
8354}
8455
8556// init initializes application and plugins.
@@ -108,34 +79,25 @@ func (app *appImpl) init() error {
10879 }
10980 app .cmd .SetVersionTemplate (`{{ appVersionFull }}` )
11081 app .earlyCmd = launchr .EarlyPeekCommand ()
111- // Set io streams.
112- app .SetStreams (MaskedStdStreams (app .SensitiveMask ()))
113- app .cmd .SetIn (app .streams .In ().Reader ())
114- app .cmd .SetOut (app .streams .Out ())
115- app .cmd .SetErr (app .streams .Err ())
82+ app .mask = launchr .NewSensitiveMask ("****" )
11683
117- // Set working dir and config dir.
118- app .cfgDir = "." + name
119- app .workDir = launchr .MustAbs ("." )
120- actionsPath := launchr .MustAbs (EnvVarActionsPath .Get ())
121- // Initialize managed FS for action discovery.
84+ // Initialize managed FS for action discovery and set working dir
85+ app .workDir = MustAbs ("." )
12286 app .mFS = make ([]ManagedFS , 0 , 4 )
123- app .RegisterFS (action .NewDiscoveryFS (os .DirFS (actionsPath ), app .GetWD ()))
12487
12588 // Prepare dependencies.
126- app .services = make ( map [ ServiceInfo ] Service )
89+ app .services = launchr . NewServiceManager ( )
12790 app .pluginMngr = launchr .NewPluginManagerWithRegistered ()
128- // @todo consider home dir for global config.
129- config := launchr .ConfigFromFS (os .DirFS (app .cfgDir ))
130- actionMngr := action .NewManager (
131- action .WithDefaultRuntime (config ),
132- action .WithContainerRuntimeConfig (config , name + "_" ),
133- )
134-
135- // Register services for other modules.
136- app .AddService (actionMngr )
137- app .AddService (app .pluginMngr )
138- app .AddService (config )
91+
92+ // Register svcMngr for other modules.
93+ app .services .Add (app .mask )
94+ app .services .Add (app .pluginMngr )
95+
96+ // Set io streams.
97+ app .SetStreams (MaskedStdStreams (app .mask ))
98+ app .cmd .SetIn (app .streams .In ().Reader ())
99+ app .cmd .SetOut (app .streams .Out ())
100+ app .cmd .SetErr (app .streams .Err ())
139101
140102 Log ().Debug ("initialising application" )
141103
@@ -148,7 +110,7 @@ func (app *appImpl) init() error {
148110 return err
149111 }
150112 }
151- Log ().Debug ("init success" , "wd" , app .workDir , "actions_dir" , actionsPath )
113+ Log ().Debug ("init success" , "wd" , app .workDir )
152114
153115 return nil
154116}
0 commit comments