From 8af82a2e0b7c1c3bb87b29c025ce129ac28a8d8f Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Fri, 14 Jun 2024 15:17:30 +0200 Subject: [PATCH] implement command Signed-off-by: Pablo Chacin --- catalog.json | 5 +++++ cmd/cmd.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 catalog.json diff --git a/catalog.json b/catalog.json new file mode 100644 index 0000000..3c9f1e5 --- /dev/null +++ b/catalog.json @@ -0,0 +1,5 @@ +{ + "k6": {"module": "go.k6.io/k6", "versions": ["v0.50.0", "v0.51.0"]}, + "k6/x/kubernetes": {"module": "github.com/grafana/xk6-kubernetes", "versions": ["v0.8.0"]}, + "k6/x/output-kafka": {"module": "github.com/grafana/xk6-output-kafka", "versions": ["v0.1.0"]} +} \ No newline at end of file diff --git a/cmd/cmd.go b/cmd/cmd.go index 3605494..b750be6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -2,8 +2,16 @@ package cmd import ( + "context" + "encoding/json" "errors" + "fmt" + "os" + "strings" + "github.com/grafana/k6build" + "github.com/grafana/k6catalog" + "github.com/grafana/k6foundry" "github.com/spf13/cobra" ) @@ -17,6 +25,14 @@ k6 build service returns artifacts that satisfies certain dependencies // New creates new cobra command for resolve command. func New() *cobra.Command { + var ( + deps []string + k6Constrains string + platform string + registry string + opts k6foundry.NativeBuilderOpts + ) + cmd := &cobra.Command{ Use: "k6build", Short: "k6 build service", @@ -26,9 +42,49 @@ func New() *cobra.Command { // this is needed to prevent cobra to print errors reported by subcommands in the stderr SilenceErrors: true, RunE: func(cmd *cobra.Command, _ []string) error { + catalog, err := k6catalog.NewCatalogFromJSON(registry) + if err != nil { + return fmt.Errorf("loading catalog %w", err) + } + + // This is required to pass environment variables like the github access credential + opts.CopyEnv = true + builder, err := k6foundry.NewNativeBuilder(context.TODO(), opts) + if err != nil { + return fmt.Errorf("loading catalog %w", err) + } + + cache, err := k6build.NewTempFileCache() + if err != nil { + return fmt.Errorf("creating build cache %w", err) + } + + srv := k6build.NewBuildService(catalog, builder, cache) + + buildDeps := []k6build.Dependency{} + for _, d := range deps { + name, constrains, _ := strings.Cut(d, ":") + buildDeps = append(buildDeps, k6build.Dependency{Name: name, Constraints: constrains}) + } + + artifact, err := srv.Build(context.TODO(), platform, k6Constrains, buildDeps) + if err != nil { + return fmt.Errorf("building %w", err) + } + + encoder := json.NewEncoder(os.Stdout) + encoder.SetIndent("", " ") + encoder.Encode(artifact) + return nil }, } + cmd.Flags().StringArrayVarP(&deps, "dependency", "d", nil, "list of dependencies in form package:constrains") + cmd.Flags().StringVarP(&k6Constrains, "k6-constrains", "k", "*", "k6 version constrains") + cmd.Flags().StringVarP(&platform, "platform", "p", "", "target platform") + cmd.Flags().StringVarP(®istry, "registry", "r", "registry.json", "dependency catalog") + cmd.Flags().BoolVarP(&opts.Verbose, "verbose", "v", false, "print build output") + return cmd }