Skip to content

Commit 7f66682

Browse files
authored
Allow to return early if the requested action is discovered. (#86)
* Allow to return early if the requested action is discovered. * Fix logger format flags
1 parent e7e657b commit 7f66682

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

plugins/actionscobra/plugin.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

plugins/verbosity/plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ func (p Plugin) OnAppInit(app launchr.App) error {
100100
var logger *launchr.Logger
101101
switch logFormat {
102102
case LogFormatPlain:
103-
logger = launchr.NewJSONHandlerLogger(out)
104-
case LogFormatJSON:
105103
logger = launchr.NewTextHandlerLogger(out)
104+
case LogFormatJSON:
105+
logger = launchr.NewJSONHandlerLogger(out)
106106
default:
107107
logger = launchr.NewConsoleLogger(out)
108108
}

plugins/yamldiscovery/plugin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ type Plugin struct {
2323
// PluginInfo implements [launchr.Plugin] interface.
2424
func (p *Plugin) PluginInfo() launchr.PluginInfo {
2525
return launchr.PluginInfo{
26-
Weight: math.MinInt,
26+
// Discover yaml files in fs as late as possible
27+
// to allow early return in discovery if the desired command is found.
28+
// Set the weight less than actionscobra plugin to run OnAppInit earlier.
29+
// @todo review after introducing dependency graph.
30+
Weight: math.MaxInt - 10,
2731
}
2832
}
2933

0 commit comments

Comments
 (0)