Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bazel, add travis #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ vendor/

*.iml
.idea/

# Bazel
bazel-bin
bazel-genfiles
bazel-kexpand
bazel-out
bazel-testlogs


15 changes: 0 additions & 15 deletions .gitmodules

This file was deleted.

12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: go
os: linux
dist: trusty

sudo: required

before_script:
- wget "http://storage.googleapis.com/bazel-apt/pool/jdk1.8/b/bazel/bazel_0.4.0_amd64.deb"
- sudo dpkg --force-all -i bazel_0.4.0_amd64.deb

script:
- make ci
8 changes: 8 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load("@io_bazel_rules_go//go:def.bzl", "go_prefix")

go_prefix("github.com/kopeio/kexpand")

39 changes: 27 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# TODO: Move entirely to bazel?
.PHONY: images

all: gocode

DOCKER_REGISTRY=kopeio
Expand All @@ -6,24 +9,36 @@ ifndef VERSION
endif

gocode:
GO15VENDOREXPERIMENT=1 go install -ldflags "-X main.BuildVersion=${VERSION}" github.com/kopeio/kexpand
GO15VENDOREXPERIMENT=1 go install -ldflags "-X main.BuildVersion=${VERSION}" github.com/kopeio/kexpand/cmd/...

gofmt:
gofmt -w -s main.go
gofmt -w -s cmd

syncdeps:
rsync -avz _vendor/ vendor/

builder-image:
docker build -f images/kexpand-builder/Dockerfile -t kexpand-builder .

build-in-docker: builder-image
docker run -it -v `pwd`:/src kexpand-builder /onbuild.sh

image: build-in-docker
docker build -t ${DOCKER_REGISTRY}/kexpand -f images/kexpand/Dockerfile .
# --------------------------------------------------
# Docker images

push: image
push: images
docker push ${DOCKER_REGISTRY}/kexpand

syncdeps:
rsync -avz _vendor/ vendor/
images:
bazel run //images:kexpand ${DOCKER_REGISTRY}/kexpand


# --------------------------------------------------
# Continuous integration targets

ci: images test govet
echo "Done"

govet:
go vet \
github.com/kopeio/kexpand/cmd/...

test:
bazel test //...
#go test github.com/kopeio/kexpand/cmd/...

61 changes: 61 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
git_repository(
name = "io_bazel_rules_go",
remote = "https://github.com/bazelbuild/rules_go.git",
tag = "0.3.0",
)

load("@io_bazel_rules_go//go:def.bzl", "go_repositories", "new_go_repository")

go_repositories()

# for building docker base images
debs = (
(
"busybox_deb",
"51651980a993b02c8dc663a5539a4d83704e56c2fed93dd8d1b2580e61319af5",
"http://ftp.us.debian.org/debian/pool/main/b/busybox/busybox-static_1.22.0-19_amd64.deb",
),
(
"libc_deb",
"ee4d9dea08728e2c2bbf43d819c3c7e61798245fab4b983ae910865980f791ad",
"http://ftp.us.debian.org/debian/pool/main/g/glibc/libc6_2.19-18+deb8u6_amd64.deb",
)
)

[http_file(
name = name,
sha256 = sha256,
url = url,
) for name, sha256, url in debs]


new_go_repository(
name = "com_github_golang_glog",
importpath = "github.com/golang/glog",
commit = "23def4e6c14b4da8ac2ed8007337bc5eb5007998",
)

new_go_repository(
name = "com_github_spf13_cobra",
importpath = "github.com/spf13/cobra",
commit = "dbb7c2d02e284aa24423e3e6e105969dffe6e4d6",
)

new_go_repository(
name = "com_github_spf13_pflag",
importpath = "github.com/spf13/pflag",
commit = "1560c1005499d61b80f865c04d39ca7505bf7f0b",
)

new_go_repository(
name = "com_github_ghodss_yaml",
importpath = "github.com/ghodss/yaml",
commit = "bea76d6a4713e18b7f5321a2b020738552def3ea",
)


new_go_repository(
name = "in_gopkg_yaml_v2",
importpath = "gopkg.in/yaml.v2",
commit = "a5b47d31c556af34a302ce5d659e6fea44d90de0",
)
1 change: 0 additions & 1 deletion _vendor/github.com/cloudfoundry-incubator/candiedyaml
Submodule candiedyaml deleted from 99c3df
1 change: 0 additions & 1 deletion _vendor/github.com/ghodss/yaml
Submodule yaml deleted from aa0c86
1 change: 0 additions & 1 deletion _vendor/github.com/golang/glog
Submodule glog deleted from 23def4
1 change: 0 additions & 1 deletion _vendor/github.com/spf13/cobra
Submodule cobra deleted from dbb7c2
1 change: 0 additions & 1 deletion _vendor/github.com/spf13/pflag
Submodule pflag deleted from 1560c1
21 changes: 21 additions & 0 deletions cmd/kexpand/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_test",
)

go_binary(
name = "kexpand",
srcs = ["expand.go", "main.go", "root.go", "version.go"],
deps = [
"@com_github_golang_glog//:go_default_library",
"@com_github_ghodss_yaml//:go_default_library",
"@com_github_spf13_pflag//:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
"//pkg/expand:go_default_library",
],
)
65 changes: 8 additions & 57 deletions cmd/expand.go → cmd/kexpand/expand.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package cmd
package main

import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"

"encoding/base64"
"github.com/ghodss/yaml"
"github.com/golang/glog"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"strings"
"github.com/kopeio/kexpand/pkg/expand"
)

type ExpandCmd struct {
Expand Down Expand Up @@ -70,56 +68,9 @@ func (c *ExpandCmd) Run(args []string) error {
return fmt.Errorf("expected exactly one argument, a path to a file to expand")
}

expanded := src

{
// All
expr := `\$(\({1,2})([[:alnum:]_\.\-]+)(\|base64)?\){1,2}|(\{{2})([[:alnum:]_\.\-]+)(\|base64)?\}{2}`
re := regexp.MustCompile(expr)
expandFunction := func(match []byte) []byte {
re := regexp.MustCompile(expr)

matchStr := string(match[:])
result := re.FindStringSubmatch(matchStr)

if result[0] != matchStr {
glog.Fatalf("Unexpected match: %q", matchStr)
}

if result[2] == "" && result[5] == "" {
glog.Fatalf("No variable defined within: %q", matchStr)
}

key := result[2] + result[5]
replacement := values[key]

if replacement == nil {
err = fmt.Errorf("Key not found: %q", key)
return match
}

if (result[3] + result[6]) == "|base64" {
replacement = base64.StdEncoding.EncodeToString([]byte(replacement.(string)))
}

var s string
delim := result[1] + result[4]
switch len(delim) {
case 1:
s = fmt.Sprintf("\"%v\"", replacement)
case 2:
s = fmt.Sprintf("%v", replacement)
default:
glog.Fatalf("Unexpected delimiter %q count: %q", delim, len(delim))
}

return []byte(s)
}

expanded = re.ReplaceAllFunc(expanded, expandFunction)
if err != nil {
return err
}
expanded, err := expand.DoExpand(src, values)
if err != nil {
return err
}

_, err = os.Stdout.Write(expanded)
Expand Down
5 changes: 5 additions & 0 deletions cmd/kexpand/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
Execute()
}
5 changes: 2 additions & 3 deletions cmd/root.go → cmd/kexpand/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cmd
package main

import (
goflag "flag"
"fmt"
"os"

"github.com/spf13/cobra"
"os"
)

type RootCmd struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go → cmd/kexpand/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand Down
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package kexpand is the parent package for the kexpand tool
package kexpand
35 changes: 35 additions & 0 deletions images/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package(default_visibility = ["//visibility:public"])

load("@bazel_tools//tools/build_defs/docker:docker.bzl", "docker_build")

docker_build(
name = "busybox",
debs = [
"@busybox_deb//file",
],
symlinks = {
"/bin/sh": "/bin/busybox",
"/usr/bin/busybox": "/bin/busybox",
"/usr/sbin/busybox": "/bin/busybox",
"/sbin/busybox": "/bin/busybox",
},
)

docker_build(
name = "busybox-libc",
base = ":busybox",
debs = [
"@libc_deb//file",
],
)

docker_build(
name = "kexpand",
base = ":busybox-libc",
cmd = ["/kexpand"],
files = [
"//cmd/kexpand",
],
directory = "/usr/bin/",
repository = "kopeio",
)
9 changes: 0 additions & 9 deletions main.go

This file was deleted.

25 changes: 25 additions & 0 deletions pkg/expand/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test"
)

go_library(
name = "go_default_library",
srcs = [
"expand.go",
],
deps = [
"@com_github_golang_glog//:go_default_library",
],
)

go_test(
name = "go_default_tests",
srcs = ["expand_test.go"],
library = ":go_default_library",
)
Loading