Skip to content

Commit

Permalink
implement command
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Jun 14, 2024
1 parent abfd436 commit 8af82a2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions catalog.json
Original file line number Diff line number Diff line change
@@ -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"]}
}
56 changes: 56 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
Expand All @@ -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(&registry, "registry", "r", "registry.json", "dependency catalog")
cmd.Flags().BoolVarP(&opts.Verbose, "verbose", "v", false, "print build output")

return cmd
}

0 comments on commit 8af82a2

Please sign in to comment.