@@ -18,9 +18,12 @@ package main
1818
1919import  (
2020	"context" 
21+ 	"encoding/json" 
2122	"fmt" 
2223	"io" 
2324
25+ 	"sigs.k8s.io/yaml" 
26+ 
2427	"github.com/spf13/cobra" 
2528	"k8s.io/kops" 
2629	"k8s.io/kops/cmd/kops/util" 
@@ -57,6 +60,7 @@ func NewCmdVersion(f *util.Factory, out io.Writer) *cobra.Command {
5760
5861	cmd .Flags ().BoolVar (& options .short , "short" , options .short , "only print the main kOps version. Useful for scripting." )
5962	cmd .Flags ().BoolVar (& options .server , "server" , options .server , "show the kOps version that made the last change to the state store." )
63+ 	cmd .Flags ().StringVarP (& options .Output , "output" , "o" , options .Output , "One of 'yaml' or 'json'." )
6064
6165	return  cmd 
6266}
@@ -65,43 +69,69 @@ type VersionOptions struct {
6569	short        bool 
6670	server       bool 
6771	ClusterName  string 
72+ 	Output       string 
73+ }
74+ 
75+ // Version is a struct for version information 
76+ type  Version  struct  {
77+ 	ClientVersion  * kops.Info  `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` 
78+ 	ServerVersion  string      `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"` 
6879}
6980
7081// RunVersion implements the version command logic 
7182func  RunVersion (f  * util.Factory , out  io.Writer , options  * VersionOptions ) error  {
72- 	if  options .short  {
73- 		s  :=  kops .Version 
74- 		_ , err  :=  fmt .Fprintf (out , "%s\n " , s )
75- 		if  err  !=  nil  {
76- 			return  err 
77- 		}
78- 		if  options .server  {
79- 			server  :=  serverVersion (f , options )
80- 
81- 			_ , err  :=  fmt .Fprintf (out , "%s\n " , server )
82- 			return  err 
83- 		}
84- 
85- 		return  nil 
86- 	} else  {
87- 		client  :=  kops .Version 
88- 		if  kops .GitVersion  !=  ""  {
89- 			client  +=  " (git-"  +  kops .GitVersion  +  ")" 
90- 		}
91- 
92- 		{
93- 			_ , err  :=  fmt .Fprintf (out , "Client version: %s\n " , client )
83+ 	var  versionInfo  Version 
84+ 	clientVersion  :=  kops .Get ()
85+ 	versionInfo .ClientVersion  =  & clientVersion 
86+ 	if  options .server  {
87+ 		versionInfo .ServerVersion  =  serverVersion (f , options )
88+ 	}
89+ 	switch  options .Output  {
90+ 	case  "" :
91+ 		if  options .short  {
92+ 			_ , err  :=  fmt .Fprintf (out , "%s\n " , versionInfo .ClientVersion .Version )
9493			if  err  !=  nil  {
9594				return  err 
9695			}
97- 		}
98- 		if  options .server  {
99- 			server  :=  serverVersion (f , options )
96+ 			if  options .server  {
97+ 				_ , err  :=  fmt .Fprintf (out , "%s\n " , versionInfo .ServerVersion )
98+ 				return  err 
99+ 			}
100+ 			return  nil 
101+ 		} else  {
102+ 			client  :=  versionInfo .ClientVersion .Version 
103+ 			if  versionInfo .ClientVersion .GitVersion  !=  ""  {
104+ 				client  +=  " (git-"  +  versionInfo .ClientVersion .GitVersion  +  ")" 
105+ 			}
100106
101- 			_ , err  :=  fmt .Fprintf (out , "Last applied server version: %s\n " , server )
107+ 			{
108+ 				_ , err  :=  fmt .Fprintf (out , "Client Version: %s\n " , client )
109+ 				if  err  !=  nil  {
110+ 					return  err 
111+ 				}
112+ 			}
113+ 			if  options .server  {
114+ 				_ , err  :=  fmt .Fprintf (out , "Last applied server version: %s\n " , versionInfo .ServerVersion )
115+ 				return  err 
116+ 			}
117+ 			return  nil 
118+ 		}
119+ 	case  OutputYaml :
120+ 		marshalled , err  :=  yaml .Marshal (& versionInfo )
121+ 		if  err  !=  nil  {
122+ 			return  err 
123+ 		}
124+ 		_ , err  =  fmt .Fprintln (out , string (marshalled ))
125+ 		return  err 
126+ 	case  OutputJSON :
127+ 		marshalled , err  :=  json .MarshalIndent (& versionInfo , "" , "  " )
128+ 		if  err  !=  nil  {
102129			return  err 
103130		}
104- 		return  nil 
131+ 		_ , err  =  fmt .Fprintln (out , string (marshalled ))
132+ 		return  err 
133+ 	default :
134+ 		return  fmt .Errorf ("VersionOptions were not validated: --output=%q should have been rejected" , options .Output )
105135	}
106136}
107137
0 commit comments