class Glooctl < Formula
  desc "Envoy-Powered API Gateway"
  homepage "https://docs.solo.io/gloo/latest/"
  url "https://github.com/solo-io/gloo.git",
      :tag      => "v1.4.3",
      :revision => "75dc856b54afb9f29c7b85951f423edf8f5e39fe"
  license "Apache-2.0"
  head "https://github.com/solo-io/gloo.git"

  bottle do
    cellar :any_skip_relocation
    sha256 "8e51e1a293601ec54d8338e20662a4fc7e41e44d6eeccfc6cac82619343c6214" => :catalina
    sha256 "8f8e8ceed37061f7f3dc8a2f3a72e04bd0a590a0df5d773ac86ddeec88d7902a" => :mojave
    sha256 "429a03ec39894d2b0083e70245fecb3ace45b2e6f569e17e4e72a7ecb9a04fa1" => :high_sierra
    sha256 "11447321cca4fdfdf868c5bf7eed476653d264e3f1c82c3431df1e4917baa8ce" => :x86_64_linux
  end

  depends_on "go" => :build

  def install
    ENV["GOPATH"] = buildpath
    dir = buildpath/"src/github.com/solo-io/gloo"
    dir.install buildpath.children - [buildpath/".brew_home"]

    cd dir do
      system "make", "glooctl", "TAGGED_VERSION=v#{version}"
      bin.install "_output/glooctl"
    end
  end

  test do
    run_output = shell_output("#{bin}/glooctl 2>&1")
    assert_match "glooctl is the unified CLI for Gloo.", run_output

    version_output = shell_output("#{bin}/glooctl version 2>&1")
    assert_match "Client: {\"version\":\"#{version}\"}", version_output

    version_output = shell_output("#{bin}/glooctl version 2>&1")
    assert_match "Server: version undefined", version_output

    # Should error out as it needs access to a Kubernetes cluster to operate correctly
    status_output = shell_output("#{bin}/glooctl get proxy 2>&1", 1)
    assert_match "failed to create kube client", status_output
  end
end
