Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to return early if the requested action is discovered. #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions plugins/actionscobra/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type Plugin struct {
app launchr.AppInternal
am action.Manager
pm launchr.PluginManager

// reqaid is a action id that was requested to run in cli.
reqaid string
}

// PluginInfo implements [launchr.Plugin] interface.
Expand All @@ -55,33 +58,41 @@ func (p *Plugin) discoverActions() (err error) {
if early.IsVersion || early.IsGen {
return err
}
var discovered []*action.Action
// @todo configure timeout from flags
// Define timeout for cases when we may traverse the whole FS, e.g. in / or home.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, p := range launchr.GetPluginByType[action.DiscoveryPlugin](p.pm) {
actions, errDis := p.V.DiscoverActions(ctx)

discoveryLoop:
for _, pldisc := range launchr.GetPluginByType[action.DiscoveryPlugin](p.pm) {
actions, errDis := pldisc.V.DiscoverActions(ctx)
if errDis != nil {
return errDis
}
discovered = append(discovered, actions...)

// Add discovered actions.
for _, a := range actions {
err = p.am.Add(a)
if err != nil {
launchr.Log().Warn("action was skipped due to error", "action_id", a.ID, "error", err)
launchr.Term().Warning().Printfln("Action %q was skipped:\n%v", a.ID, err)
continue
}
}

// Stop discovery if the requested command found.
// Check if an alias was provided to find the real action.
aid := p.am.GetIDFromAlias(early.Command)
if _, ok := p.am.Get(aid); ok {
p.reqaid = aid
break discoveryLoop
}
}
// Failed to discover actions in reasonable time.
if errCtx := ctx.Err(); errCtx != nil {
return errors.New(errDiscoveryTimeout)
}

// Add discovered actions.
for _, a := range discovered {
err = p.am.Add(a)
if err != nil {
launchr.Log().Warn("action was skipped due to error", "action_id", a.ID, "error", err)
launchr.Term().Warning().Printfln("Action %q was skipped:\n%v", a.ID, err)
continue
}
}

// Alter all registered actions.
for _, p := range launchr.GetPluginByType[action.AlterActionsPlugin](p.pm) {
err = p.V.AlterActions()
Expand All @@ -100,17 +111,16 @@ func (p *Plugin) CobraAddCommands(rootCmd *launchr.Command) error {
// Convert actions to cobra commands.
// Check the requested command to see what actions we must actually load.
var actions map[string]*action.Action
if early.Command != "" {
// Check if an alias was provided to find the real action.
early.Command = p.am.GetIDFromAlias(early.Command)
a, ok := p.am.Get(early.Command)
if ok {
// Use only the requested action.
actions = map[string]*action.Action{a.ID: a}
} else {
// Action was not requested, no need to load them.
return nil
if p.reqaid != "" {
// Use only the requested action.
a, ok := p.am.Get(p.reqaid)
if !ok {
panic("unexpected action id provided")
}
actions = map[string]*action.Action{p.reqaid: a}
} else if early.Command != "" {
// Action was not requested, no need to load them.
return nil
} else {
// Load all.
actions = p.am.All()
Expand Down
4 changes: 2 additions & 2 deletions plugins/verbosity/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func (p Plugin) OnAppInit(app launchr.App) error {
var logger *launchr.Logger
switch logFormat {
case LogFormatPlain:
logger = launchr.NewJSONHandlerLogger(out)
case LogFormatJSON:
logger = launchr.NewTextHandlerLogger(out)
case LogFormatJSON:
logger = launchr.NewJSONHandlerLogger(out)
default:
logger = launchr.NewConsoleLogger(out)
}
Expand Down
6 changes: 5 additions & 1 deletion plugins/yamldiscovery/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ type Plugin struct {
// PluginInfo implements [launchr.Plugin] interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{
Weight: math.MinInt,
// Discover yaml files in fs as late as possible
// to allow early return in discovery if the desired command is found.
// Set the weight less than actionscobra plugin to run OnAppInit earlier.
// @todo review after introducing dependency graph.
Weight: math.MaxInt - 10,
}
}

Expand Down