diff --git a/Makefile b/Makefile index dd07ba8..63bc48a 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,16 @@ .PHONY: build-linux build-osx build-windows clean +VERSION=$(shell git rev-parse --short HEAD) +BUILD=$(shell date +%FT%T%z) + build-linux: - @GOARCH=amd64 CGO_ENABLED=1 GOOS=linux go build -ldflags "-s -w" -o bin/kube-resource_linux + @GOARCH=amd64 CGO_ENABLED=1 GOOS=linux go build -ldflags "-s -w -X main.Version=${VERSION} -X main.Build=${BUILD}" -o bin/kube-resource_linux build-osx: - @GOARCH=amd64 CGO_ENABLED=1 GOOS=darwin go build -ldflags "-s -w" -o bin/kube-resource_darwin + @GOARCH=amd64 CGO_ENABLED=1 GOOS=darwin go build -ldflags "-s -w -X main.Version=${VERSION} -X main.Build=${BUILD}" -o bin/kube-resource_darwin build-windows: - @GOARCH=amd64 CGO_ENABLED=1 GOOS=windows go build -ldflags "-s -w" -o bin/kube-resource_windows + @GOARCH=amd64 CGO_ENABLED=1 GOOS=windows go build -ldflags "-s -w -X main.Version=${VERSION} -X main.Build=${BUILD}" -o bin/kube-resource_windows clean: @if [ -f bin/kube-resource_linux ] ; then rm bin/kube-resource_linux ; fi diff --git a/config/config.go b/config/config.go index 29d9327..64befdb 100644 --- a/config/config.go +++ b/config/config.go @@ -1,14 +1,11 @@ package config import ( - "github.com/spf13/pflag" "os" "path/filepath" ) -func InitKubeConfig() *string { - var kubeConfig *string - kubeConfig = pflag.StringP("kubeconfig", "c", "", "(optional) absolute path to the kubeconfig file") +func InitKubeConfig(kubeConfig *string) *string { if *kubeConfig == "" { if home := homeDir(); home != "" { c := filepath.Join(home, ".kube", "config") diff --git a/main.go b/main.go index 1cf700e..4f23319 100644 --- a/main.go +++ b/main.go @@ -8,19 +8,30 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "kubernetes-resource-list/config" + "os" "strings" ) var ( - prefer *bool + kubeConfig = pflag.StringP("kubeconfig", "c", "", "(optional) absolute path to the kubeconfig file") + prefer = pflag.BoolP("prefer", "p", false, "(optional) only display the supported resources with the version preferred by the server.") + search = pflag.StringP("search", "s", "", "(optional) only display the supported resources for a group and version.") + version = pflag.BoolP("version", "v", false, "show app version") + Version string + Build string ) func main() { - prefer = pflag.BoolP("prefer", "p", false, "(optional) only display the supported resources with the version preferred by the server.") - search := pflag.StringP("search", "s", "", "(optional) only display the supported resources for a group and version.") - c := config.InitKubeConfig() pflag.Parse() + + // init kubeconfig file + c := config.InitKubeConfig(kubeConfig) + + // show app info + showInfo() + discoveryClient := newDiscoveryClient(*c) + if *search == "" { getResources(discoveryClient) } else { @@ -100,3 +111,16 @@ func tableHeader() { func searchTableHeader() { fmt.Printf("%-35s\t%-s\n", "NAME", "VERBS") } + +func showInfo() { + if *version { + showAppInfo() + os.Exit(0) + } +} + +func showAppInfo() { + fmt.Printf("Version:\t %s\n", Version) + fmt.Printf("Build:\t %s\n", Build) + +}