forked from paulvollmer/2gobytes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (109 loc) · 2.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Simple utility to convert a file into a Go byte array
// Clint Caywood
// Paul Vollmer
// http://github.com/paulvollmer/2gobytes
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/codegangsta/cli"
"github.com/paulvollmer/2gobytes/generator"
)
func main() {
// the commandline interface
app := cli.NewApp()
app.Name = generator.Name
app.Version = generator.Version
app.Author = generator.Name + " contributors"
app.Email = generator.URL + "/graphs/contributors"
app.Usage = "A simple utility to encode a file into a Go byte array"
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "input, i",
Usage: "path to the input file",
},
cli.StringFlag{
Name: "output, o",
Usage: "filepath to write to the generated source",
},
cli.Uint64Flag{
Name: "perm, P",
Value: 0644,
Usage: "file permission of the generated source",
},
cli.StringFlag{
Name: "package-name, p",
Value: "main",
Usage: "name of the package",
},
cli.StringFlag{
Name: "array-name, a",
Usage: "name of the byte array",
},
cli.BoolFlag{
Name: "no-index, I",
Usage: "generate no index map",
},
cli.StringFlag{
Name: "index",
Usage: "name of the index map variable",
},
cli.BoolFlag{
Name: "no-generated-info, ni",
Usage: "no 'generated by' info at the first code line",
},
cli.BoolFlag{
Name: "no-package, np",
Usage: "no 'package' code",
},
}
app.Action = cliAction
app.Run(os.Args)
}
func cliAction(c *cli.Context) {
// create generator object.
// here we store the generator parameter
code := generator.NewGenerator()
// get the cli flag values
input := c.StringSlice("input")
output := c.String("output")
perm := c.Uint64("perm")
code.PackageName = c.String("package-name")
varName := c.String("array-name")
code.Index = !c.Bool("no-index")
code.IndexName = c.String("index")
code.GenerateInfo = !c.Bool("no-generated-info")
code.GeneratePackage = !c.Bool("no-package")
// check if the input flag was set
if len(input) != 0 {
for i := 0; i < len(input); i++ {
// read the input file
code.AddFile(input[i], varName)
}
} else {
// read the input from stdin
if isTerminal() {
fmt.Print("\nPlease pipe the file you wish to encode into stdin or use the -input flag\n\n")
os.Exit(1)
}
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
code.AddData(varName, []byte(text))
}
// generate the code
genCode := code.GenerateCode()
// write or print generated code
if output != "" {
// fmt.Println("OK, write to file", output)
ioutil.WriteFile(output, genCode, os.FileMode(perm))
os.Exit(0)
}
fmt.Print(string(genCode))
}