[SEBA-684] Adding --client-only flag to version command

Change-Id: Ifdda34f83c0c2569fe2ac0bed6f02db89af77352
diff --git a/Makefile b/Makefile
index 1f7edc5..b25a304 100644
--- a/Makefile
+++ b/Makefile
@@ -30,18 +30,20 @@
 BUILDTIME    = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
 
 LDFLAGS      = -ldflags \
-	'-X "github.com/opencord/cordctl/cli/version.Version=$(VERSION)"  \
-	 -X "github.com/opencord/cordctl/cli/version.GitCommit=$(GITCOMMIT)"  \
-	 -X "github.com/opencord/cordctl/cli/version.GitDirty=$(GITDIRTY)"  \
-	 -X "github.com/opencord/cordctl/cli/version.GoVersion=$(GOVERSION)"  \
-	 -X "github.com/opencord/cordctl/cli/version.Os=$$GOOS" \
-	 -X "github.com/opencord/cordctl/cli/version.Arch=$$GOARCH" \
-	 -X "github.com/opencord/cordctl/cli/version.BuildTime=$(BUILDTIME)"'
+	"-X github.com/opencord/cordctl/cli/version.Version=$(VERSION)  \
+	 -X github.com/opencord/cordctl/cli/version.GitCommit=$(GITCOMMIT)  \
+	 -X github.com/opencord/cordctl/cli/version.GitDirty=$(GITDIRTY)  \
+	 -X github.com/opencord/cordctl/cli/version.GoVersion=$(GOVERSION)  \
+	 -X github.com/opencord/cordctl/cli/version.Os=$$GOOS \
+	 -X github.com/opencord/cordctl/cli/version.Arch=$$GOARCH \
+	 -X github.com/opencord/cordctl/cli/version.BuildTime=$(BUILDTIME)"
 
 help:
 
 build: dependencies
-	GOOS=$(HOST_OS) GOARCH=$(HOST_ARCH) go build $(LDFLAGS) cmd/cordctl.go
+	export GOOS=$(HOST_OS) ;\
+	export GOARCH=$(HOST_ARCH) ;\
+	go build $(LDFLAGS) cmd/cordctl.go
 
 dependencies:
 	[ -d "vendor" ] || dep ensure
@@ -73,7 +75,9 @@
 rel_arch  = $(word 3, $(subst -, ,$(notdir $@)))
 
 $(RELEASE_BINS): dependencies
-	GOOS=$(rel_os) GOARCH=$(rel_arch) go build -v $(LDFLAGS) -o "$@" cmd/cordctl.go
+	export GOOS=$(rel_os) ;\
+	export GOARCH=$(rel_arch) ;\
+	go build -v $(LDFLAGS) -o "$@" cmd/cordctl.go
 
 release: $(RELEASE_BINS)
 
diff --git a/commands/version.go b/commands/version.go
index 4af5a8a..1941589 100644
--- a/commands/version.go
+++ b/commands/version.go
@@ -18,6 +18,7 @@
 
 import (
 	"context"
+
 	"github.com/fullstorydev/grpcurl"
 	flags "github.com/jessevdk/go-flags"
 	"github.com/jhump/protoreflect/dynamic"
@@ -50,7 +51,8 @@
 }
 
 type VersionOpts struct {
-	OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
+	OutputAs   string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
+	ClientOnly bool   `short:"c" long:"client-only" description:"Print only client version"`
 }
 
 var versionOpts = VersionOpts{}
@@ -79,14 +81,15 @@
 	parent.AddCommand("version", "display version", "Display client version", &versionOpts)
 }
 
-const DefaultFormat = `Client:
+const ClientFormat = `Client:
  Version         {{.Client.Version}}
  Go version:     {{.Client.GoVersion}}
  Git commit:     {{.Client.GitCommit}}
  Git dirty:      {{.Client.GitDirty}}
  Built:          {{.Client.BuildTime}}
  OS/Arch:        {{.Client.Os}}/{{.Client.Arch}}
-
+`
+const ServerFormat = `
 Server:
  Version         {{.Server.Version}}
  Python version: {{.Server.PythonVersion}}
@@ -95,51 +98,62 @@
  OS/Arch:        {{.Server.Os}}/{{.Server.Arch}}
 `
 
+const DefaultFormat = ClientFormat + ServerFormat
+
 func (options *VersionOpts) Execute(args []string) error {
-	conn, err := NewConnection()
-	if err != nil {
-		return err
+
+	if !options.ClientOnly {
+		conn, err := NewConnection()
+		if err != nil {
+			return err
+		}
+		defer conn.Close()
+
+		descriptor, method, err := GetReflectionMethod(conn, "xos.utility.GetVersion")
+		if err != nil {
+			return err
+		}
+
+		ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
+		defer cancel()
+
+		headers := GenerateHeaders()
+
+		h := &RpcEventHandler{}
+		err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams)
+		if err != nil {
+			return err
+		}
+
+		if h.Status != nil && h.Status.Err() != nil {
+			return h.Status.Err()
+		}
+
+		d, err := dynamic.AsDynamicMessage(h.Response)
+		if err != nil {
+			return err
+		}
+
+		versionInfo.Server.Version = d.GetFieldByName("version").(string)
+		versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
+		versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
+		versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
+		versionInfo.Server.Os = d.GetFieldByName("os").(string)
+		versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
 	}
-	defer conn.Close()
-
-	descriptor, method, err := GetReflectionMethod(conn, "xos.utility.GetVersion")
-	if err != nil {
-		return err
-	}
-
-	ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
-	defer cancel()
-
-	headers := GenerateHeaders()
-
-	h := &RpcEventHandler{}
-	err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams)
-	if err != nil {
-		return err
-	}
-
-	if h.Status != nil && h.Status.Err() != nil {
-		return h.Status.Err()
-	}
-
-	d, err := dynamic.AsDynamicMessage(h.Response)
-	if err != nil {
-		return err
-	}
-
-	versionInfo.Server.Version = d.GetFieldByName("version").(string)
-	versionInfo.Server.PythonVersion = d.GetFieldByName("pythonVersion").(string)
-	versionInfo.Server.GitCommit = d.GetFieldByName("gitCommit").(string)
-	versionInfo.Server.BuildTime = d.GetFieldByName("buildTime").(string)
-	versionInfo.Server.Os = d.GetFieldByName("os").(string)
-	versionInfo.Server.Arch = d.GetFieldByName("arch").(string)
 
 	result := CommandResult{
-		Format:   format.Format(DefaultFormat),
+		// Format:   format.Format(DefaultFormat),
 		OutputAs: toOutputType(options.OutputAs),
 		Data:     versionInfo,
 	}
 
+	if options.ClientOnly {
+		result.Format = format.Format(ClientFormat)
+	} else {
+		result.Format = format.Format(DefaultFormat)
+	}
+
 	GenerateOutput(&result)
 	return nil
 }