Skip to content

Commit

Permalink
add extension version
Browse files Browse the repository at this point in the history
  • Loading branch information
HarrisChu committed Nov 3, 2022
1 parent 11deb07 commit d4addec
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"gopkg.in/yaml.v3"

"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/output"
Expand Down Expand Up @@ -81,7 +82,13 @@ func getColor(noColor bool, attributes ...color.Attribute) *color.Color {

func getBanner(noColor bool) string {
c := getColor(noColor, color.FgCyan)
return c.Sprint(consts.Banner())
banner := make([]string, 0, len(modules.GetJSModuleVersions())+1)

banner = append(banner, consts.Banner())
for pkg, version := range modules.GetJSModuleVersions() {
banner = append(banner, fmt.Sprintf("extension %s %s", pkg, version))
}
return c.Sprint(strings.Join(banner, "\n"))
}

func printBanner(gs *globalState) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib/consts"
)

Expand All @@ -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))
}
},
}
}
52 changes: 50 additions & 2 deletions js/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package modules
import (
"context"
"fmt"
"reflect"
"runtime/debug"
"strings"
"sync"

Expand All @@ -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)
mx sync.RWMutex
)

// Register the given mod as an external JavaScript module that can be imported
Expand All @@ -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{}) {
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
Expand All @@ -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
Expand Down

0 comments on commit d4addec

Please sign in to comment.