Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: small tweaks for namespace functionality. #101

Merged
merged 1 commit into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
statik
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ go_import_path: github.com/rakyll/statik

install:
- go build -v
- ./statik -f -src=./example/public -dest=./example/ -include=*.jpg,*.txt,*.html,*.css,*.js
- ./statik -f -src=./example/public -dest=./example/ -include=*.jpg,*.txt,*.html,*.css,*.js -ns=web

script:
- go test -v -bench=. ./...
6 changes: 3 additions & 3 deletions example/statik/statik.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ type statikFS struct {
// Register registers zip contents data, later used to initialize
// the statik file system.
func Register(data string) {
RegisterWithName("default", data)
RegisterWithNamespace("default", data)
}

// RegisterWithName registers zip contents data and set asset namespace,
// RegisterWithNamespace registers zip contents data and set asset namespace,
// later used to initialize the statik file system.
func RegisterWithName(assetNamespace string, data string) {
func RegisterWithNamespace(assetNamespace string, data string) {
zipData[assetNamespace] = data
}

// New creates a new file system with the default registered zip contents data.
// It unzips all files and stores them in an in-memory map.
func New() (http.FileSystem, error) {
return NewWithName("default")
return NewWithNamespace("default")
}

// NewWithName creates a new file system with the registered zip contents data.
// NewWithNamespace creates a new file system with the registered zip contents data.
// It unzips all files and stores them in an in-memory map.
func NewWithName(assetNamespace string) (http.FileSystem, error) {
func NewWithNamespace(assetNamespace string) (http.FileSystem, error) {
asset, ok := zipData[assetNamespace]
if !ok {
return nil, errors.New("statik/fs: no zip data registered")
Expand Down
10 changes: 5 additions & 5 deletions fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ type wantFile struct {
err error
}

func TestRegisterWithName(t *testing.T) {
func TestRegisterWithNamespace(t *testing.T) {
tests := []struct {
description string
assetName string
zipData string
condition func() error
}{
{
description: "RegisterWithName() should set zipData with assetName to be key",
description: "RegisterWithNamespace() should set zipData with assetName to be key",
assetName: "file",
zipData: "file test",
condition: func() error {
Expand Down Expand Up @@ -78,11 +78,11 @@ func TestRegisterWithName(t *testing.T) {
},
},
{
description: "zipData[\"foo\"] should be able to open by OpenWithName(\"foo\")",
description: "zipData[\"foo\"] should be able to open by Open()",
assetName: "foo",
zipData: mustZipTree("../testdata/file"),
condition: func() error {
fs, err := NewWithName("foo")
fs, err := NewWithNamespace("foo")
if err != nil {
return err
}
Expand All @@ -95,7 +95,7 @@ func TestRegisterWithName(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
RegisterWithName(tc.assetName, tc.zipData)
RegisterWithNamespace(tc.assetName, tc.zipData)
if err := tc.condition(); err != nil {
t.Error(err)
}
Expand Down
14 changes: 7 additions & 7 deletions statik.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ var (
flagForce = flag.Bool("f", false, "")
flagTags = flag.String("tags", "", "")
flagPkg = flag.String("p", "statik", "")
flagAstName = flag.String("a", "default", "")
flagPkgCmt = flag.String("c", "", "")
flagNamespace = flag.String("ns", "default", "")
flagPkgCmt = flag.String("c", "", "")
flagInclude = flag.String("include", "*.*", "")
)

const helpText = `statik [options]

Options:
-src The source directory of the assets. "public" by default.
-dest The destination directory of the generated package. "." by default.
-src The source directory of the assets, "public" by default.
-dest The destination directory of the generated package, "." by default.

-a Support different asset namespaces
-ns The namespace where assets will exist, "default" by default.
-f Override destination if it already exists, false by default.
-include Wildcard to filter files to include, "*.*" by default.
-m Ignore modification times on files, false by default.
Expand Down Expand Up @@ -265,7 +265,7 @@ func generateSource(srcPath string, includes string) (file *os.File, err error)
// e.g.)
// assetNamespaceIdentify is "AbcDeF_G"
// when assetNamespace is "abc de f-g"
assetNamespace := *flagAstName
assetNamespace := *flagNamespace
assetNamespaceIdentify := toSymbolSafe(assetNamespace)

// then embed it as a quoted string
Expand All @@ -289,7 +289,7 @@ func init() {
data := "`)
FprintZipData(&qb, buffer.Bytes())
fmt.Fprintf(&qb, `"
fs.RegisterWithName("%s", data)
fs.RegisterWithNamespace("%s", data)
}
`, assetNamespace)

Expand Down