grim/local-pipelines

Start the conversion to golang
golang
2016-07-13, Gary Kramlich
409ee9cec40c
Parents e921b0627ab8
Children bc45c6cae46f
Start the conversion to golang
  • +26 -0
    main.go
  • +44 -0
    plan.go
  • +95 -0
    plan_test.go
  • --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/main.go Wed Jul 13 00:10:48 2016 -0500
    @@ -0,0 +1,26 @@
    +package main
    +
    +import (
    + "flag"
    + "fmt"
    + "os"
    +)
    +
    +func main() {
    + filename := flag.String("f", "", "The bitbucket-pipelines.yml to load")
    +
    + flag.Parse()
    +
    + if *filename == "" {
    + fmt.Printf("No filename specified\n")
    + os.Exit(1)
    + }
    +
    + p, err := loadFile(*filename)
    + if err != nil {
    + fmt.Printf("Error: %s\n", err.Error())
    + os.Exit(1)
    + }
    +
    + fmt.Printf("%+v\n", p)
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/plan.go Wed Jul 13 00:10:48 2016 -0500
    @@ -0,0 +1,44 @@
    +package main
    +
    +import (
    + "io/ioutil"
    +
    + "gopkg.in/yaml.v2"
    +)
    +
    +type Step struct {
    + Image string `yaml:"image"`
    + Script []string `yaml:"script"`
    +}
    +
    +type Pipelines struct {
    + Default []Step `yaml:"default"`
    + Branches map[string][]Step `yaml:"branches"`
    +}
    +
    +type Plan struct {
    + Image string `yaml:"image"`
    + Pipelines Pipelines `yaml:"pipelines"`
    +}
    +
    +func load(data []byte) (*Plan, error) {
    + p := new(Plan)
    +
    + err := yaml.Unmarshal(data, p)
    + if err != nil {
    + return nil, err
    + }
    +
    + return p, nil
    +}
    +
    +func loadFile(filename string) (*Plan, error) {
    + data, err := ioutil.ReadFile(filename)
    + if err != nil {
    + return nil, err
    + }
    +
    + p, err := load(data)
    +
    + return p, err
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/plan_test.go Wed Jul 13 00:10:48 2016 -0500
    @@ -0,0 +1,95 @@
    +package main
    +
    +import (
    + "fmt"
    + "testing"
    +
    + . "gopkg.in/check.v1"
    +)
    +
    +func TestPlan(t *testing.T) { TestingT(t) }
    +
    +type PlanSuite struct{}
    +
    +var _ = Suite(&PlanSuite{})
    +
    +func (s *PlanSuite) testPlan(c *C, inp string, exp *Plan) {
    + act, err := load([]byte(inp))
    +
    + fmt.Printf("act: %#v\n", act)
    + fmt.Printf("exp: %#v\n", exp)
    +
    + c.Assert(err, IsNil)
    + c.Assert(act, DeepEquals, exp)
    +}
    +
    +func (s *PlanSuite) TestPlanSimple(c *C) {
    + inp := `pipelines:
    + default:
    + - step:
    + script:
    + - echo "hello"`
    +
    + exp := &Plan{
    + Image: "",
    + Pipelines: Pipelines{
    + Default: []Step{
    + Step{
    + Script: []string{"echo \"hello\""},
    + },
    + },
    + },
    + }
    +
    + s.testPlan(c, inp, exp)
    +
    + c.Assert(false, Equals, true)
    +}
    +
    +func (s *PlanSuite) TestPlanComplex(c *C) {
    + inp := `image: debian:jessie
    +pipelines:
    + default:
    + - step:
    + image: pidgin/libpurple-2-x-y-dev:debian-stretch
    + script:
    + - apt-get update
    + - apt-get install -y libjson-glib-dev libprotobuf-c-dev protobuf-c-compiler
    + - make clean
    + - make -j $(grep processor /proc/cpuinfo | wc -l)
    + - step:
    + image: pidgin/libpurple-2-x-y-dev:debian-jessie
    + script:
    + - apt-get update
    + - apt-get install -y libjson-glib-dev libprotobuf-c-dev protobuf-c-compiler
    + - make clean
    + - make -j $(grep processor /proc/cpuinfo | wc -l)`
    +
    + exp := &Plan{
    + Image: "debian:jessie",
    + Pipelines: Pipelines{
    + Default: []Step{
    + Step{
    + Image: "pidgin/libpurple-2-x-y-dev:debian-stretch",
    + Script: []string{
    + "apt-get update",
    + "apt-get install -y libjson-glib-dev libprotobuf-c-dev protobuf-c-compiler",
    + "make clean",
    + "make -j $(grep processor /proc/cpuinfo | wc -l)",
    + },
    + },
    + Step{
    + Image: "pidgin/libpurple-2-x-y-dev:debian-jessie",
    + Script: []string{
    + "apt-get update",
    + "apt-get install -y libjson-glib-dev libprotobuf-c-dev protobuf-c-compiler",
    + "make clean",
    + "make -j $(grep processor /proc/cpuinfo | wc -l)",
    + },
    + },
    + },
    + },
    + }
    +
    + s.testPlan(c, inp, exp)
    +}