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
52d45cfd
Commit
52d45cfd
authored
Jul 02, 2018
by
David Eads
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move template printers to genericclioptions
parent
09784ccf
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
42 additions
and
46 deletions
+42
-46
cmd_printing_test.go
pkg/kubectl/cmd/cmd_printing_test.go
+3
-3
flags.go
pkg/kubectl/cmd/config/flags.go
+2
-2
flags.go
pkg/kubectl/cmd/create/flags.go
+2
-2
get_flags.go
pkg/kubectl/cmd/get/get_flags.go
+2
-2
jsonpath_flags.go
pkg/kubectl/genericclioptions/jsonpath_flags.go
+6
-6
jsonpath_flags_test.go
pkg/kubectl/genericclioptions/jsonpath_flags_test.go
+4
-5
kube_template_flags.go
pkg/kubectl/genericclioptions/kube_template_flags.go
+4
-4
jsonpath.go
pkg/kubectl/genericclioptions/printers/jsonpath.go
+2
-3
template.go
pkg/kubectl/genericclioptions/printers/template.go
+2
-3
template_test.go
pkg/kubectl/genericclioptions/printers/template_test.go
+0
-0
template_flags.go
pkg/kubectl/genericclioptions/template_flags.go
+6
-6
template_flags_test.go
pkg/kubectl/genericclioptions/template_flags_test.go
+4
-5
printers_test.go
pkg/printers/internalversion/printers_test.go
+5
-5
No files found.
pkg/kubectl/cmd/cmd_printing_test.go
View file @
52d45cfd
...
@@ -122,12 +122,12 @@ func TestIllegalPackageSourceCheckerThroughPrintFlags(t *testing.T) {
...
@@ -122,12 +122,12 @@ func TestIllegalPackageSourceCheckerThroughPrintFlags(t *testing.T) {
}
}
func
TestIllegalPackageSourceCheckerDirectlyThroughPrinters
(
t
*
testing
.
T
)
{
func
TestIllegalPackageSourceCheckerDirectlyThroughPrinters
(
t
*
testing
.
T
)
{
jsonPathPrinter
,
err
:=
printers
.
NewJSONPathPrinter
(
"{ .metadata.name }"
)
jsonPathPrinter
,
err
:=
generic
printers
.
NewJSONPathPrinter
(
"{ .metadata.name }"
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
}
goTemplatePrinter
,
err
:=
printers
.
NewGoTemplatePrinter
([]
byte
(
"{{ .metadata.name }}"
))
goTemplatePrinter
,
err
:=
generic
printers
.
NewGoTemplatePrinter
([]
byte
(
"{{ .metadata.name }}"
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -140,7 +140,7 @@ func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) {
...
@@ -140,7 +140,7 @@ func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) {
testCases
:=
[]
struct
{
testCases
:=
[]
struct
{
name
string
name
string
expectInternalObjErr
bool
expectInternalObjErr
bool
printer
printers
.
ResourcePrinter
printer
generic
printers
.
ResourcePrinter
obj
runtime
.
Object
obj
runtime
.
Object
expectedOutput
string
expectedOutput
string
}{
}{
...
...
pkg/kubectl/cmd/config/flags.go
View file @
52d45cfd
...
@@ -31,7 +31,7 @@ import (
...
@@ -31,7 +31,7 @@ import (
type
kubectlConfigPrintFlags
struct
{
type
kubectlConfigPrintFlags
struct
{
JSONYamlPrintFlags
*
genericclioptions
.
JSONYamlPrintFlags
JSONYamlPrintFlags
*
genericclioptions
.
JSONYamlPrintFlags
NamePrintFlags
*
genericclioptions
.
NamePrintFlags
NamePrintFlags
*
genericclioptions
.
NamePrintFlags
TemplateFlags
*
printer
s
.
KubeTemplatePrintFlags
TemplateFlags
*
genericclioption
s
.
KubeTemplatePrintFlags
TypeSetter
*
genericprinters
.
TypeSetterPrinter
TypeSetter
*
genericprinters
.
TypeSetterPrinter
...
@@ -94,7 +94,7 @@ func newKubeConfigPrintFlags(scheme runtime.ObjectTyper) *kubectlConfigPrintFlag
...
@@ -94,7 +94,7 @@ func newKubeConfigPrintFlags(scheme runtime.ObjectTyper) *kubectlConfigPrintFlag
JSONYamlPrintFlags
:
genericclioptions
.
NewJSONYamlPrintFlags
(),
JSONYamlPrintFlags
:
genericclioptions
.
NewJSONYamlPrintFlags
(),
NamePrintFlags
:
genericclioptions
.
NewNamePrintFlags
(
""
),
NamePrintFlags
:
genericclioptions
.
NewNamePrintFlags
(
""
),
TemplateFlags
:
printer
s
.
NewKubeTemplatePrintFlags
(),
TemplateFlags
:
genericclioption
s
.
NewKubeTemplatePrintFlags
(),
TypeSetter
:
genericprinters
.
NewTypeSetter
(
scheme
),
TypeSetter
:
genericprinters
.
NewTypeSetter
(
scheme
),
}
}
...
...
pkg/kubectl/cmd/create/flags.go
View file @
52d45cfd
...
@@ -31,7 +31,7 @@ import (
...
@@ -31,7 +31,7 @@ import (
type
PrintFlags
struct
{
type
PrintFlags
struct
{
JSONYamlPrintFlags
*
genericclioptions
.
JSONYamlPrintFlags
JSONYamlPrintFlags
*
genericclioptions
.
JSONYamlPrintFlags
NamePrintFlags
*
genericclioptions
.
NamePrintFlags
NamePrintFlags
*
genericclioptions
.
NamePrintFlags
TemplateFlags
*
printer
s
.
KubeTemplatePrintFlags
TemplateFlags
*
genericclioption
s
.
KubeTemplatePrintFlags
TypeSetter
*
genericprinters
.
TypeSetterPrinter
TypeSetter
*
genericprinters
.
TypeSetterPrinter
...
@@ -86,7 +86,7 @@ func NewPrintFlags(operation string, scheme runtime.ObjectTyper) *PrintFlags {
...
@@ -86,7 +86,7 @@ func NewPrintFlags(operation string, scheme runtime.ObjectTyper) *PrintFlags {
JSONYamlPrintFlags
:
genericclioptions
.
NewJSONYamlPrintFlags
(),
JSONYamlPrintFlags
:
genericclioptions
.
NewJSONYamlPrintFlags
(),
NamePrintFlags
:
genericclioptions
.
NewNamePrintFlags
(
operation
),
NamePrintFlags
:
genericclioptions
.
NewNamePrintFlags
(
operation
),
TemplateFlags
:
printer
s
.
NewKubeTemplatePrintFlags
(),
TemplateFlags
:
genericclioption
s
.
NewKubeTemplatePrintFlags
(),
TypeSetter
:
genericprinters
.
NewTypeSetter
(
scheme
),
TypeSetter
:
genericprinters
.
NewTypeSetter
(
scheme
),
}
}
...
...
pkg/kubectl/cmd/get/get_flags.go
View file @
52d45cfd
...
@@ -35,7 +35,7 @@ type PrintFlags struct {
...
@@ -35,7 +35,7 @@ type PrintFlags struct {
NamePrintFlags
*
genericclioptions
.
NamePrintFlags
NamePrintFlags
*
genericclioptions
.
NamePrintFlags
CustomColumnsFlags
*
printers
.
CustomColumnsPrintFlags
CustomColumnsFlags
*
printers
.
CustomColumnsPrintFlags
HumanReadableFlags
*
HumanPrintFlags
HumanReadableFlags
*
HumanPrintFlags
TemplateFlags
*
printer
s
.
KubeTemplatePrintFlags
TemplateFlags
*
genericclioption
s
.
KubeTemplatePrintFlags
NoHeaders
*
bool
NoHeaders
*
bool
OutputFormat
*
string
OutputFormat
*
string
...
@@ -182,7 +182,7 @@ func NewGetPrintFlags() *PrintFlags {
...
@@ -182,7 +182,7 @@ func NewGetPrintFlags() *PrintFlags {
JSONYamlPrintFlags
:
genericclioptions
.
NewJSONYamlPrintFlags
(),
JSONYamlPrintFlags
:
genericclioptions
.
NewJSONYamlPrintFlags
(),
NamePrintFlags
:
genericclioptions
.
NewNamePrintFlags
(
""
),
NamePrintFlags
:
genericclioptions
.
NewNamePrintFlags
(
""
),
TemplateFlags
:
printer
s
.
NewKubeTemplatePrintFlags
(),
TemplateFlags
:
genericclioption
s
.
NewKubeTemplatePrintFlags
(),
HumanReadableFlags
:
NewHumanPrintFlags
(),
HumanReadableFlags
:
NewHumanPrintFlags
(),
CustomColumnsFlags
:
printers
.
NewCustomColumnsPrintFlags
(),
CustomColumnsFlags
:
printers
.
NewCustomColumnsPrintFlags
(),
...
...
pkg/
printer
s/jsonpath_flags.go
→
pkg/
kubectl/genericclioption
s/jsonpath_flags.go
View file @
52d45cfd
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
printer
s
package
genericclioption
s
import
(
import
(
"fmt"
"fmt"
...
@@ -23,7 +23,7 @@ import (
...
@@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions
/printers
"
)
)
// templates are logically optional for specifying a format.
// templates are logically optional for specifying a format.
...
@@ -55,9 +55,9 @@ func (f *JSONPathPrintFlags) AllowedFormats() []string {
...
@@ -55,9 +55,9 @@ func (f *JSONPathPrintFlags) AllowedFormats() []string {
// ToPrinter receives an templateFormat and returns a printer capable of
// ToPrinter receives an templateFormat and returns a printer capable of
// handling --template format printing.
// handling --template format printing.
// Returns false if the specified templateFormat does not match a template format.
// Returns false if the specified templateFormat does not match a template format.
func
(
f
*
JSONPathPrintFlags
)
ToPrinter
(
templateFormat
string
)
(
ResourcePrinter
,
error
)
{
func
(
f
*
JSONPathPrintFlags
)
ToPrinter
(
templateFormat
string
)
(
printers
.
ResourcePrinter
,
error
)
{
if
(
f
.
TemplateArgument
==
nil
||
len
(
*
f
.
TemplateArgument
)
==
0
)
&&
len
(
templateFormat
)
==
0
{
if
(
f
.
TemplateArgument
==
nil
||
len
(
*
f
.
TemplateArgument
)
==
0
)
&&
len
(
templateFormat
)
==
0
{
return
nil
,
genericclioptions
.
NoCompatiblePrinterError
{
Options
:
f
,
OutputFormat
:
&
templateFormat
}
return
nil
,
NoCompatiblePrinterError
{
Options
:
f
,
OutputFormat
:
&
templateFormat
}
}
}
templateValue
:=
""
templateValue
:=
""
...
@@ -76,7 +76,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
...
@@ -76,7 +76,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
}
}
if
_
,
supportedFormat
:=
jsonFormats
[
templateFormat
];
!
supportedFormat
{
if
_
,
supportedFormat
:=
jsonFormats
[
templateFormat
];
!
supportedFormat
{
return
nil
,
genericclioptions
.
NoCompatiblePrinterError
{
OutputFormat
:
&
templateFormat
,
AllowedFormats
:
f
.
AllowedFormats
()}
return
nil
,
NoCompatiblePrinterError
{
OutputFormat
:
&
templateFormat
,
AllowedFormats
:
f
.
AllowedFormats
()}
}
}
if
len
(
templateValue
)
==
0
{
if
len
(
templateValue
)
==
0
{
...
@@ -92,7 +92,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
...
@@ -92,7 +92,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
templateValue
=
string
(
data
)
templateValue
=
string
(
data
)
}
}
p
,
err
:=
NewJSONPathPrinter
(
templateValue
)
p
,
err
:=
printers
.
NewJSONPathPrinter
(
templateValue
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error parsing jsonpath %s, %v
\n
"
,
templateValue
,
err
)
return
nil
,
fmt
.
Errorf
(
"error parsing jsonpath %s, %v
\n
"
,
templateValue
,
err
)
}
}
...
...
pkg/
printer
s/jsonpath_flags_test.go
→
pkg/
kubectl/genericclioption
s/jsonpath_flags_test.go
View file @
52d45cfd
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
printer
s
package
genericclioption
s
import
(
import
(
"bytes"
"bytes"
...
@@ -26,7 +26,6 @@ import (
...
@@ -26,7 +26,6 @@ import (
"k8s.io/api/core/v1"
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
)
)
func
TestPrinterSupportsExpectedJSONPathFormats
(
t
*
testing
.
T
)
{
func
TestPrinterSupportsExpectedJSONPathFormats
(
t
*
testing
.
T
)
{
...
@@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) {
...
@@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) {
p
,
err
:=
printFlags
.
ToPrinter
(
tc
.
outputFormat
)
p
,
err
:=
printFlags
.
ToPrinter
(
tc
.
outputFormat
)
if
tc
.
expectNoMatch
{
if
tc
.
expectNoMatch
{
if
!
genericclioptions
.
IsNoCompatiblePrinterError
(
err
)
{
if
!
IsNoCompatiblePrinterError
(
err
)
{
t
.
Fatalf
(
"expected no printer matches for output format %q"
,
tc
.
outputFormat
)
t
.
Fatalf
(
"expected no printer matches for output format %q"
,
tc
.
outputFormat
)
}
}
return
return
}
}
if
genericclioptions
.
IsNoCompatiblePrinterError
(
err
)
{
if
IsNoCompatiblePrinterError
(
err
)
{
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
tc
.
outputFormat
)
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
tc
.
outputFormat
)
}
}
...
@@ -184,7 +183,7 @@ func TestJSONPathPrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
...
@@ -184,7 +183,7 @@ func TestJSONPathPrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
outputFormat
:=
"jsonpath"
outputFormat
:=
"jsonpath"
p
,
err
:=
printFlags
.
ToPrinter
(
outputFormat
)
p
,
err
:=
printFlags
.
ToPrinter
(
outputFormat
)
if
genericclioptions
.
IsNoCompatiblePrinterError
(
err
)
{
if
IsNoCompatiblePrinterError
(
err
)
{
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
outputFormat
)
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
outputFormat
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/
printer
s/kube_template_flags.go
→
pkg/
kubectl/genericclioption
s/kube_template_flags.go
View file @
52d45cfd
...
@@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
...
@@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
printer
s
package
genericclioption
s
import
(
import
(
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions
/printers
"
)
)
// KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer.
// KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer.
...
@@ -37,8 +37,8 @@ func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
...
@@ -37,8 +37,8 @@ func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
return
append
(
f
.
GoTemplatePrintFlags
.
AllowedFormats
(),
f
.
JSONPathPrintFlags
.
AllowedFormats
()
...
)
return
append
(
f
.
GoTemplatePrintFlags
.
AllowedFormats
(),
f
.
JSONPathPrintFlags
.
AllowedFormats
()
...
)
}
}
func
(
f
*
KubeTemplatePrintFlags
)
ToPrinter
(
outputFormat
string
)
(
ResourcePrinter
,
error
)
{
func
(
f
*
KubeTemplatePrintFlags
)
ToPrinter
(
outputFormat
string
)
(
printers
.
ResourcePrinter
,
error
)
{
if
p
,
err
:=
f
.
JSONPathPrintFlags
.
ToPrinter
(
outputFormat
);
!
genericclioptions
.
IsNoCompatiblePrinterError
(
err
)
{
if
p
,
err
:=
f
.
JSONPathPrintFlags
.
ToPrinter
(
outputFormat
);
!
IsNoCompatiblePrinterError
(
err
)
{
return
p
,
err
return
p
,
err
}
}
return
f
.
GoTemplatePrintFlags
.
ToPrinter
(
outputFormat
)
return
f
.
GoTemplatePrintFlags
.
ToPrinter
(
outputFormat
)
...
...
pkg/printers/jsonpath.go
→
pkg/
kubectl/genericclioptions/
printers/jsonpath.go
View file @
52d45cfd
...
@@ -24,7 +24,6 @@ import (
...
@@ -24,7 +24,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/jsonpath"
"k8s.io/client-go/util/jsonpath"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
)
)
// exists returns true if it would be possible to call the index function
// exists returns true if it would be possible to call the index function
...
@@ -118,8 +117,8 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
...
@@ -118,8 +117,8 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
// we use reflect.Indirect here in order to obtain the actual value from a pointer.
// we use reflect.Indirect here in order to obtain the actual value from a pointer.
// we need an actual value in order to retrieve the package path for an object.
// we need an actual value in order to retrieve the package path for an object.
// using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers.
// using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers.
if
printers
.
InternalObjectPreventer
.
IsForbidden
(
reflect
.
Indirect
(
reflect
.
ValueOf
(
obj
))
.
Type
()
.
PkgPath
())
{
if
InternalObjectPreventer
.
IsForbidden
(
reflect
.
Indirect
(
reflect
.
ValueOf
(
obj
))
.
Type
()
.
PkgPath
())
{
return
fmt
.
Errorf
(
printers
.
InternalObjectPrinterErr
)
return
fmt
.
Errorf
(
InternalObjectPrinterErr
)
}
}
var
queryObj
interface
{}
=
obj
var
queryObj
interface
{}
=
obj
...
...
pkg/printers/template.go
→
pkg/
kubectl/genericclioptions/
printers/template.go
View file @
52d45cfd
...
@@ -25,7 +25,6 @@ import (
...
@@ -25,7 +25,6 @@ import (
"text/template"
"text/template"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
)
)
// GoTemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
// GoTemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
...
@@ -61,8 +60,8 @@ func (p *GoTemplatePrinter) AllowMissingKeys(allow bool) {
...
@@ -61,8 +60,8 @@ func (p *GoTemplatePrinter) AllowMissingKeys(allow bool) {
// PrintObj formats the obj with the Go Template.
// PrintObj formats the obj with the Go Template.
func
(
p
*
GoTemplatePrinter
)
PrintObj
(
obj
runtime
.
Object
,
w
io
.
Writer
)
error
{
func
(
p
*
GoTemplatePrinter
)
PrintObj
(
obj
runtime
.
Object
,
w
io
.
Writer
)
error
{
if
printers
.
InternalObjectPreventer
.
IsForbidden
(
reflect
.
Indirect
(
reflect
.
ValueOf
(
obj
))
.
Type
()
.
PkgPath
())
{
if
InternalObjectPreventer
.
IsForbidden
(
reflect
.
Indirect
(
reflect
.
ValueOf
(
obj
))
.
Type
()
.
PkgPath
())
{
return
fmt
.
Errorf
(
printers
.
InternalObjectPrinterErr
)
return
fmt
.
Errorf
(
InternalObjectPrinterErr
)
}
}
var
data
[]
byte
var
data
[]
byte
...
...
pkg/printers/template_test.go
→
pkg/
kubectl/genericclioptions/
printers/template_test.go
View file @
52d45cfd
File moved
pkg/
printer
s/template_flags.go
→
pkg/
kubectl/genericclioption
s/template_flags.go
View file @
52d45cfd
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
printer
s
package
genericclioption
s
import
(
import
(
"fmt"
"fmt"
...
@@ -23,7 +23,7 @@ import (
...
@@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions
/printers
"
)
)
// templates are logically optional for specifying a format.
// templates are logically optional for specifying a format.
...
@@ -57,9 +57,9 @@ func (f *GoTemplatePrintFlags) AllowedFormats() []string {
...
@@ -57,9 +57,9 @@ func (f *GoTemplatePrintFlags) AllowedFormats() []string {
// ToPrinter receives an templateFormat and returns a printer capable of
// ToPrinter receives an templateFormat and returns a printer capable of
// handling --template format printing.
// handling --template format printing.
// Returns false if the specified templateFormat does not match a template format.
// Returns false if the specified templateFormat does not match a template format.
func
(
f
*
GoTemplatePrintFlags
)
ToPrinter
(
templateFormat
string
)
(
ResourcePrinter
,
error
)
{
func
(
f
*
GoTemplatePrintFlags
)
ToPrinter
(
templateFormat
string
)
(
printers
.
ResourcePrinter
,
error
)
{
if
(
f
.
TemplateArgument
==
nil
||
len
(
*
f
.
TemplateArgument
)
==
0
)
&&
len
(
templateFormat
)
==
0
{
if
(
f
.
TemplateArgument
==
nil
||
len
(
*
f
.
TemplateArgument
)
==
0
)
&&
len
(
templateFormat
)
==
0
{
return
nil
,
genericclioptions
.
NoCompatiblePrinterError
{
Options
:
f
,
OutputFormat
:
&
templateFormat
}
return
nil
,
NoCompatiblePrinterError
{
Options
:
f
,
OutputFormat
:
&
templateFormat
}
}
}
templateValue
:=
""
templateValue
:=
""
...
@@ -78,7 +78,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
...
@@ -78,7 +78,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
}
}
if
_
,
supportedFormat
:=
templateFormats
[
templateFormat
];
!
supportedFormat
{
if
_
,
supportedFormat
:=
templateFormats
[
templateFormat
];
!
supportedFormat
{
return
nil
,
genericclioptions
.
NoCompatiblePrinterError
{
OutputFormat
:
&
templateFormat
,
AllowedFormats
:
f
.
AllowedFormats
()}
return
nil
,
NoCompatiblePrinterError
{
OutputFormat
:
&
templateFormat
,
AllowedFormats
:
f
.
AllowedFormats
()}
}
}
if
len
(
templateValue
)
==
0
{
if
len
(
templateValue
)
==
0
{
...
@@ -94,7 +94,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
...
@@ -94,7 +94,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
templateValue
=
string
(
data
)
templateValue
=
string
(
data
)
}
}
p
,
err
:=
NewGoTemplatePrinter
([]
byte
(
templateValue
))
p
,
err
:=
printers
.
NewGoTemplatePrinter
([]
byte
(
templateValue
))
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error parsing template %s, %v
\n
"
,
templateValue
,
err
)
return
nil
,
fmt
.
Errorf
(
"error parsing template %s, %v
\n
"
,
templateValue
,
err
)
}
}
...
...
pkg/
printer
s/template_flags_test.go
→
pkg/
kubectl/genericclioption
s/template_flags_test.go
View file @
52d45cfd
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
package
printer
s
package
genericclioption
s
import
(
import
(
"bytes"
"bytes"
...
@@ -26,7 +26,6 @@ import (
...
@@ -26,7 +26,6 @@ import (
"k8s.io/api/core/v1"
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
)
)
func
TestPrinterSupportsExpectedTemplateFormats
(
t
*
testing
.
T
)
{
func
TestPrinterSupportsExpectedTemplateFormats
(
t
*
testing
.
T
)
{
...
@@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) {
...
@@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) {
p
,
err
:=
printFlags
.
ToPrinter
(
tc
.
outputFormat
)
p
,
err
:=
printFlags
.
ToPrinter
(
tc
.
outputFormat
)
if
tc
.
expectNoMatch
{
if
tc
.
expectNoMatch
{
if
!
genericclioptions
.
IsNoCompatiblePrinterError
(
err
)
{
if
!
IsNoCompatiblePrinterError
(
err
)
{
t
.
Fatalf
(
"expected no printer matches for output format %q"
,
tc
.
outputFormat
)
t
.
Fatalf
(
"expected no printer matches for output format %q"
,
tc
.
outputFormat
)
}
}
return
return
}
}
if
genericclioptions
.
IsNoCompatiblePrinterError
(
err
)
{
if
IsNoCompatiblePrinterError
(
err
)
{
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
tc
.
outputFormat
)
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
tc
.
outputFormat
)
}
}
...
@@ -178,7 +177,7 @@ func TestTemplatePrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
...
@@ -178,7 +177,7 @@ func TestTemplatePrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
outputFormat
:=
"template"
outputFormat
:=
"template"
p
,
err
:=
printFlags
.
ToPrinter
(
outputFormat
)
p
,
err
:=
printFlags
.
ToPrinter
(
outputFormat
)
if
genericclioptions
.
IsNoCompatiblePrinterError
(
err
)
{
if
IsNoCompatiblePrinterError
(
err
)
{
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
outputFormat
)
t
.
Fatalf
(
"expected to match template printer for output format %q"
,
outputFormat
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/printers/internalversion/printers_test.go
View file @
52d45cfd
...
@@ -338,7 +338,7 @@ func TestUnknownTypePrinting(t *testing.T) {
...
@@ -338,7 +338,7 @@ func TestUnknownTypePrinting(t *testing.T) {
func
TestTemplatePanic
(
t
*
testing
.
T
)
{
func
TestTemplatePanic
(
t
*
testing
.
T
)
{
tmpl
:=
`{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
tmpl
:=
`{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
printer
,
err
:=
printers
.
NewGoTemplatePrinter
([]
byte
(
tmpl
))
printer
,
err
:=
generic
printers
.
NewGoTemplatePrinter
([]
byte
(
tmpl
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"tmpl fail: %v"
,
err
)
t
.
Fatalf
(
"tmpl fail: %v"
,
err
)
}
}
...
@@ -503,7 +503,7 @@ func TestTemplateStrings(t *testing.T) {
...
@@ -503,7 +503,7 @@ func TestTemplateStrings(t *testing.T) {
}
}
// The point of this test is to verify that the below template works.
// The point of this test is to verify that the below template works.
tmpl
:=
`{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
tmpl
:=
`{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
printer
,
err
:=
printers
.
NewGoTemplatePrinter
([]
byte
(
tmpl
))
printer
,
err
:=
generic
printers
.
NewGoTemplatePrinter
([]
byte
(
tmpl
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"tmpl fail: %v"
,
err
)
t
.
Fatalf
(
"tmpl fail: %v"
,
err
)
}
}
...
@@ -535,17 +535,17 @@ func TestPrinters(t *testing.T) {
...
@@ -535,17 +535,17 @@ func TestPrinters(t *testing.T) {
jsonpathPrinter
printers
.
ResourcePrinter
jsonpathPrinter
printers
.
ResourcePrinter
)
)
templatePrinter
,
err
=
printers
.
NewGoTemplatePrinter
([]
byte
(
"{{.name}}"
))
templatePrinter
,
err
=
generic
printers
.
NewGoTemplatePrinter
([]
byte
(
"{{.name}}"
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
templatePrinter2
,
err
=
printers
.
NewGoTemplatePrinter
([]
byte
(
"{{len .items}}"
))
templatePrinter2
,
err
=
generic
printers
.
NewGoTemplatePrinter
([]
byte
(
"{{len .items}}"
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
jsonpathPrinter
,
err
=
printers
.
NewJSONPathPrinter
(
"{.metadata.name}"
)
jsonpathPrinter
,
err
=
generic
printers
.
NewJSONPathPrinter
(
"{.metadata.name}"
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
...
...
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