Skip to content

Commit 6f16e5e

Browse files
authored
#76: refactor version check (#39)
Co-authored-by: Denis Neverov <dneverov@skilld.cloud>
1 parent fbb9efc commit 6f16e5e

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

app.go

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ var ActionsGroup = &cobra.Group{
2424
}
2525

2626
type appImpl struct {
27-
rootCmd *cobra.Command
28-
streams cli.Streams
29-
workDir string
30-
cfgDir string
31-
services map[ServiceInfo]Service
32-
actionMngr action.Manager
33-
pluginMngr PluginManager
34-
config Config
35-
mFS []ManagedFS
27+
rootCmd *cobra.Command
28+
streams cli.Streams
29+
workDir string
30+
cfgDir string
31+
services map[ServiceInfo]Service
32+
actionMngr action.Manager
33+
pluginMngr PluginManager
34+
config Config
35+
mFS []ManagedFS
36+
skipActions bool // skipActions to skip loading if not requested.
37+
reqCmd string // reqCmd to search for the requested cobra command.
3638
}
3739

38-
const versionFlag = "--version"
39-
4040
// getPluginByType returns specific plugins from the app.
4141
func getPluginByType[T Plugin](app *appImpl) []T {
4242
plugins := app.pluginMngr.All()
@@ -142,23 +142,30 @@ func (app *appImpl) init() error {
142142
}
143143
}
144144

145-
// Skip discover actions if we check version.
145+
// Quick parse arguments to see if a version or help was requested.
146146
args := os.Args[1:]
147147
for i := 0; i < len(args); i++ {
148-
if args[i] == versionFlag {
149-
return nil
148+
// Skip discover actions if we check version.
149+
if args[i] == "--version" {
150+
app.skipActions = true
151+
}
152+
153+
if app.reqCmd == "" && !strings.HasPrefix(args[i], "-") {
154+
app.reqCmd = args[i]
150155
}
151156
}
152157

153158
// Discover actions.
154-
for _, p := range getPluginByType[ActionDiscoveryPlugin](app) {
155-
for _, fs := range app.GetRegisteredFS() {
156-
actions, err := p.DiscoverActions(fs)
157-
if err != nil {
158-
return err
159-
}
160-
for _, actConf := range actions {
161-
app.actionMngr.Add(actConf)
159+
if !app.skipActions {
160+
for _, p := range getPluginByType[ActionDiscoveryPlugin](app) {
161+
for _, fs := range app.GetRegisteredFS() {
162+
actions, err := p.DiscoverActions(fs)
163+
if err != nil {
164+
return err
165+
}
166+
for _, actConf := range actions {
167+
app.actionMngr.Add(actConf)
168+
}
162169
}
163170
}
164171
}
@@ -178,35 +185,25 @@ func (app *appImpl) exec() error {
178185
return cmd.Help()
179186
},
180187
}
181-
// Quick parse arguments to see if a version or help was requested.
182-
args := os.Args[1:]
183-
var skipActions bool // skipActions to skip loading if not requested.
184-
var reqCmd string // reqCmd to search for the requested cobra command.
185-
for i := 0; i < len(args); i++ {
186-
if args[i] == versionFlag {
187-
rootCmd.SetVersionTemplate(Version().Full())
188-
skipActions = true
189-
}
190-
if reqCmd == "" && !strings.HasPrefix(args[i], "-") {
191-
reqCmd = args[i]
192-
}
193-
}
194188

189+
if app.skipActions {
190+
rootCmd.SetVersionTemplate(Version().Full())
191+
}
195192
// Convert actions to cobra commands.
196193
actions := app.actionMngr.AllRef()
197194
// Check the requested command to see what actions we must actually load.
198-
if reqCmd != "" {
199-
a, ok := actions[reqCmd]
195+
if app.reqCmd != "" {
196+
a, ok := actions[app.reqCmd]
200197
if ok {
201198
// Use only the requested action.
202199
actions = map[string]*action.Action{a.ID: a}
203200
} else {
204201
// Action was not requested, no need to load them.
205-
skipActions = true
202+
app.skipActions = true
206203
}
207204
}
208205
// @todo consider cobra completion and caching between runs.
209-
if !skipActions {
206+
if !app.skipActions {
210207
if len(actions) > 0 {
211208
rootCmd.AddGroup(ActionsGroup)
212209
}

0 commit comments

Comments
 (0)