@@ -122,6 +122,9 @@ func init() {
122122 componentsCmd .AddCommand (componentsDevModeCmd )
123123
124124 // load components dynamically
125+ cli .PrototypeLoadComponents ()
126+
127+ // v1 components
125128 cli .LoadComponents ()
126129}
127130
@@ -158,9 +161,96 @@ func (c *cliState) IsComponentInstalled(name string) bool {
158161 return false
159162}
160163
164+ // Load v1 components
165+ func (c * cliState ) LoadComponents () {
166+ components , err := lwcomponent .LocalComponents ()
167+ if err != nil {
168+ c .Log .Debugw ("unable to load components" , "error" , err )
169+ return
170+ }
171+
172+ // @jon-stewart: TODO: load from cached API info
173+
174+ for _ , component := range components {
175+ exists := false
176+
177+ for _ , cmd := range rootCmd .Commands () {
178+ if cmd .Use == component .Name {
179+ exists = true
180+ break
181+ }
182+ }
183+
184+ // Skip components that were added by the prototype code
185+ if exists {
186+ continue
187+ }
188+
189+ version := component .InstalledVersion ()
190+ if version != nil && component .Exec .Executable () {
191+ componentCmd := & cobra.Command {
192+ Use : component .Name ,
193+ Short : component .Description ,
194+ Annotations : map [string ]string {"type" : componentTypeAnnotation },
195+ Version : version .String (),
196+ SilenceUsage : true ,
197+ DisableFlagParsing : true ,
198+ DisableFlagsInUseLine : true ,
199+ RunE : func (cmd * cobra.Command , args []string ) error {
200+ return v1ComponentCommand (c , cmd , args )
201+ },
202+ }
203+
204+ rootCmd .AddCommand (componentCmd )
205+ }
206+ }
207+ }
208+
209+ // Grpc server used for components to communicate back to the CLI
210+ func startGrpcServer (c * cliState ) {
211+ if err := c .Serve (); err != nil {
212+ c .Log .Errorw ("couldn't serve gRPC server" , "error" , err )
213+ }
214+ }
215+
216+ func v1ComponentCommand (c * cliState , cmd * cobra.Command , args []string ) error {
217+ // Parse component -v/--version flag
218+ versionVal , _ := cmd .Flags ().GetBool ("version" )
219+ if versionVal {
220+ cmd .Printf ("%s version %s\n " , cmd .Use , cmd .Version )
221+ return nil
222+ }
223+
224+ go startGrpcServer (c )
225+
226+ c .Log .Debugw ("running component" , "component" , cmd .Use ,
227+ "args" , c .componentParser .componentArgs ,
228+ "cli_flags" , c .componentParser .cliArgs )
229+
230+ catalog , err := lwcomponent .NewCatalog (cli .LwApi , lwcomponent .NewStageTarGz )
231+ if err != nil {
232+ return errors .Wrap (err , "unable to load component Catalog" )
233+ }
234+
235+ component , err := catalog .GetComponent (cmd .Use )
236+ if err != nil {
237+ return err
238+ }
239+
240+ // @jon-stewart: TODO: v1 dailyComponentUpdateAvailable
241+
242+ envs := []string {
243+ fmt .Sprintf ("LW_COMPONENT_NAME=%s" , cmd .Use ),
244+ }
245+
246+ envs = append (envs , c .envs ()... )
247+
248+ return component .Exec .ExecuteInline (c .componentParser .componentArgs , envs ... )
249+ }
250+
161251// LoadComponents reads the local components state and loads all installed components
162252// of type `CLI_COMMAND` dynamically into the root command of the CLI (`rootCmd`)
163- func (c * cliState ) LoadComponents () {
253+ func (c * cliState ) PrototypeLoadComponents () {
164254 c .Log .Debugw ("loading local components" )
165255 state , err := lwcomponent .LocalState ()
166256 if err != nil || state == nil {
@@ -421,14 +511,7 @@ func showComponent(args []string) error {
421511
422512 printComponent (component .PrintSummary ())
423513
424- version := component .InstalledVersion ()
425-
426- availableVersions , err := catalog .ListComponentVersions (component )
427- if err != nil {
428- return err
429- }
430-
431- printAvailableVersions (version , availableVersions )
514+ printAvailableVersions (component .InstalledVersion (), catalog .ListComponentVersions (component ))
432515
433516 return nil
434517}
0 commit comments