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
89be12ae
Commit
89be12ae
authored
Aug 11, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Add MachineReadableUi
parent
edc59499
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
0 deletions
+85
-0
packer/ui.go
packer/ui.go
+46
-0
packer/ui_test.go
packer/ui_test.go
+39
-0
No files found.
packer/ui.go
View file @
89be12ae
...
...
@@ -11,6 +11,7 @@ import (
"runtime"
"strings"
"sync"
"time"
"unicode"
)
...
...
@@ -63,6 +64,12 @@ type BasicUi struct {
interrupted
bool
}
// MachineReadableUi is a UI that only outputs machine-readable output
// to the given Writer.
type
MachineReadableUi
struct
{
Writer
io
.
Writer
}
func
(
u
*
ColoredUi
)
Ask
(
query
string
)
(
string
,
error
)
{
return
u
.
Ui
.
Ask
(
u
.
colorize
(
query
,
u
.
Color
,
true
))
}
...
...
@@ -232,3 +239,42 @@ func (rw *BasicUi) Error(message string) {
func
(
rw
*
BasicUi
)
Machine
(
t
string
,
args
...
string
)
{
log
.
Printf
(
"machine readable: %s %#v"
,
t
,
args
)
}
func
(
u
*
MachineReadableUi
)
Ask
(
query
string
)
(
string
,
error
)
{
return
""
,
errors
.
New
(
"machine-readable UI can't ask"
)
}
func
(
u
*
MachineReadableUi
)
Say
(
message
string
)
{
u
.
Machine
(
"ui"
,
"say"
,
message
)
}
func
(
u
*
MachineReadableUi
)
Message
(
message
string
)
{
u
.
Machine
(
"ui"
,
"message"
,
message
)
}
func
(
u
*
MachineReadableUi
)
Error
(
message
string
)
{
u
.
Machine
(
"ui"
,
"error"
,
message
)
}
func
(
u
*
MachineReadableUi
)
Machine
(
category
string
,
args
...
string
)
{
now
:=
time
.
Now
()
.
UTC
()
// Determine if we have a target, and set it
target
:=
""
commaIdx
:=
strings
.
Index
(
category
,
","
)
if
commaIdx
>
-
1
{
target
=
category
[
0
:
commaIdx
]
category
=
category
[
commaIdx
+
1
:
]
}
// Prepare the args
for
i
,
v
:=
range
args
{
args
[
i
]
=
strings
.
Replace
(
v
,
","
,
"%!(PACKER_COMMA)"
,
-
1
)
}
argsString
:=
strings
.
Join
(
args
,
","
)
_
,
err
:=
fmt
.
Fprintf
(
u
.
Writer
,
"%d,%s,%s,%s"
,
now
.
Unix
(),
target
,
category
,
argsString
)
if
err
!=
nil
{
panic
(
err
)
}
}
packer/ui_test.go
View file @
89be12ae
...
...
@@ -3,6 +3,7 @@ package packer
import
(
"bytes"
"cgl.tideland.biz/asserts"
"strings"
"testing"
)
...
...
@@ -106,6 +107,44 @@ func TestBasicUi_Say(t *testing.T) {
assert
.
Equal
(
readWriter
(
bufferUi
),
"5
\n
"
,
"formatting"
)
}
func
TestMachineReadableUi_ImplUi
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
&
MachineReadableUi
{}
if
_
,
ok
:=
raw
.
(
Ui
);
!
ok
{
t
.
Fatalf
(
"MachineReadableUi must implement Ui"
)
}
}
func
TestMachineReadableUi
(
t
*
testing
.
T
)
{
var
data
,
expected
string
buf
:=
new
(
bytes
.
Buffer
)
ui
:=
&
MachineReadableUi
{
Writer
:
buf
}
ui
.
Machine
(
"foo"
,
"bar"
,
"baz"
)
data
=
strings
.
SplitN
(
buf
.
String
(),
","
,
2
)[
1
]
expected
=
",foo,bar,baz"
if
data
!=
expected
{
t
.
Fatalf
(
"bad: %s"
,
data
)
}
buf
.
Reset
()
ui
.
Machine
(
"mitchellh,foo"
,
"bar"
,
"baz"
)
data
=
strings
.
SplitN
(
buf
.
String
(),
","
,
2
)[
1
]
expected
=
"mitchellh,foo,bar,baz"
if
data
!=
expected
{
t
.
Fatalf
(
"bad: %s"
,
data
)
}
buf
.
Reset
()
ui
.
Machine
(
"foo"
,
"foo,bar"
)
data
=
strings
.
SplitN
(
buf
.
String
(),
","
,
2
)[
1
]
expected
=
",foo,foo%!(PACKER_COMMA)bar"
if
data
!=
expected
{
t
.
Fatalf
(
"bad: %s"
,
data
)
}
}
// This reads the output from the bytes.Buffer in our test object
// and then resets the buffer.
func
readWriter
(
ui
*
BasicUi
)
(
result
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