-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcmd.go
80 lines (63 loc) · 1.52 KB
/
cmd.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
// Unfancy resources embedding with Go.
package main
import (
"flag"
"log"
"os"
"path/filepath"
"strings"
"time"
)
var (
pkgName = "main"
varName = "FS"
tag = ""
out = ""
trimPath = ""
)
type nope struct{}
func main() {
t0 := time.Now()
flag.StringVar(&pkgName, "package", pkgName, "`name` of the package to generate")
flag.StringVar(&varName, "var", varName, "`name` of the variable to assign the virtual filesystem to")
flag.StringVar(&tag, "tag", tag, "`tag` to use for the generated package (default no tag)")
flag.StringVar(&out, "output", out, "`filename` to write the output to")
flag.StringVar(&trimPath, "trim", trimPath, "path `prefix` to remove from the resulting file path in the virtual filesystem")
flag.Parse()
if out == "" {
flag.PrintDefaults()
log.Fatal("-output is required.")
}
config := Config{
Pkg: pkgName,
Var: varName,
Tag: tag,
}
res := New()
res.Config = config
files := make(map[string]nope)
for _, g := range flag.Args() {
matches, err := filepath.Glob(g)
if err != nil {
log.Fatal(err)
}
for _, m := range matches {
info, err := os.Stat(m)
if !os.IsNotExist(err) && !info.IsDir() {
files[m] = nope{}
}
}
}
for path := range files {
name := filepath.ToSlash(path)
name = strings.TrimPrefix(name, trimPath)
err := res.AddFile(name, path)
if err != nil {
log.Fatal(err)
}
}
if err := res.Write(out); err != nil {
log.Fatal(err)
}
log.Printf("Finished in %v. Wrote %d resources to %s", time.Since(t0), len(files), out)
}