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
02be089b
Unverified
Commit
02be089b
authored
Mar 20, 2018
by
juanvallejo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wire through json/yaml print flags
parent
808545a8
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
162 additions
and
5 deletions
+162
-5
BUILD
pkg/printers/BUILD
+6
-1
json_yaml_flags.go
pkg/printers/json_yaml_flags.go
+57
-0
json_yaml_flags_test.go
pkg/printers/json_yaml_flags_test.go
+89
-0
printers.go
pkg/printers/printers.go
+10
-4
No files found.
pkg/printers/BUILD
View file @
02be089b
...
@@ -13,6 +13,7 @@ go_library(
...
@@ -13,6 +13,7 @@ go_library(
"humanreadable.go",
"humanreadable.go",
"interface.go",
"interface.go",
"json.go",
"json.go",
"json_yaml_flags.go",
"jsonpath.go",
"jsonpath.go",
"name.go",
"name.go",
"printers.go",
"printers.go",
...
@@ -24,6 +25,7 @@ go_library(
...
@@ -24,6 +25,7 @@ go_library(
deps = [
deps = [
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
...
@@ -39,7 +41,10 @@ go_library(
...
@@ -39,7 +41,10 @@ go_library(
go_test(
go_test(
name = "go_default_xtest",
name = "go_default_xtest",
srcs = ["customcolumn_test.go"],
srcs = [
"customcolumn_test.go",
"json_yaml_flags_test.go",
],
deps = [
deps = [
":go_default_library",
":go_default_library",
"//pkg/api/legacyscheme:go_default_library",
"//pkg/api/legacyscheme:go_default_library",
...
...
pkg/printers/json_yaml_flags.go
0 → 100644
View file @
02be089b
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
printers
import
(
"fmt"
"strings"
"github.com/spf13/cobra"
)
// JSONYamlPrintFlags provides default flags necessary for json/yaml printing.
// Given the following flag values, a printer can be requested that knows
// how to handle printing based on these values.
type
JSONYamlPrintFlags
struct
{}
// ToPrinter receives an outputFormat and returns a printer capable of
// handling --output=(yaml|json) printing.
// Returns false if the specified outputFormat does not match a supported format.
// Supported Format types can be found in pkg/printers/printers.go
func
(
f
*
JSONYamlPrintFlags
)
ToPrinter
(
outputFormat
string
)
(
ResourcePrinter
,
bool
,
error
)
{
outputFormat
=
strings
.
ToLower
(
outputFormat
)
switch
outputFormat
{
case
"json"
:
return
&
JSONPrinter
{},
true
,
nil
case
"yaml"
:
return
&
YAMLPrinter
{},
true
,
nil
case
""
:
return
nil
,
false
,
fmt
.
Errorf
(
"missing output format"
)
default
:
return
nil
,
false
,
nil
}
}
// AddFlags receives a *cobra.Command reference and binds
// flags related to JSON or Yaml printing to it
func
(
f
*
JSONYamlPrintFlags
)
AddFlags
(
c
*
cobra
.
Command
)
{}
// NewJSONYamlPrintFlags returns flags associated with
// yaml or json printing, with default values set.
func
NewJSONYamlPrintFlags
()
*
JSONYamlPrintFlags
{
return
&
JSONYamlPrintFlags
{}
}
pkg/printers/json_yaml_flags_test.go
0 → 100644
View file @
02be089b
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
printers_test
import
(
"bytes"
"strings"
"testing"
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/printers"
)
func
TestPrinterSupportsExpectedJSONYamlFormats
(
t
*
testing
.
T
)
{
testObject
:=
&
v1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
}}
testCases
:=
[]
struct
{
name
string
outputFormat
string
expectedOutput
string
expectNoMatch
bool
}{
{
name
:
"json output format matches a json printer"
,
outputFormat
:
"json"
,
expectedOutput
:
"
\"
name
\"
:
\"
foo
\"
"
,
},
{
name
:
"yaml output format matches a yaml printer"
,
outputFormat
:
"yaml"
,
expectedOutput
:
"name: foo"
,
},
{
name
:
"output format for another printer does not match a json/yaml printer"
,
outputFormat
:
"jsonpath"
,
expectNoMatch
:
true
,
},
{
name
:
"invalid output format results in no match"
,
outputFormat
:
"invalid"
,
expectNoMatch
:
true
,
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
printFlags
:=
printers
.
JSONYamlPrintFlags
{}
p
,
matched
,
err
:=
printFlags
.
ToPrinter
(
tc
.
outputFormat
)
if
tc
.
expectNoMatch
{
if
matched
{
t
.
Fatalf
(
"expected no printer matches for output format %q"
,
tc
.
outputFormat
)
}
return
}
if
!
matched
{
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
tc
.
outputFormat
)
}
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
out
:=
bytes
.
NewBuffer
([]
byte
{})
err
=
p
.
PrintObj
(
testObject
,
out
)
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
if
!
strings
.
Contains
(
out
.
String
(),
tc
.
expectedOutput
)
{
t
.
Errorf
(
"unexpected output: expecting %q, got %q"
,
tc
.
expectedOutput
,
out
.
String
())
}
})
}
}
pkg/printers/printers.go
View file @
02be089b
...
@@ -34,11 +34,17 @@ func GetStandardPrinter(typer runtime.ObjectTyper, encoder runtime.Encoder, deco
...
@@ -34,11 +34,17 @@ func GetStandardPrinter(typer runtime.ObjectTyper, encoder runtime.Encoder, deco
var
printer
ResourcePrinter
var
printer
ResourcePrinter
switch
format
{
switch
format
{
case
"json"
:
case
"json"
,
"yaml"
:
printer
=
&
JSONPrinter
{}
jsonYamlFlags
:=
NewJSONYamlPrintFlags
()
p
,
matched
,
err
:=
jsonYamlFlags
.
ToPrinter
(
format
)
if
!
matched
{
return
nil
,
fmt
.
Errorf
(
"unable to match a printer to handle current print options"
)
}
if
err
!=
nil
{
return
nil
,
err
}
case
"yaml"
:
printer
=
p
printer
=
&
YAMLPrinter
{}
case
"name"
:
case
"name"
:
printer
=
&
NamePrinter
{
printer
=
&
NamePrinter
{
...
...
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