This repository has been archived by the owner on Nov 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
action_http.go
143 lines (129 loc) · 2.59 KB
/
action_http.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/bmatcuk/doublestar"
"github.com/urfave/cli"
"github.com/bradrydzewski/togo/template"
)
type (
httpParams struct {
Encode bool
Package string
Files []*httpFile
}
httpFile struct {
Base string
Name string
Path string
Ext string
Data string
Size int64
Time int64
Encoded bool
}
)
var httpCommand = cli.Command{
Name: "http",
Usage: "generate an http filesystem",
Action: httpAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "package",
Value: "http",
},
cli.StringFlag{
Name: "input",
Value: "files/**",
},
cli.StringFlag{
Name: "output",
Value: "http_gen.go",
},
cli.StringFlag{
Name: "exclude",
},
cli.StringFlag{
Name: "trim-prefix",
Value: "files",
},
cli.StringSliceFlag{
Name: "plain-text",
Value: &cli.StringSlice{"html", "js", "css"},
},
},
}
func httpAction(c *cli.Context) error {
pattern := c.Args().First()
if pattern == "" {
pattern = c.String("input")
}
matches, err := doublestar.Glob(pattern)
if err != nil {
return err
}
params := httpParams{
Encode: c.Bool("encode"),
Package: c.String("package"),
}
var (
prefix = c.String("trim-prefix")
exclude *regexp.Regexp
)
if s := c.String("exclude"); s != "" {
exclude = regexp.MustCompilePOSIX(s)
}
for _, match := range matches {
stat, oserr := os.Stat(match)
if oserr != nil {
return oserr
}
if stat.IsDir() {
continue
}
if exclude != nil && exclude.MatchString(match) {
continue
}
raw, ioerr := ioutil.ReadFile(match)
if ioerr != nil {
return ioerr
}
encoded := true
switch {
case strings.HasSuffix(match, ".min.js"):
case strings.HasSuffix(match, ".min.css"):
case strings.HasSuffix(match, ".css"):
encoded = false
case strings.HasSuffix(match, ".js"):
encoded = false
case strings.HasSuffix(match, ".html"):
encoded = false
}
data := string(raw)
if !encoded {
data = strings.Replace(data, "`", "`+\"`\"+`", -1)
}
params.Files = append(params.Files, &httpFile{
Path: strings.TrimPrefix(match, prefix),
Name: filepath.Base(match),
Base: strings.TrimSuffix(filepath.Base(match), filepath.Ext(match)),
Ext: filepath.Ext(match),
Data: data,
Time: stat.ModTime().Unix(),
Size: stat.Size(),
Encoded: encoded,
})
}
wr := os.Stdout
if output := c.String("output"); output != "" {
wr, err = os.Create(output)
if err != nil {
return err
}
defer wr.Close()
}
return template.Execute(wr, "http.tmpl", ¶ms)
}