-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
36 lines (33 loc) · 972 Bytes
/
version.go
File metadata and controls
36 lines (33 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package clif
import (
"context"
)
// VersionMiddleware provides a middleware that, when any of the specified
// flags are passed, will print the version output and prevent any other
// [Handler] or [Middleware] from executing.
type VersionMiddleware struct {
Flags []string
}
func (middleware VersionMiddleware) Intercept(ctx context.Context, flags FlagSet, _ []string, defs DefParams, resp *Response) bool { //nolint:revive // that's the way interfaces go sometimes, this really needs 5 arguments
var showVersion bool
for _, flag := range middleware.Flags {
versionFlag, ok := flags[flag]
if !ok {
continue
}
err := versionFlag.As(ctx, &showVersion)
if err != nil {
fprintlnOrPanic(resp.Error, "Error checking if the", versionFlag, "flag is set:", err.Error())
resp.Code = 1
return false
}
if showVersion {
break
}
}
if !showVersion {
return true
}
fprintlnOrPanic(resp.Output, defs.App.Name, defs.App.Version)
return false
}