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
579264bb
Commit
579264bb
authored
May 29, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
command/push: interpolate
parent
39463466
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
122 additions
and
21 deletions
+122
-21
command/command_test.go
command/command_test.go
+1
-0
command/push.go
command/push.go
+30
-12
command/push_test.go
command/push_test.go
+29
-0
command/test-fixtures/push-vars/template.json
command/test-fixtures/push-vars/template.json
+11
-0
packer/core.go
packer/core.go
+10
-9
template/interpolate/render_test.go
template/interpolate/render_test.go
+41
-0
No files found.
command/command_test.go
View file @
579264bb
...
@@ -28,6 +28,7 @@ func testMeta(t *testing.T) Meta {
...
@@ -28,6 +28,7 @@ func testMeta(t *testing.T) Meta {
var
out
,
err
bytes
.
Buffer
var
out
,
err
bytes
.
Buffer
return
Meta
{
return
Meta
{
CoreConfig
:
packer
.
TestCoreConfig
(
t
),
Ui
:
&
packer
.
BasicUi
{
Ui
:
&
packer
.
BasicUi
{
Writer
:
&
out
,
Writer
:
&
out
,
ErrorWriter
:
&
err
,
ErrorWriter
:
&
err
,
...
...
command/push.go
View file @
579264bb
package
command
package
command
import
(
import
(
"flag"
"fmt"
"fmt"
"io"
"io"
"os"
"os"
...
@@ -12,6 +11,7 @@ import (
...
@@ -12,6 +11,7 @@ import (
"github.com/hashicorp/atlas-go/archive"
"github.com/hashicorp/atlas-go/archive"
"github.com/hashicorp/atlas-go/v1"
"github.com/hashicorp/atlas-go/v1"
"github.com/mitchellh/packer/template"
"github.com/mitchellh/packer/template"
"github.com/mitchellh/packer/template/interpolate"
)
)
// archiveTemplateEntry is the name the template always takes within the slug.
// archiveTemplateEntry is the name the template always takes within the slug.
...
@@ -37,7 +37,7 @@ func (c *PushCommand) Run(args []string) int {
...
@@ -37,7 +37,7 @@ func (c *PushCommand) Run(args []string) int {
var
name
string
var
name
string
var
create
bool
var
create
bool
f
:=
flag
.
NewFlagSet
(
"push"
,
flag
.
ContinueOnError
)
f
:=
c
.
Meta
.
FlagSet
(
"push"
,
FlagSetVars
)
f
.
Usage
=
func
()
{
c
.
Ui
.
Error
(
c
.
Help
())
}
f
.
Usage
=
func
()
{
c
.
Ui
.
Error
(
c
.
Help
())
}
f
.
StringVar
(
&
token
,
"token"
,
""
,
"token"
)
f
.
StringVar
(
&
token
,
"token"
,
""
,
"token"
)
f
.
StringVar
(
&
message
,
"m"
,
""
,
"message"
)
f
.
StringVar
(
&
message
,
"m"
,
""
,
"message"
)
...
@@ -67,9 +67,23 @@ func (c *PushCommand) Run(args []string) int {
...
@@ -67,9 +67,23 @@ func (c *PushCommand) Run(args []string) int {
return
1
return
1
}
}
// Get the core
core
,
err
:=
c
.
Meta
.
Core
(
tpl
)
if
err
!=
nil
{
c
.
Ui
.
Error
(
err
.
Error
())
return
1
}
push
:=
tpl
.
Push
pushRaw
,
err
:=
interpolate
.
RenderInterface
(
&
push
,
core
.
Context
())
if
err
!=
nil
{
c
.
Ui
.
Error
(
err
.
Error
())
return
1
}
push
=
*
pushRaw
.
(
*
template
.
Push
)
// If we didn't pass name from the CLI, use the template
// If we didn't pass name from the CLI, use the template
if
name
==
""
{
if
name
==
""
{
name
=
tpl
.
P
ush
.
Name
name
=
p
ush
.
Name
}
}
// Validate some things
// Validate some things
...
@@ -83,14 +97,14 @@ func (c *PushCommand) Run(args []string) int {
...
@@ -83,14 +97,14 @@ func (c *PushCommand) Run(args []string) int {
// Determine our token
// Determine our token
if
token
==
""
{
if
token
==
""
{
token
=
tpl
.
P
ush
.
Token
token
=
p
ush
.
Token
}
}
// Build our client
// Build our client
defer
func
()
{
c
.
client
=
nil
}()
defer
func
()
{
c
.
client
=
nil
}()
c
.
client
=
atlas
.
DefaultClient
()
c
.
client
=
atlas
.
DefaultClient
()
if
tpl
.
P
ush
.
Address
!=
""
{
if
p
ush
.
Address
!=
""
{
c
.
client
,
err
=
atlas
.
NewClient
(
tpl
.
P
ush
.
Address
)
c
.
client
,
err
=
atlas
.
NewClient
(
p
ush
.
Address
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
Ui
.
Error
(
fmt
.
Sprintf
(
c
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error setting up API client: %s"
,
err
))
"Error setting up API client: %s"
,
err
))
...
@@ -103,9 +117,9 @@ func (c *PushCommand) Run(args []string) int {
...
@@ -103,9 +117,9 @@ func (c *PushCommand) Run(args []string) int {
// Build the archiving options
// Build the archiving options
var
opts
archive
.
ArchiveOpts
var
opts
archive
.
ArchiveOpts
opts
.
Include
=
tpl
.
P
ush
.
Include
opts
.
Include
=
p
ush
.
Include
opts
.
Exclude
=
tpl
.
P
ush
.
Exclude
opts
.
Exclude
=
p
ush
.
Exclude
opts
.
VCS
=
tpl
.
P
ush
.
VCS
opts
.
VCS
=
p
ush
.
VCS
opts
.
Extra
=
map
[
string
]
string
{
opts
.
Extra
=
map
[
string
]
string
{
archiveTemplateEntry
:
args
[
0
],
archiveTemplateEntry
:
args
[
0
],
}
}
...
@@ -120,7 +134,7 @@ func (c *PushCommand) Run(args []string) int {
...
@@ -120,7 +134,7 @@ func (c *PushCommand) Run(args []string) int {
// 3.) BaseDir is relative, so we use the path relative to the directory
// 3.) BaseDir is relative, so we use the path relative to the directory
// of the template.
// of the template.
//
//
path
:=
tpl
.
P
ush
.
BaseDir
path
:=
p
ush
.
BaseDir
if
path
==
""
||
!
filepath
.
IsAbs
(
path
)
{
if
path
==
""
||
!
filepath
.
IsAbs
(
path
)
{
tplPath
,
err
:=
filepath
.
Abs
(
args
[
0
])
tplPath
,
err
:=
filepath
.
Abs
(
args
[
0
])
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -150,7 +164,7 @@ func (c *PushCommand) Run(args []string) int {
...
@@ -150,7 +164,7 @@ func (c *PushCommand) Run(args []string) int {
// Build the upload options
// Build the upload options
var
uploadOpts
uploadOpts
var
uploadOpts
uploadOpts
uploadOpts
.
Slug
=
tpl
.
P
ush
.
Name
uploadOpts
.
Slug
=
p
ush
.
Name
uploadOpts
.
Builds
=
make
(
map
[
string
]
*
uploadBuildInfo
)
uploadOpts
.
Builds
=
make
(
map
[
string
]
*
uploadBuildInfo
)
for
_
,
b
:=
range
tpl
.
Builders
{
for
_
,
b
:=
range
tpl
.
Builders
{
info
:=
&
uploadBuildInfo
{
Type
:
b
.
Type
}
info
:=
&
uploadBuildInfo
{
Type
:
b
.
Type
}
...
@@ -229,7 +243,7 @@ func (c *PushCommand) Run(args []string) int {
...
@@ -229,7 +243,7 @@ func (c *PushCommand) Run(args []string) int {
return
1
return
1
}
}
c
.
Ui
.
Say
(
fmt
.
Sprintf
(
"Push successful to '%s'"
,
tpl
.
P
ush
.
Name
))
c
.
Ui
.
Say
(
fmt
.
Sprintf
(
"Push successful to '%s'"
,
p
ush
.
Name
))
return
0
return
0
}
}
...
@@ -257,6 +271,10 @@ Options:
...
@@ -257,6 +271,10 @@ Options:
"username/name".
"username/name".
-token=<token> The access token to use to when uploading
-token=<token> The access token to use to when uploading
-var 'key=value' Variable for templates, can be used multiple times.
-var-file=path JSON file containing user variables.
`
`
return
strings
.
TrimSpace
(
helpText
)
return
strings
.
TrimSpace
(
helpText
)
...
...
command/push_test.go
View file @
579264bb
...
@@ -190,6 +190,35 @@ func TestPush_uploadErrorCh(t *testing.T) {
...
@@ -190,6 +190,35 @@ func TestPush_uploadErrorCh(t *testing.T) {
}
}
}
}
func
TestPush_vars
(
t
*
testing
.
T
)
{
var
actualOpts
*
uploadOpts
uploadFn
:=
func
(
r
io
.
Reader
,
opts
*
uploadOpts
)
(
<-
chan
struct
{},
<-
chan
error
,
error
)
{
actualOpts
=
opts
doneCh
:=
make
(
chan
struct
{})
close
(
doneCh
)
return
doneCh
,
nil
,
nil
}
c
:=
&
PushCommand
{
Meta
:
testMeta
(
t
),
uploadFn
:
uploadFn
,
}
args
:=
[]
string
{
"-var"
,
"name=foo/bar"
,
filepath
.
Join
(
testFixture
(
"push-vars"
),
"template.json"
),
}
if
code
:=
c
.
Run
(
args
);
code
!=
0
{
fatalCommand
(
t
,
c
.
Meta
)
}
expected
:=
"foo/bar"
if
actualOpts
.
Slug
!=
expected
{
t
.
Fatalf
(
"bad: %#v"
,
actualOpts
.
Slug
)
}
}
func
testArchive
(
t
*
testing
.
T
,
r
io
.
Reader
)
[]
string
{
func
testArchive
(
t
*
testing
.
T
,
r
io
.
Reader
)
[]
string
{
// Finish the archiving process in-memory
// Finish the archiving process in-memory
var
buf
bytes
.
Buffer
var
buf
bytes
.
Buffer
...
...
command/test-fixtures/push-vars/template.json
0 → 100644
View file @
579264bb
{
"variables"
:
{
"name"
:
null
},
"builders"
:
[{
"type"
:
"dummy"
}],
"push"
:
{
"name"
:
"{{user `name`}}"
}
}
packer/core.go
View file @
579264bb
...
@@ -66,7 +66,7 @@ func NewCore(c *CoreConfig) (*Core, error) {
...
@@ -66,7 +66,7 @@ func NewCore(c *CoreConfig) (*Core, error) {
// to do this at this point with the variables.
// to do this at this point with the variables.
result
.
builds
=
make
(
map
[
string
]
*
template
.
Builder
)
result
.
builds
=
make
(
map
[
string
]
*
template
.
Builder
)
for
_
,
b
:=
range
c
.
Template
.
Builders
{
for
_
,
b
:=
range
c
.
Template
.
Builders
{
v
,
err
:=
interpolate
.
Render
(
b
.
Name
,
result
.
c
ontext
())
v
,
err
:=
interpolate
.
Render
(
b
.
Name
,
result
.
C
ontext
())
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
return
nil
,
fmt
.
Errorf
(
"Error interpolating builder '%s': %s"
,
"Error interpolating builder '%s': %s"
,
...
@@ -206,6 +206,14 @@ func (c *Core) Build(n string) (Build, error) {
...
@@ -206,6 +206,14 @@ func (c *Core) Build(n string) (Build, error) {
},
nil
},
nil
}
}
// Context returns an interpolation context.
func
(
c
*
Core
)
Context
()
*
interpolate
.
Context
{
return
&
interpolate
.
Context
{
TemplatePath
:
c
.
template
.
Path
,
UserVariables
:
c
.
variables
,
}
}
// validate does a full validation of the template.
// validate does a full validation of the template.
//
//
// This will automatically call template.validate() in addition to doing
// This will automatically call template.validate() in addition to doing
...
@@ -241,7 +249,7 @@ func (c *Core) init() error {
...
@@ -241,7 +249,7 @@ func (c *Core) init() error {
}
}
// Go through the variables and interpolate the environment variables
// Go through the variables and interpolate the environment variables
ctx
:=
c
.
c
ontext
()
ctx
:=
c
.
C
ontext
()
ctx
.
EnableEnv
=
true
ctx
.
EnableEnv
=
true
ctx
.
UserVariables
=
nil
ctx
.
UserVariables
=
nil
for
k
,
v
:=
range
c
.
template
.
Variables
{
for
k
,
v
:=
range
c
.
template
.
Variables
{
...
@@ -268,10 +276,3 @@ func (c *Core) init() error {
...
@@ -268,10 +276,3 @@ func (c *Core) init() error {
return
nil
return
nil
}
}
func
(
c
*
Core
)
context
()
*
interpolate
.
Context
{
return
&
interpolate
.
Context
{
TemplatePath
:
c
.
template
.
Path
,
UserVariables
:
c
.
variables
,
}
}
template/interpolate/render_test.go
View file @
579264bb
...
@@ -5,6 +5,47 @@ import (
...
@@ -5,6 +5,47 @@ import (
"testing"
"testing"
)
)
func
TestRenderInterface
(
t
*
testing
.
T
)
{
type
Test
struct
{
Foo
string
}
cases
:=
map
[
string
]
struct
{
Input
interface
{}
Output
interface
{}
}{
"basic"
:
{
map
[
string
]
interface
{}{
"foo"
:
"{{upper `bar`}}"
,
},
map
[
string
]
interface
{}{
"foo"
:
"BAR"
,
},
},
"struct"
:
{
&
Test
{
Foo
:
"{{upper `bar`}}"
,
},
&
Test
{
Foo
:
"BAR"
,
},
},
}
ctx
:=
&
Context
{}
for
k
,
tc
:=
range
cases
{
actual
,
err
:=
RenderInterface
(
tc
.
Input
,
ctx
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s
\n\n
%s"
,
k
,
err
)
}
if
!
reflect
.
DeepEqual
(
actual
,
tc
.
Output
)
{
t
.
Fatalf
(
"err: %s
\n\n
%#v
\n\n
%#v"
,
k
,
actual
,
tc
.
Output
)
}
}
}
func
TestRenderMap
(
t
*
testing
.
T
)
{
func
TestRenderMap
(
t
*
testing
.
T
)
{
cases
:=
map
[
string
]
struct
{
cases
:=
map
[
string
]
struct
{
Input
interface
{}
Input
interface
{}
...
...
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