Skip to content

Instantly share code, notes, and snippets.

@nicolai86
Created October 17, 2020 03:36
Show Gist options
  • Save nicolai86/8cf415434c9661fad073e5ed2f643309 to your computer and use it in GitHub Desktop.
Save nicolai86/8cf415434c9661fad073e5ed2f643309 to your computer and use it in GitHub Desktop.
package builder
import (
"bytes"
"context"
"fmt"
"os/exec"
"path"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
)
type BuildConfig struct {
// BazelPath string "hcl:bazel_path,optional"
// Options []string "hcl:options,optional"
Target string "hcl:target"
}
type Builder struct {
config BuildConfig
}
// Implement Configurable
func (b *Builder) Config() interface{} {
return &b.config
}
// Implement ConfigurableNotify
func (b *Builder) ConfigSet(config interface{}) error {
c, ok := config.(*BuildConfig)
if !ok {
// The Waypoint SDK should ensure this never gets hit
return fmt.Errorf("Expected *BuildConfig as parameter")
}
// validate the config
if c.Target == "" {
return fmt.Errorf("bazel build target must be set to a valid target")
}
// if c.BazelPath != "" {
// info, err := os.Stat(c.BazelPath)
// if err != nil {
// return fmt.Errorf("bazel path doesn't exist: %w", err)
// }
// if info.Mode()&0100 == 0 {
// return fmt.Errorf("bazel is not executable")
// }
// }
return nil
}
// Implement Builder
func (b *Builder) BuildFunc() interface{} {
// return a function which will be called by Waypoint
return b.build
}
// A BuildFunc does not have a strict signature, you can define the parameters
// you need based on the Available parameters that the Waypoint SDK provides.
// Waypoint will automatically inject parameters as specified
// in the signature at run time.
//
// Available input parameters:
// - context.Context
// - *component.Source
// - *component.JobInfo
// - *component.DeploymentConfig
// - *datadir.Project
// - *datadir.App
// - *datadir.Component
// - hclog.Logger
// - terminal.UI
// - *component.LabelSet
//
// The output parameters for BuildFunc must be a Struct which can
// be serialzied to Protocol Buffers binary format and an error.
// This Output Value will be made available for other functions
// as an input parameter.
// If an error is returned, Waypoint stops the execution flow and
// returns an error to the user.
func (b *Builder) build(ctx context.Context, ui terminal.UI) (*Binary, error) {
u := ui.Status()
defer u.Close()
u.Update("Building application")
options := []string{"build"}
// options = append(options, b.config.Options...)
options = append(options, b.config.Target)
c := exec.Command("bazel", options...)
err := c.Run()
if err != nil {
u.Step(terminal.StatusError, "bazel build failed: %w")
return nil, err
}
c = exec.Command("bazel", "aquery", "--output", "jsonproto", b.config.Target)
buf := &bytes.Buffer{}
c.Stdout = buf
err = c.Run()
if err != nil {
u.Step(terminal.StatusError, "bazel aquery failed: %w")
return nil, err
}
fmt.Printf("%s\n", string(buf.Bytes()))
u.Step(terminal.StatusOK, "bazel built successful")
return &Binary{
Location: path.Join("/tmp", "todo"),
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment