Skip to content
Merged
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
75 changes: 36 additions & 39 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ var ActionsGroup = &cobra.Group{
}

type appImpl struct {
rootCmd *cobra.Command
streams cli.Streams
workDir string
cfgDir string
services map[ServiceInfo]Service
actionMngr action.Manager
pluginMngr PluginManager
config Config
mFS []ManagedFS
rootCmd *cobra.Command
streams cli.Streams
workDir string
cfgDir string
services map[ServiceInfo]Service
actionMngr action.Manager
pluginMngr PluginManager
config Config
mFS []ManagedFS
skipActions bool // skipActions to skip loading if not requested.
reqCmd string // reqCmd to search for the requested cobra command.
}

const versionFlag = "--version"

// getPluginByType returns specific plugins from the app.
func getPluginByType[T Plugin](app *appImpl) []T {
plugins := app.pluginMngr.All()
Expand Down Expand Up @@ -142,23 +142,30 @@ func (app *appImpl) init() error {
}
}

// Skip discover actions if we check version.
// Quick parse arguments to see if a version or help was requested.
args := os.Args[1:]
for i := 0; i < len(args); i++ {
if args[i] == versionFlag {
return nil
// Skip discover actions if we check version.
if args[i] == "--version" {
app.skipActions = true
}

if app.reqCmd == "" && !strings.HasPrefix(args[i], "-") {
app.reqCmd = args[i]
}
}

// Discover actions.
for _, p := range getPluginByType[ActionDiscoveryPlugin](app) {
for _, fs := range app.GetRegisteredFS() {
actions, err := p.DiscoverActions(fs)
if err != nil {
return err
}
for _, actConf := range actions {
app.actionMngr.Add(actConf)
if !app.skipActions {
for _, p := range getPluginByType[ActionDiscoveryPlugin](app) {
for _, fs := range app.GetRegisteredFS() {
actions, err := p.DiscoverActions(fs)
if err != nil {
return err
}
for _, actConf := range actions {
app.actionMngr.Add(actConf)
}
}
}
}
Expand All @@ -178,35 +185,25 @@ func (app *appImpl) exec() error {
return cmd.Help()
},
}
// Quick parse arguments to see if a version or help was requested.
args := os.Args[1:]
var skipActions bool // skipActions to skip loading if not requested.
var reqCmd string // reqCmd to search for the requested cobra command.
for i := 0; i < len(args); i++ {
if args[i] == versionFlag {
rootCmd.SetVersionTemplate(Version().Full())
skipActions = true
}
if reqCmd == "" && !strings.HasPrefix(args[i], "-") {
reqCmd = args[i]
}
}

if app.skipActions {
rootCmd.SetVersionTemplate(Version().Full())
}
// Convert actions to cobra commands.
actions := app.actionMngr.AllRef()
// Check the requested command to see what actions we must actually load.
if reqCmd != "" {
a, ok := actions[reqCmd]
if app.reqCmd != "" {
a, ok := actions[app.reqCmd]
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.
skipActions = true
app.skipActions = true
}
}
// @todo consider cobra completion and caching between runs.
if !skipActions {
if !app.skipActions {
if len(actions) > 0 {
rootCmd.AddGroup(ActionsGroup)
}
Expand Down