-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
add extension version #2754
add extension version #2754
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
|
||
"github.com/spf13/cobra" | ||
|
||
"go.k6.io/k6/js/modules" | ||
"go.k6.io/k6/lib/consts" | ||
) | ||
|
||
|
@@ -16,6 +17,9 @@ func getCmdVersion(globalState *globalState) *cobra.Command { | |
Long: `Show the application version and exit.`, | ||
Run: func(_ *cobra.Command, _ []string) { | ||
printToStdout(globalState, fmt.Sprintf("k6 v%s\n", consts.FullVersion())) | ||
for path, version := range modules.GetJSModuleVersions() { | ||
printToStdout(globalState, fmt.Sprintf("extension %s %s\n", path, version)) | ||
} | ||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. k6 supports both JS and output extensions. So we should make sure that both are shown in this output (same goes for the You can test by building a binary with an output extension, e.g.:
It should be a matter of doing something similar as in |
||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,8 @@ package modules | |||||
import ( | ||||||
"context" | ||||||
"fmt" | ||||||
"reflect" | ||||||
"runtime/debug" | ||||||
"strings" | ||||||
"sync" | ||||||
|
||||||
|
@@ -15,8 +17,9 @@ const extPrefix string = "k6/x/" | |||||
|
||||||
//nolint:gochecknoglobals | ||||||
var ( | ||||||
modules = make(map[string]interface{}) | ||||||
mx sync.RWMutex | ||||||
modules = make(map[string]interface{}) | ||||||
moduleVersions = make(map[string]string) | ||||||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to reuse the type module struct {
mod interface{}
version string
} This way This will complicate things slightly, as you'll need to change wherever There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good |
||||||
mx sync.RWMutex | ||||||
) | ||||||
|
||||||
// Register the given mod as an external JavaScript module that can be imported | ||||||
|
@@ -34,6 +37,38 @@ func Register(name string, mod interface{}) { | |||||
panic(fmt.Sprintf("module already registered: %s", name)) | ||||||
} | ||||||
modules[name] = mod | ||||||
getPackageVersion(mod) | ||||||
} | ||||||
|
||||||
func getPackageVersion(mod interface{}) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this is slightly clearer, as we are getting the Go module version after all (and the map is called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch. btw, If a package has two modules, it just has one package in map.
|
||||||
t := reflect.TypeOf(mod) | ||||||
p := t.PkgPath() | ||||||
if p == "" { | ||||||
if t.Kind() != reflect.Ptr { | ||||||
return | ||||||
} | ||||||
if t.Elem() != nil { | ||||||
p = t.Elem().PkgPath() | ||||||
} | ||||||
} | ||||||
buildInfo, ok := debug.ReadBuildInfo() | ||||||
if !ok { | ||||||
return | ||||||
} | ||||||
for _, dep := range buildInfo.Deps { | ||||||
packagePath := strings.TrimSpace(dep.Path) | ||||||
if strings.HasPrefix(p, packagePath) { | ||||||
if _, ok := moduleVersions[packagePath]; ok { | ||||||
return | ||||||
} | ||||||
if dep.Replace != nil { | ||||||
moduleVersions[packagePath] = dep.Replace.Version | ||||||
} else { | ||||||
moduleVersions[packagePath] = dep.Version | ||||||
} | ||||||
break | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// Module is the interface js modules should implement in order to get access to the VU | ||||||
|
@@ -56,6 +91,19 @@ func GetJSModules() map[string]interface{} { | |||||
return result | ||||||
} | ||||||
|
||||||
// GetJSModuleVersions returns a map of all registered js modules package and their versions | ||||||
func GetJSModuleVersions() map[string]string { | ||||||
mx.Lock() | ||||||
defer mx.Unlock() | ||||||
result := make(map[string]string, len(moduleVersions)) | ||||||
|
||||||
for name, version := range moduleVersions { | ||||||
result[name] = version | ||||||
} | ||||||
|
||||||
return result | ||||||
} | ||||||
|
||||||
// Instance is what a module needs to return | ||||||
type Instance interface { | ||||||
Exports() Exports | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this output needs to be a bit more visually pleasing. Here's what it currently looks like:

The extension information shouldn't be part of the banner. The banner itself is only reserved for... well, the k6.io banner 😄
Instead, if we do want to include this information as part of the
k6 run
output in addition to thek6 version
output (I do think it makes sense to have it in both places), then let's integrate it into the section below the banner, as suggested in #1741.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok