From 90c14f2fd654340ae770929b1d86ab2c3c05c182 Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Tue, 27 Aug 2024 20:16:50 +0200 Subject: [PATCH] output binary in local build Signed-off-by: Pablo Chacin --- cmd/local/local.go | 47 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/cmd/local/local.go b/cmd/local/local.go index 3602e7e..a30d9a0 100644 --- a/cmd/local/local.go +++ b/cmd/local/local.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "fmt" + "io" + "net/url" "os" "strings" @@ -18,7 +20,8 @@ var ErrTargetPlatformUndefined = errors.New("target platform is required") //nol const ( long = ` -k6build local builder returns artifacts that satisfies certain dependencies +k6build local builder creates a custom k6 binary artifacts that satisfies certain +dependencies. Requires the golang toolchain and git. ` example = ` @@ -43,17 +46,19 @@ k6build local -k v0.50.0 -e GOPROXY=http://localhost:80 ) // New creates new cobra command for local build command. -func New() *cobra.Command { +func New() *cobra.Command { //nolint:funlen var ( + config local.BuildServiceConfig deps []string k6 string + output string platform string - config local.BuildServiceConfig + quiet bool ) cmd := &cobra.Command{ Use: "local", - Short: "build using a local builder", + Short: "build custom k6 binary locally", Long: long, Example: example, // prevent the usage help to printed to stderr when an error is reported by a subcommand @@ -80,11 +85,35 @@ func New() *cobra.Command { return fmt.Errorf("building %w", err) } - encoder := json.NewEncoder(os.Stdout) - encoder.SetIndent("", " ") - err = encoder.Encode(artifact) + if !quiet { + encoder := json.NewEncoder(os.Stdout) + encoder.SetIndent("", " ") + err = encoder.Encode(artifact) + if err != nil { + return fmt.Errorf("processing object %w", err) + } + } + + binaryURL, err := url.Parse(artifact.URL) + if err != nil { + return fmt.Errorf("malformed URL %w", err) + } + artifactBinary, err := os.Open(binaryURL.Path) + if err != nil { + return fmt.Errorf("opening output file %w", err) + } + defer func() { + _ = artifactBinary.Close() + }() + + binary, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE, 0o755) //nolint:gosec + if err != nil { + return fmt.Errorf("opening output file %w", err) + } + + _, err = io.Copy(binary, artifactBinary) if err != nil { - return fmt.Errorf("processing object %w", err) + return fmt.Errorf("copying artifact %w", err) } return nil @@ -100,6 +129,8 @@ func New() *cobra.Command { cmd.Flags().BoolVarP(&config.Verbose, "verbose", "v", false, "print build process output") cmd.Flags().BoolVarP(&config.CopyGoEnv, "copy-go-env", "g", true, "copy go environment") cmd.Flags().StringToStringVarP(&config.BuildEnv, "env", "e", nil, "build environment variables") + cmd.Flags().StringVarP(&output, "output", "o", "k6", "path to put the binary as an executable.") + cmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "don't print artifact's details") return cmd }