@@ -30,6 +30,9 @@ type Plugin struct {
3030 app launchr.AppInternal
3131 am action.Manager
3232 pm launchr.PluginManager
33+
34+ // reqaid is a action id that was requested to run in cli.
35+ reqaid string
3336}
3437
3538// PluginInfo implements [launchr.Plugin] interface.
@@ -55,33 +58,41 @@ func (p *Plugin) discoverActions() (err error) {
5558 if early .IsVersion || early .IsGen {
5659 return err
5760 }
58- var discovered []* action.Action
5961 // @todo configure timeout from flags
6062 // Define timeout for cases when we may traverse the whole FS, e.g. in / or home.
6163 ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
6264 defer cancel ()
63- for _ , p := range launchr.GetPluginByType [action.DiscoveryPlugin ](p .pm ) {
64- actions , errDis := p .V .DiscoverActions (ctx )
65+
66+ discoveryLoop:
67+ for _ , pldisc := range launchr.GetPluginByType [action.DiscoveryPlugin ](p .pm ) {
68+ actions , errDis := pldisc .V .DiscoverActions (ctx )
6569 if errDis != nil {
6670 return errDis
6771 }
68- discovered = append (discovered , actions ... )
72+
73+ // Add discovered actions.
74+ for _ , a := range actions {
75+ err = p .am .Add (a )
76+ if err != nil {
77+ launchr .Log ().Warn ("action was skipped due to error" , "action_id" , a .ID , "error" , err )
78+ launchr .Term ().Warning ().Printfln ("Action %q was skipped:\n %v" , a .ID , err )
79+ continue
80+ }
81+ }
82+
83+ // Stop discovery if the requested command found.
84+ // Check if an alias was provided to find the real action.
85+ aid := p .am .GetIDFromAlias (early .Command )
86+ if _ , ok := p .am .Get (aid ); ok {
87+ p .reqaid = aid
88+ break discoveryLoop
89+ }
6990 }
7091 // Failed to discover actions in reasonable time.
7192 if errCtx := ctx .Err (); errCtx != nil {
7293 return errors .New (errDiscoveryTimeout )
7394 }
7495
75- // Add discovered actions.
76- for _ , a := range discovered {
77- err = p .am .Add (a )
78- if err != nil {
79- launchr .Log ().Warn ("action was skipped due to error" , "action_id" , a .ID , "error" , err )
80- launchr .Term ().Warning ().Printfln ("Action %q was skipped:\n %v" , a .ID , err )
81- continue
82- }
83- }
84-
8596 // Alter all registered actions.
8697 for _ , p := range launchr.GetPluginByType [action.AlterActionsPlugin ](p .pm ) {
8798 err = p .V .AlterActions ()
@@ -100,17 +111,16 @@ func (p *Plugin) CobraAddCommands(rootCmd *launchr.Command) error {
100111 // Convert actions to cobra commands.
101112 // Check the requested command to see what actions we must actually load.
102113 var actions map [string ]* action.Action
103- if early .Command != "" {
104- // Check if an alias was provided to find the real action.
105- early .Command = p .am .GetIDFromAlias (early .Command )
106- a , ok := p .am .Get (early .Command )
107- if ok {
108- // Use only the requested action.
109- actions = map [string ]* action.Action {a .ID : a }
110- } else {
111- // Action was not requested, no need to load them.
112- return nil
114+ if p .reqaid != "" {
115+ // Use only the requested action.
116+ a , ok := p .am .Get (p .reqaid )
117+ if ! ok {
118+ panic ("unexpected action id provided" )
113119 }
120+ actions = map [string ]* action.Action {p .reqaid : a }
121+ } else if early .Command != "" {
122+ // Action was not requested, no need to load them.
123+ return nil
114124 } else {
115125 // Load all.
116126 actions = p .am .All ()
0 commit comments