Skip to content

Commit

Permalink
Add consul
Browse files Browse the repository at this point in the history
  • Loading branch information
spachava753 committed Jul 12, 2021
1 parent 8ce275a commit 256b5f3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/toolmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spachava753/kpkg/pkg/tool/buildx"
"github.com/spachava753/kpkg/pkg/tool/civo"
"github.com/spachava753/kpkg/pkg/tool/clairctl"
"github.com/spachava753/kpkg/pkg/tool/consul"
"github.com/spachava753/kpkg/pkg/tool/copilot"
"github.com/spachava753/kpkg/pkg/tool/dive"
"github.com/spachava753/kpkg/pkg/tool/dockercompose"
Expand Down Expand Up @@ -61,7 +62,7 @@ import (
"github.com/spachava753/kpkg/pkg/tool/yq"
)

// register tools here
// GetTools registers tools here
func GetTools(os, arch string) []tool.Binary {
return []tool.Binary{
linkerd2.MakeBinary(os, arch),
Expand Down Expand Up @@ -121,5 +122,6 @@ func GetTools(os, arch string) []tool.Binary {
natscli.MakeBinary(os, arch),
flux.MakeBinary(os, arch),
tkn.MakeBinary(os, arch),
consul.MakeBinary(os, arch),
}
}
76 changes: 76 additions & 0 deletions pkg/tool/consul/consul.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package consul

import (
"fmt"
"github.com/Masterminds/semver"
kpkgerr "github.com/spachava753/kpkg/pkg/error"
"github.com/spachava753/kpkg/pkg/tool"
"os"
"path/filepath"
)

type consulTool struct {
arch,
os string
tool.GithubReleaseTool
}

func (l consulTool) Extract(artifactPath, _ string) (string, error) {
binaryPath := filepath.Join(artifactPath, l.Name())
binaryPathInfo, err := os.Stat(binaryPath)
if err != nil {
return "", err
}
if binaryPathInfo.IsDir() {
return "", fmt.Errorf("could not extract binary: %w", fmt.Errorf("path %s is not a directory", binaryPathInfo))
}

return binaryPath, err
}

func (l consulTool) Name() string {
return "consul"
}

func (l consulTool) ShortDesc() string {
return "Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure"
}

func (l consulTool) LongDesc() string {
return "Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure"
}

func (l consulTool) MakeUrl(version string) (string, error) {
v, err := semver.NewVersion(version)
if err != nil {
return "", err
}
version = v.String()
switch {
case l.os == "darwin" && l.arch == "amd64",
l.os == "windows" && l.arch == "386",
l.os == "windows" && l.arch == "amd64",
l.os == "linux" && l.arch == "amd64",
l.os == "linux" && l.arch == "arm64",
l.os == "linux" && l.arch == "arm",
l.os == "linux" && l.arch == "386",
l.os == "freebsd" && l.arch == "amd64",
l.os == "freebsd" && l.arch == "arm",
l.os == "freebsd" && l.arch == "386",
l.os == "openbsd" && l.arch == "amd64",
l.os == "openbsd" && l.arch == "386",
l.os == "solaris" && l.arch == "amd64":
default:
return "", &kpkgerr.UnsupportedRuntimeErr{Binary: l.Name()}
}
url := fmt.Sprintf("https://releases.hashicorp.com/consul/%s/consul_%s_%s_%s.zip", version, version, l.os, l.arch)
return url, nil
}

func MakeBinary(os, arch string) tool.Binary {
return consulTool{
arch: arch,
os: os,
GithubReleaseTool: tool.MakeGithubReleaseTool("hashicorp", "consul", 20),
}
}
32 changes: 32 additions & 0 deletions pkg/tool/consul/consul_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package consul

import (
"runtime"
"testing"
)

func TestConsulTool_Versions(t *testing.T) {
tests := []struct {
name string
want []string
homeDir string
wantErr bool
}{
{
name: "List versions",
want: nil,
homeDir: t.TempDir(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := MakeBinary(runtime.GOOS, runtime.GOARCH)
_, err := l.Versions()
if (err != nil) != tt.wantErr {
t.Errorf("Versions() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}

0 comments on commit 256b5f3

Please sign in to comment.