load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_embed_data")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_docker//container:push.bzl", "container_push")

# Concatenates the fortune databases to a single bundle.
# May need to adjust paths for your system (built on ubuntu 16.04).
# $ apt-get install fortune
genrule(
    name = "tar",
    outs = ["fortune.tar"],
    cmd = " && ".join([
        "OUT=$$(pwd)/$@",
        "tar -cvf $$OUT /usr/share/games/fortunes",
    ]),
)

# Generates a .go source file with the tarball content in 
# the fortuneFiles variable.
go_embed_data(
    name = "fortune_assets",
    srcs = [
        ":tar",
    ],
    package = "main",
    unpack = True,
    var = "fortuneFiles",
)

go_library(
    name = "go_default_library",
    srcs = [
        "main.go",
        ":fortune_assets",  # keep
    ],
    importpath = "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller/app",
    visibility = ["//visibility:private"],
    deps = [
        "//proto/fortune:go_default_library",
        "@com_github_vromero_gofortune//lib/fortune:go_default_library",
        "@org_golang_google_grpc//:go_default_library",
        "@org_golang_google_grpc//codes:go_default_library",
        "@org_golang_google_grpc//reflection:go_default_library",
    ],
)

go_binary(
    name = "fortune",
    embed = [":go_default_library"],
    goarch = "amd64",
    goos = "linux",
    pure = "on",
    static = "on",
    visibility = ["//visibility:public"],
)

go_image(
    name = "image",
    binary = ":fortune",
    visibility = ["//visibility:public"],
)

container_push(
    name = "push",
    format = "Docker",
    image = ":image",
    registry = "quay.io",
    repository = "kubernetes-ingress-controller/grpc-fortune-teller",
    tag = "0.1",
)
