This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
121 lines (97 loc) · 2.95 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
120
121
// Copyright 2014 Paul Hammond.
// This software is licensed under the MIT license, see LICENSE.txt for details.
/*
Stringfs encodes a net/http FileSystem into a string.
Most web applications require a number of static assets such as images,
CSS stylesheets etc. Stringfs provides a way to compile those assets into a
single application binary for easier deployment.
Usage:
stringfs [flags] directory outputfile
Stringfs takes a directory and an output file. It encodes all files within the
directory into a string in specified output source file.
The flags are:
-var
The variable the encoded string will be assigned to. Defaults to
"fileSystem"
-pkg
The package this file will be part of. If not specified stringfs will
autodetect the package name by checking other files in the same
directory.
The file created by stringfs does not declare the variable, it just assigns a
value. You are expected to declare the variable yourself in another file,
giving you control over initialization.
A typical usage is to declare the variable with a fallback value. This allows
you to remove the generated file during development, avoiding an extra
compilation step. For example:
var assets http.FileSystem = http.Dir("./assets")
Alternatively you can initialize the variable and later check for the nil
value. For example:
// at the top level
var assets http.FileSystem
// inside a function
if assets == nil {
// working with precompiled assets
} else {
// no precompiled assets
}
*/
package main
import (
"bytes"
"flag"
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/paulhammond/stringfs/fs"
)
func check(err error) {
if err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
}
func findPackage(dir string) string {
pkgs, err := parser.ParseDir(token.NewFileSet(), dir, nil, parser.PackageClauseOnly)
check(err)
if len(pkgs) != 1 {
fmt.Println("Could not autodetect package name, try -pkg flag")
os.Exit(1)
}
for pkg, _ := range pkgs {
return pkg
}
panic("unreachable")
}
func main() {
var varName = flag.String("var", "fileSystem", "Variable Name")
var pkgName = flag.String("pkg", "", "Package Name")
flag.Parse()
logger := log.New(os.Stdout, "", 0)
args := flag.Args()
src := args[0]
dest := args[1]
destDir := filepath.Dir(dest)
if *pkgName == "" {
*pkgName = findPackage(destDir)
}
s, err := fs.CreateString(http.Dir(src), logger)
check(err)
code := new(bytes.Buffer)
fmt.Fprintf(code, "// generated with `%v`\n\n", strings.Join(os.Args, " "))
fmt.Fprintf(code, "package %s\n\n", *pkgName)
fmt.Fprintf(code, "import \"github.com/paulhammond/stringfs/fs\"\n\n")
fmt.Fprintf(code, "func init() {\n")
fmt.Fprintf(code, "\t%s = fs.Must(fs.New(%q))\n", *varName, s)
fmt.Fprintf(code, "}\n")
err = os.MkdirAll(destDir, 0777)
check(err)
err = ioutil.WriteFile(dest, code.Bytes(), 0666)
check(err)
logger.Printf("Wrote %s", dest)
}