-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #575 from fuweid/me-add-template-output
cmd: add go-template option for inspect commands
- Loading branch information
Showing
16 changed files
with
337 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2020 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 main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func builtinTmplFuncs() template.FuncMap { | ||
return template.FuncMap{ | ||
"json": jsonBuiltinTmplFunc, | ||
"title": strings.Title, | ||
"lower": strings.ToLower, | ||
"upper": strings.ToUpper, | ||
} | ||
} | ||
|
||
// jsonBuiltinTmplFunc allows to jsonify result of template execution. | ||
func jsonBuiltinTmplFunc(v interface{}) string { | ||
o := new(bytes.Buffer) | ||
enc := json.NewEncoder(o) | ||
// FIXME(fuweid): should we panic? | ||
enc.Encode(v) | ||
return o.String() | ||
} | ||
|
||
// tmplExecuteRawJSON executes the template with interface{} with decoded by | ||
// rawJSON string. | ||
func tmplExecuteRawJSON(tmplStr string, rawJSON string) (string, error) { | ||
dec := json.NewDecoder( | ||
bytes.NewReader([]byte(rawJSON)), | ||
) | ||
dec.UseNumber() | ||
|
||
var raw interface{} | ||
if err := dec.Decode(&raw); err != nil { | ||
return "", errors.Wrapf(err, "failed to decode json") | ||
} | ||
|
||
var o = new(bytes.Buffer) | ||
tmpl, err := template.New("tmplExecuteRawJSON").Funcs(builtinTmplFuncs()).Parse(tmplStr) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "failed to generate go-template") | ||
} | ||
|
||
// return error if key doesn't exist | ||
tmpl = tmpl.Option("missingkey=error") | ||
if err := tmpl.Execute(o, raw); err != nil { | ||
return "", errors.Wrapf(err, "failed to template data") | ||
} | ||
return o.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2020 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 main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTmplExecuteRawJSON(t *testing.T) { | ||
testcases := []struct { | ||
rawJSON string | ||
tmplStr string | ||
expected string | ||
hasErr bool | ||
}{ | ||
{ | ||
rawJSON: `{"ImageID": "abc", "Tag": "latest", "Size": 123456}`, | ||
tmplStr: "{{.Size}}", | ||
expected: "123456", | ||
}, | ||
{ | ||
rawJSON: `{"ImageID": "abcd", "Tag": "v1.0", "Size": 123456}`, | ||
tmplStr: "{{.Size}} , {{.ImageID}}", | ||
expected: "123456 , abcd", | ||
}, | ||
{ | ||
rawJSON: `{"ImageID": "aBcd", "Tag": "v1.0", "Size": 123456}`, | ||
tmplStr: "{{title .ImageID}} {{lower .ImageID}} {{upper .ImageID}}", | ||
expected: "ABcd abcd ABCD", | ||
}, | ||
{ | ||
rawJSON: `{"ImageID": "aBcd", "Tag": "v1.0", "Size": 123456}`, | ||
tmplStr: "{{ .ImageName }}", | ||
hasErr: true, // missing key | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
got, err := tmplExecuteRawJSON(tc.tmplStr, tc.rawJSON) | ||
if (err != nil) != tc.hasErr { | ||
t.Errorf("expected hasErr=%v, but got error=%v", tc.hasErr, err) | ||
} | ||
|
||
if tc.hasErr { | ||
continue | ||
} | ||
|
||
if got != tc.expected { | ||
t.Errorf("expected result=%v, but got=%v", tc.expected, got) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.