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
8f660dc2
Unverified
Commit
8f660dc2
authored
Feb 01, 2017
by
Lucas Käldström
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a ParseTemplate util function for parsing go text templates easily
parent
582187b6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
129 additions
and
0 deletions
+129
-0
BUILD
cmd/kubeadm/app/util/BUILD
+2
-0
template.go
cmd/kubeadm/app/util/template.go
+37
-0
template_test.go
cmd/kubeadm/app/util/template_test.go
+90
-0
No files found.
cmd/kubeadm/app/util/BUILD
View file @
8f660dc2
...
...
@@ -12,6 +12,7 @@ go_library(
name = "go_default_library",
srcs = [
"error.go",
"template.go",
"tokens.go",
"version.go",
],
...
...
@@ -32,6 +33,7 @@ go_test(
name = "go_default_test",
srcs = [
"error_test.go",
"template_test.go",
"tokens_test.go",
"version_test.go",
],
...
...
cmd/kubeadm/app/util/template.go
0 → 100644
View file @
8f660dc2
/*
Copyright 2016 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
util
import
(
"bytes"
"fmt"
"text/template"
)
// TODO: Should be unit-tested
func
ParseTemplate
(
strtmpl
string
,
obj
interface
{})
([]
byte
,
error
)
{
var
buf
bytes
.
Buffer
tmpl
,
err
:=
template
.
New
(
"template"
)
.
Parse
(
strtmpl
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error when parsing template: %v"
,
err
)
}
err
=
tmpl
.
Execute
(
&
buf
,
obj
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error when executing template: %v"
,
err
)
}
return
buf
.
Bytes
(),
nil
}
cmd/kubeadm/app/util/template_test.go
0 → 100644
View file @
8f660dc2
/*
Copyright 2017 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
util
import
(
"testing"
)
const
(
validTmpl
=
"image: {{ .ImageRepository }}/pause-{{ .Arch }}:3.0"
validTmplOut
=
"image: gcr.io/google_containers/pause-amd64:3.0"
doNothing
=
"image: gcr.io/google_containers/pause-amd64:3.0"
invalidTmpl1
=
"{{ .baz }/d}"
invalidTmpl2
=
"{{ !foobar }}"
)
func
TestParseTemplate
(
t
*
testing
.
T
)
{
var
tmplTests
=
[]
struct
{
template
string
data
interface
{}
output
string
errExpected
bool
}{
// should parse a valid template and set the right values
{
template
:
validTmpl
,
data
:
struct
{
ImageRepository
,
Arch
string
}{
ImageRepository
:
"gcr.io/google_containers"
,
Arch
:
"amd64"
,
},
output
:
validTmplOut
,
errExpected
:
false
,
},
// should noop if there aren't any {{ .foo }} present
{
template
:
doNothing
,
data
:
struct
{
ImageRepository
,
Arch
string
}{
ImageRepository
:
"gcr.io/google_containers"
,
Arch
:
"amd64"
,
},
output
:
doNothing
,
errExpected
:
false
,
},
// invalid syntax, passing nil
{
template
:
invalidTmpl1
,
data
:
nil
,
output
:
""
,
errExpected
:
true
,
},
// invalid syntax
{
template
:
invalidTmpl2
,
data
:
struct
{}{},
output
:
""
,
errExpected
:
true
,
},
}
for
_
,
tt
:=
range
tmplTests
{
outbytes
,
err
:=
ParseTemplate
(
tt
.
template
,
tt
.
data
)
if
tt
.
errExpected
!=
(
err
!=
nil
)
{
t
.
Errorf
(
"failed TestParseTemplate:
\n\t
expected err: %s
\n\t
actual: %s"
,
tt
.
errExpected
,
err
,
)
}
if
tt
.
output
!=
string
(
outbytes
)
{
t
.
Errorf
(
"failed TestParseTemplate:
\n\t
expected bytes: %s
\n\t
actual: %s"
,
tt
.
output
,
outbytes
,
)
}
}
}
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