added verbose flag
Mark Bates
6 years ago
115 | 115 |
|
116 | 116 |
When it comes time to build, or install, your Go binary, simply use `packr build` or `packr install` just as you would `go build` or `go install`. All flags for the `go` tool are supported and everything works the way you expect, the only difference is your static assets are now bundled in the generated binary. If you want more control over how this happens, looking at the following section on building binaries (the hard way).
|
117 | 117 |
|
118 | |
### Building a Binary (the hard way)
|
|
118 |
## Building a Binary (the hard way)
|
119 | 119 |
|
120 | 120 |
Before you build your Go binary, run the `packr` command first. It will look for all the boxes in your code and then generate `.go` files that pack the static files into bytes that can be bundled into the Go binary.
|
121 | 121 |
|
122 | 122 |
```
|
123 | 123 |
$ packr
|
124 | |
--> packing foo/foo-packr.go
|
125 | |
--> packing example-packr.go
|
126 | 124 |
```
|
127 | 125 |
|
128 | 126 |
Then run your `go build command` like normal.
|
|
135 | 133 |
|
136 | 134 |
```
|
137 | 135 |
$ packr clean
|
138 | |
----> cleaning up example-packr.go
|
139 | |
----> cleaning up foo/foo-packr.go
|
140 | 136 |
```
|
141 | 137 |
|
142 | 138 |
Why do you want to do this? Packr first looks to the information stored in these generated files, if the information isn't there it looks to disk. This makes it easy to work with in development.
|
|
139 |
|
|
140 |
---
|
|
141 |
|
|
142 |
## Debugging
|
|
143 |
|
|
144 |
The `packr` command passes all arguments down to the underlying `go` command, this includes the `-v` flag to print out `go build` information. Packr looks for the `-v` flag, and will turn on its own verbose logging. This is very useful for trying to understand what the `packr` command is doing when it is run.
|
10 | 10 |
|
11 | 11 |
var input string
|
12 | 12 |
var compress bool
|
|
13 |
var verbose bool
|
13 | 14 |
|
14 | 15 |
var rootCmd = &cobra.Command{
|
15 | 16 |
Use: "packr",
|
16 | 17 |
Short: "compiles static files into Go files",
|
17 | 18 |
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
18 | |
for _, a := range args {
|
19 | |
if a == "-v" {
|
20 | |
builder.DebugLog = func(s string, a ...interface{}) {
|
21 | |
os.Stdout.WriteString(fmt.Sprintf(s, a...))
|
|
19 |
if !verbose {
|
|
20 |
for _, a := range args {
|
|
21 |
if a == "-v" {
|
|
22 |
verbose = true
|
|
23 |
break
|
22 | 24 |
}
|
23 | |
break
|
|
25 |
}
|
|
26 |
}
|
|
27 |
|
|
28 |
if verbose {
|
|
29 |
builder.DebugLog = func(s string, a ...interface{}) {
|
|
30 |
os.Stdout.WriteString(fmt.Sprintf(s, a...))
|
24 | 31 |
}
|
25 | 32 |
}
|
26 | 33 |
return nil
|
|
36 | 43 |
pwd, _ := os.Getwd()
|
37 | 44 |
rootCmd.Flags().StringVarP(&input, "input", "i", pwd, "path to scan for packr Boxes")
|
38 | 45 |
rootCmd.Flags().BoolVarP(&compress, "compress", "z", false, "compress box contents")
|
|
46 |
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "print verbose logging information")
|
39 | 47 |
}
|
40 | 48 |
|
41 | 49 |
// Execute the commands
|