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
4d10489f
Commit
4d10489f
authored
Mar 24, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement the help method
parent
91c524c7
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
2 deletions
+44
-2
packer/environment.go
packer/environment.go
+35
-2
packer/environment_test.go
packer/environment_test.go
+5
-0
packer/version.go
packer/version.go
+4
-0
No files found.
packer/environment.go
View file @
4d10489f
// The packer package contains the core components of Packer.
// The packer package contains the core components of Packer.
package
packer
package
packer
import
"os"
import
(
"fmt"
"os"
"sort"
"strings"
)
// A command is a runnable sub-command of the `packer` application.
// A command is a runnable sub-command of the `packer` application.
// When `packer` is called with the proper subcommand, this will be
// When `packer` is called with the proper subcommand, this will be
...
@@ -11,6 +16,7 @@ import "os"
...
@@ -11,6 +16,7 @@ import "os"
// Environment struct.
// Environment struct.
type
Command
interface
{
type
Command
interface
{
Run
(
env
*
Environment
,
args
[]
string
)
int
Run
(
env
*
Environment
,
args
[]
string
)
int
Synopsis
()
string
}
}
// The environment struct contains all the state necessary for a single
// The environment struct contains all the state necessary for a single
...
@@ -71,5 +77,32 @@ func (e *Environment) Ui() Ui {
...
@@ -71,5 +77,32 @@ func (e *Environment) Ui() Ui {
// Prints the CLI help to the UI.
// Prints the CLI help to the UI.
func
(
e
*
Environment
)
PrintHelp
()
{
func
(
e
*
Environment
)
PrintHelp
()
{
e
.
ui
.
Say
(
"Bad.
\n
"
)
// Created a sorted slice of the map keys and record the longest
// command name so we can better format the output later.
commandKeys
:=
make
([]
string
,
len
(
e
.
command
))
i
:=
0
maxKeyLen
:=
0
for
key
,
_
:=
range
e
.
command
{
commandKeys
[
i
]
=
key
if
len
(
key
)
>
maxKeyLen
{
maxKeyLen
=
len
(
key
)
}
i
++
}
// Sort the keys
sort
.
Strings
(
commandKeys
)
e
.
ui
.
Say
(
"usage: packer [--version] [--help] <command> [<args>]
\n\n
"
)
e
.
ui
.
Say
(
"Available commands are:
\n
"
)
for
_
,
key
:=
range
commandKeys
{
command
:=
e
.
command
[
key
]
// Pad the key with spaces so that they're all the same width
key
=
fmt
.
Sprintf
(
"%v%v"
,
key
,
strings
.
Repeat
(
" "
,
maxKeyLen
-
len
(
key
)))
// Output the command and the synopsis
e
.
ui
.
Say
(
" %v %v
\n
"
,
key
,
command
.
Synopsis
())
}
}
}
packer/environment_test.go
View file @
4d10489f
...
@@ -43,3 +43,8 @@ func TestEnvironment_DefaultUi(t *testing.T) {
...
@@ -43,3 +43,8 @@ func TestEnvironment_DefaultUi(t *testing.T) {
assert
.
Equal
(
rwUi
.
Writer
,
os
.
Stdout
,
"default UI should go to stdout"
)
assert
.
Equal
(
rwUi
.
Writer
,
os
.
Stdout
,
"default UI should go to stdout"
)
assert
.
Equal
(
rwUi
.
Reader
,
os
.
Stdin
,
"default UI should read from stdin"
)
assert
.
Equal
(
rwUi
.
Reader
,
os
.
Stdin
,
"default UI should read from stdin"
)
}
}
func
TestEnvironment_PrintHelp
(
t
*
testing
.
T
)
{
// Just call the function and verify that no panics occur
NewEnvironment
()
.
PrintHelp
()
}
packer/version.go
View file @
4d10489f
...
@@ -10,3 +10,7 @@ func (versionCommand) Run(env *Environment, args []string) int {
...
@@ -10,3 +10,7 @@ func (versionCommand) Run(env *Environment, args []string) int {
env
.
Ui
()
.
Say
(
"Packer v%v
\n
"
,
Version
)
env
.
Ui
()
.
Say
(
"Packer v%v
\n
"
,
Version
)
return
0
return
0
}
}
func
(
versionCommand
)
Synopsis
()
string
{
return
"print Packer version"
}
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