Skip to content

Commit

Permalink
output binary in local build
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
  • Loading branch information
pablochacin committed Aug 27, 2024
1 parent 2bbdd5f commit 90c14f2
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions cmd/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"strings"

Expand All @@ -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 = `
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 90c14f2

Please # to comment.