Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
43c83d47
Commit
43c83d47
authored
Jul 06, 2017
by
Alexander Campbell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cmd/version: refactor to use the -Options pattern
This pattern is described in
https://github.com/kubernetes/community/blob/49d65710b3ef8f4326df79706daf1e59c2e4a465/contributors/devel/kubectl-conventions.md#command-implementation-conventions
parent
8e5584fe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
28 deletions
+53
-28
version.go
pkg/kubectl/cmd/version.go
+53
-28
No files found.
pkg/kubectl/cmd/version.go
View file @
43c83d47
...
@@ -37,6 +37,14 @@ type Version struct {
...
@@ -37,6 +37,14 @@ type Version struct {
ServerVersion
*
apimachineryversion
.
Info
`json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
ServerVersion
*
apimachineryversion
.
Info
`json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
}
}
// VersionOptions: describe the options available to users of the "kubectl
// version" command.
type
VersionOptions
struct
{
clientOnly
bool
short
bool
output
string
}
var
(
var
(
versionExample
=
templates
.
Examples
(
i18n
.
T
(
`
versionExample
=
templates
.
Examples
(
i18n
.
T
(
`
# Print the client and server versions for the current context
# Print the client and server versions for the current context
...
@@ -50,73 +58,90 @@ func NewCmdVersion(f cmdutil.Factory, out io.Writer) *cobra.Command {
...
@@ -50,73 +58,90 @@ func NewCmdVersion(f cmdutil.Factory, out io.Writer) *cobra.Command {
Long
:
"Print the client and server version information for the current context"
,
Long
:
"Print the client and server version information for the current context"
,
Example
:
versionExample
,
Example
:
versionExample
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
err
:=
RunVersion
(
f
,
out
,
cmd
)
options
:=
new
(
VersionOptions
)
cmdutil
.
CheckErr
(
err
)
cmdutil
.
CheckErr
(
options
.
Complete
(
cmd
))
cmdutil
.
CheckErr
(
options
.
Validate
())
cmdutil
.
CheckErr
(
options
.
Run
(
f
,
out
))
},
},
}
}
cmd
.
Flags
()
.
BoolP
(
"client"
,
"c"
,
false
,
"Client version only (no server required)."
)
cmd
.
Flags
()
.
BoolP
(
"client"
,
"c"
,
false
,
"Client version only (no server required)."
)
cmd
.
Flags
()
.
BoolP
(
"short"
,
""
,
false
,
"Print just the version number."
)
cmd
.
Flags
()
.
BoolP
(
"short"
,
""
,
false
,
"Print just the version number."
)
cmd
.
Flags
()
.
String
(
"output"
,
""
,
"o
utput format, options available are yaml and json
"
)
cmd
.
Flags
()
.
String
(
"output"
,
""
,
"o
ne of 'yaml' or 'json'
"
)
cmd
.
Flags
()
.
MarkShorthandDeprecated
(
"client"
,
"please use --client instead."
)
cmd
.
Flags
()
.
MarkShorthandDeprecated
(
"client"
,
"please use --client instead."
)
return
cmd
return
cmd
}
}
func
RunVersion
(
f
cmdutil
.
Factory
,
out
io
.
Writer
,
cmd
*
cobra
.
Command
)
error
{
func
retrieveServerVersion
(
f
cmdutil
.
Factory
)
(
*
apimachineryversion
.
Info
,
error
)
{
var
serverVersion
*
apimachineryversion
.
Info
=
nil
discoveryClient
,
err
:=
f
.
DiscoveryClient
()
var
serverErr
error
=
nil
if
err
!=
nil
{
vo
:=
Version
{
nil
,
nil
}
return
nil
,
err
}
// Always request fresh data from the server
discoveryClient
.
Invalidate
()
return
discoveryClient
.
ServerVersion
()
}
func
(
o
*
VersionOptions
)
Run
(
f
cmdutil
.
Factory
,
out
io
.
Writer
)
error
{
var
(
serverVersion
*
apimachineryversion
.
Info
serverErr
error
versionInfo
Version
)
clientVersion
:=
version
.
Get
()
clientVersion
:=
version
.
Get
()
vo
.
ClientVersion
=
&
clientVersion
v
ersionInf
o
.
ClientVersion
=
&
clientVersion
if
!
cmdutil
.
GetFlagBool
(
cmd
,
"client"
)
{
if
!
o
.
clientOnly
{
serverVersion
,
serverErr
=
retrieveServerVersion
(
f
)
serverVersion
,
serverErr
=
retrieveServerVersion
(
f
)
vo
.
ServerVersion
=
serverVersion
v
ersionInf
o
.
ServerVersion
=
serverVersion
}
}
switch
o
f
:=
cmdutil
.
GetFlagString
(
cmd
,
"output"
);
of
{
switch
o
.
output
{
case
""
:
case
""
:
if
cmdutil
.
GetFlagBool
(
cmd
,
"short"
)
{
if
o
.
short
{
fmt
.
Fprintf
(
out
,
"Client Version: %s
\n
"
,
clientVersion
.
GitVersion
)
fmt
.
Fprintf
(
out
,
"Client Version: %s
\n
"
,
clientVersion
.
GitVersion
)
if
serverVersion
!=
nil
{
if
serverVersion
!=
nil
{
fmt
.
Fprintf
(
out
,
"Server Version: %s
\n
"
,
serverVersion
.
GitVersion
)
fmt
.
Fprintf
(
out
,
"Server Version: %s
\n
"
,
serverVersion
.
GitVersion
)
}
}
}
else
{
}
else
{
fmt
.
Fprintf
(
out
,
"Client Version: %s
\n
"
,
fmt
.
Sprintf
(
"%#v"
,
clientVersion
))
fmt
.
Fprintf
(
out
,
"Client Version: %s
\n
"
,
fmt
.
Sprintf
(
"%#v"
,
clientVersion
))
if
serverVersion
!=
nil
{
if
serverVersion
!=
nil
{
fmt
.
Fprintf
(
out
,
"Server Version: %s
\n
"
,
fmt
.
Sprintf
(
"%#v"
,
*
serverVersion
))
fmt
.
Fprintf
(
out
,
"Server Version: %s
\n
"
,
fmt
.
Sprintf
(
"%#v"
,
*
serverVersion
))
}
}
}
}
case
"yaml"
:
case
"yaml"
:
y
,
err
:=
yaml
.
Marshal
(
&
v
o
)
marshalled
,
err
:=
yaml
.
Marshal
(
&
versionInf
o
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
fmt
.
Fprintln
(
out
,
string
(
marshalled
))
fmt
.
Fprintln
(
out
,
string
(
y
))
case
"json"
:
case
"json"
:
y
,
err
:=
json
.
Marshal
(
&
v
o
)
marshalled
,
err
:=
json
.
Marshal
(
&
versionInf
o
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
fmt
.
Fprintln
(
out
,
string
(
y
))
fmt
.
Fprintln
(
out
,
string
(
marshalled
))
default
:
default
:
return
errors
.
New
(
"invalid output format: "
+
of
)
// There is a bug in the program if we hit this case.
// However, we follow a policy of never panicking.
return
fmt
.
Errorf
(
"VersionOptions were not validated: --output=%q should have been rejected"
,
o
.
output
)
}
}
return
serverErr
return
serverErr
}
}
func
retrieveServerVersion
(
f
cmdutil
.
Factory
)
(
*
apimachineryversion
.
Info
,
error
)
{
func
(
o
*
VersionOptions
)
Complete
(
cmd
*
cobra
.
Command
)
error
{
discoveryClient
,
err
:=
f
.
DiscoveryClient
()
o
.
clientOnly
=
cmdutil
.
GetFlagBool
(
cmd
,
"client"
)
if
err
!=
nil
{
o
.
short
=
cmdutil
.
GetFlagBool
(
cmd
,
"short"
)
return
nil
,
err
o
.
output
=
cmdutil
.
GetFlagString
(
cmd
,
"output"
)
return
nil
}
func
(
o
*
VersionOptions
)
Validate
()
error
{
if
o
.
output
!=
""
&&
o
.
output
!=
"yaml"
&&
o
.
output
!=
"json"
{
return
errors
.
New
(
`--output must be 'yaml' or 'json'`
)
}
}
// Always request fresh data from the server
return
nil
discoveryClient
.
Invalidate
()
return
discoveryClient
.
ServerVersion
()
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment