Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
723b91cc
Commit
723b91cc
authored
Jun 15, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2232 from mitchellh/f-build-name
core: add build_name and build_type functions
parents
0787060c
a978bbf7
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
159 additions
and
4 deletions
+159
-4
helper/config/decode.go
helper/config/decode.go
+4
-0
packer/core_test.go
packer/core_test.go
+58
-0
packer/test-fixtures/build-var-build-name.json
packer/test-fixtures/build-var-build-name.json
+6
-0
packer/test-fixtures/build-var-build-type.json
packer/test-fixtures/build-var-build-type.json
+6
-0
template/interpolate/funcs.go
template/interpolate/funcs.go
+22
-0
template/interpolate/funcs_test.go
template/interpolate/funcs_test.go
+50
-0
template/interpolate/i.go
template/interpolate/i.go
+11
-4
website/source/docs/templates/configuration-templates.html.markdown
...urce/docs/templates/configuration-templates.html.markdown
+2
-0
No files found.
helper/config/decode.go
View file @
723b91cc
...
...
@@ -106,6 +106,8 @@ func Decode(target interface{}, config *DecodeOpts, raws ...interface{}) error {
// detecting things like user variables from the raw configuration params.
func
DetectContext
(
raws
...
interface
{})
(
*
interpolate
.
Context
,
error
)
{
var
s
struct
{
BuildName
string
`mapstructure:"packer_build_name"`
BuildType
string
`mapstructure:"packer_builder_type"`
TemplatePath
string
`mapstructure:"packer_template_path"`
Vars
map
[
string
]
string
`mapstructure:"packer_user_variables"`
}
...
...
@@ -117,6 +119,8 @@ func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
}
return
&
interpolate
.
Context
{
BuildName
:
s
.
BuildName
,
BuildType
:
s
.
BuildType
,
TemplatePath
:
s
.
TemplatePath
,
UserVariables
:
s
.
Vars
,
},
nil
...
...
packer/core_test.go
View file @
723b91cc
...
...
@@ -142,6 +142,64 @@ func TestCoreBuild_env(t *testing.T) {
}
}
func
TestCoreBuild_buildNameVar
(
t
*
testing
.
T
)
{
config
:=
TestCoreConfig
(
t
)
testCoreTemplate
(
t
,
config
,
fixtureDir
(
"build-var-build-name.json"
))
b
:=
TestBuilder
(
t
,
config
,
"test"
)
core
:=
TestCore
(
t
,
config
)
b
.
ArtifactId
=
"hello"
build
,
err
:=
core
.
Build
(
"test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
_
,
err
:=
build
.
Prepare
();
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
// Interpolate the config
var
result
map
[
string
]
interface
{}
err
=
configHelper
.
Decode
(
&
result
,
nil
,
b
.
PrepareConfig
...
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
result
[
"value"
]
!=
"test"
{
t
.
Fatalf
(
"bad: %#v"
,
result
)
}
}
func
TestCoreBuild_buildTypeVar
(
t
*
testing
.
T
)
{
config
:=
TestCoreConfig
(
t
)
testCoreTemplate
(
t
,
config
,
fixtureDir
(
"build-var-build-type.json"
))
b
:=
TestBuilder
(
t
,
config
,
"test"
)
core
:=
TestCore
(
t
,
config
)
b
.
ArtifactId
=
"hello"
build
,
err
:=
core
.
Build
(
"test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
_
,
err
:=
build
.
Prepare
();
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
// Interpolate the config
var
result
map
[
string
]
interface
{}
err
=
configHelper
.
Decode
(
&
result
,
nil
,
b
.
PrepareConfig
...
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
result
[
"value"
]
!=
"test"
{
t
.
Fatalf
(
"bad: %#v"
,
result
)
}
}
func
TestCoreBuild_nonExist
(
t
*
testing
.
T
)
{
config
:=
TestCoreConfig
(
t
)
testCoreTemplate
(
t
,
config
,
fixtureDir
(
"build-basic.json"
))
...
...
packer/test-fixtures/build-var-build-name.json
0 → 100644
View file @
723b91cc
{
"builders"
:
[{
"type"
:
"test"
,
"value"
:
"{{build_name}}"
}]
}
packer/test-fixtures/build-var-build-type.json
0 → 100644
View file @
723b91cc
{
"builders"
:
[{
"type"
:
"test"
,
"value"
:
"{{build_type}}"
}]
}
template/interpolate/funcs.go
View file @
723b91cc
...
...
@@ -24,6 +24,8 @@ func init() {
// Funcs are the interpolation funcs that are available within interpolations.
var
FuncGens
=
map
[
string
]
FuncGenerator
{
"build_name"
:
funcGenBuildName
,
"build_type"
:
funcGenBuildType
,
"env"
:
funcGenEnv
,
"isotime"
:
funcGenIsotime
,
"pwd"
:
funcGenPwd
,
...
...
@@ -56,6 +58,26 @@ func Funcs(ctx *Context) template.FuncMap {
return
template
.
FuncMap
(
result
)
}
func
funcGenBuildName
(
ctx
*
Context
)
interface
{}
{
return
func
()
(
string
,
error
)
{
if
ctx
==
nil
||
ctx
.
BuildName
==
""
{
return
""
,
errors
.
New
(
"build_name not available"
)
}
return
ctx
.
BuildName
,
nil
}
}
func
funcGenBuildType
(
ctx
*
Context
)
interface
{}
{
return
func
()
(
string
,
error
)
{
if
ctx
==
nil
||
ctx
.
BuildType
==
""
{
return
""
,
errors
.
New
(
"build_name not available"
)
}
return
ctx
.
BuildType
,
nil
}
}
func
funcGenEnv
(
ctx
*
Context
)
interface
{}
{
return
func
(
k
string
)
(
string
,
error
)
{
if
!
ctx
.
EnableEnv
{
...
...
template/interpolate/funcs_test.go
View file @
723b91cc
...
...
@@ -8,6 +8,56 @@ import (
"time"
)
func
TestFuncBuildName
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
Input
string
Output
string
}{
{
`{{build_name}}`
,
"foo"
,
},
}
ctx
:=
&
Context
{
BuildName
:
"foo"
}
for
_
,
tc
:=
range
cases
{
i
:=
&
I
{
Value
:
tc
.
Input
}
result
,
err
:=
i
.
Render
(
ctx
)
if
err
!=
nil
{
t
.
Fatalf
(
"Input: %s
\n\n
err: %s"
,
tc
.
Input
,
err
)
}
if
result
!=
tc
.
Output
{
t
.
Fatalf
(
"Input: %s
\n\n
Got: %s"
,
tc
.
Input
,
result
)
}
}
}
func
TestFuncBuildType
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
Input
string
Output
string
}{
{
`{{build_type}}`
,
"foo"
,
},
}
ctx
:=
&
Context
{
BuildType
:
"foo"
}
for
_
,
tc
:=
range
cases
{
i
:=
&
I
{
Value
:
tc
.
Input
}
result
,
err
:=
i
.
Render
(
ctx
)
if
err
!=
nil
{
t
.
Fatalf
(
"Input: %s
\n\n
err: %s"
,
tc
.
Input
,
err
)
}
if
result
!=
tc
.
Output
{
t
.
Fatalf
(
"Input: %s
\n\n
Got: %s"
,
tc
.
Input
,
result
)
}
}
}
func
TestFuncEnv
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
Input
string
...
...
template/interpolate/i.go
View file @
723b91cc
...
...
@@ -14,16 +14,23 @@ type Context struct {
// Funcs are extra functions available in the template
Funcs
map
[
string
]
interface
{}
// TemplatePath is the path to the template that this is being
// rendered within.
TemplatePath
string
// UserVariables is the mapping of user variables that the
// "user" function reads from.
UserVariables
map
[
string
]
string
// EnableEnv enables the env function
EnableEnv
bool
// All the fields below are used for built-in functions.
//
// BuildName and BuildType are the name and type, respectively,
// of the builder being used.
//
// TemplatePath is the path to the template that this is being
// rendered within.
BuildName
string
BuildType
string
TemplatePath
string
}
// Render is shorthand for constructing an I and calling Render.
...
...
website/source/docs/templates/configuration-templates.html.markdown
View file @
723b91cc
...
...
@@ -55,6 +55,8 @@ While some configuration settings have local variables specific to only that
configuration, a set of functions are available globally for use in _any string_
in Packer templates. These are listed below for reference.
*
`build_name`
- The name of the build being run.
*
`build_type`
- The type of the builder being used currently.
*
`isotime [FORMAT]`
- UTC time, which can be
[
formatted
](
http://golang.org/pkg/time/#example_Time_Format
)
.
See more examples below.
*
`lower`
- Lowercases the string.
...
...
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