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
b11004b9
Commit
b11004b9
authored
Aug 13, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: ParseTemplateFile understands "-" to mean stdin
parent
588623b0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
3 deletions
+54
-3
packer/template.go
packer/template.go
+20
-3
packer/template_test.go
packer/template_test.go
+34
-0
No files found.
packer/template.go
View file @
b11004b9
package
packer
import
(
"bytes"
"fmt"
"github.com/mitchellh/mapstructure"
jsonutil
"github.com/mitchellh/packer/common/json"
"io"
"io/ioutil"
"os"
"sort"
)
...
...
@@ -235,9 +238,23 @@ func ParseTemplate(data []byte) (t *Template, err error) {
// ParseTemplateFile takes the given template file and parses it into
// a single template.
func
ParseTemplateFile
(
path
string
)
(
*
Template
,
error
)
{
data
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
return
nil
,
err
var
data
[]
byte
if
path
==
"-"
{
// Read from stdin...
buf
:=
new
(
bytes
.
Buffer
)
_
,
err
:=
io
.
Copy
(
buf
,
os
.
Stdin
)
if
err
!=
nil
{
return
nil
,
err
}
data
=
buf
.
Bytes
()
}
else
{
var
err
error
data
,
err
=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
}
return
ParseTemplate
(
data
)
...
...
packer/template_test.go
View file @
b11004b9
...
...
@@ -3,6 +3,7 @@ package packer
import
(
"cgl.tideland.biz/asserts"
"io/ioutil"
"os"
"reflect"
"sort"
"testing"
...
...
@@ -43,6 +44,39 @@ func TestParseTemplateFile_basic(t *testing.T) {
}
}
func
TestParseTemplateFile_stdin
(
t
*
testing
.
T
)
{
data
:=
`
{
"builders": [{"type": "something"}]
}
`
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
defer
tf
.
Close
()
tf
.
Write
([]
byte
(
data
))
// Sync and seek to the beginning so that we can re-read the contents
tf
.
Sync
()
tf
.
Seek
(
0
,
0
)
// Set stdin to something we control
oldStdin
:=
os
.
Stdin
defer
func
()
{
os
.
Stdin
=
oldStdin
}()
os
.
Stdin
=
tf
result
,
err
:=
ParseTemplateFile
(
"-"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
len
(
result
.
Builders
)
!=
1
{
t
.
Fatalf
(
"bad: %#v"
,
result
.
Builders
)
}
}
func
TestParseTemplate_Basic
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
...
...
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