Skip to content

Commit

Permalink
storage refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Apr 17, 2017
1 parent ccad708 commit 47430ba
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 84 deletions.
14 changes: 12 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,18 @@ func Render(c Componer) {
}
}

// Storage returns the app storage directories.
func Storage() Storer {
// Resources returns the location of the resources directory.
// resources directory should contains file required by the UI.
// Its path should be used only for read only operations, otherwise it could
// mess up with the app signature.
func Resources() string {
return driver.Resources()
}

// Storage returns the location of the app storage directory.
// Content generated (e.g. sqlite db) or downloaded (e.g. images, music)
// should be saved in this directory.
func Storage() string {
return driver.Storage()
}

Expand Down
6 changes: 2 additions & 4 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ func TestDock(t *testing.T) {
}

func TestStorage(t *testing.T) {
t.Log(Storage().Resources())
t.Log(Storage().CSS())
t.Log(Storage().JS())
t.Log(Storage().Default())
t.Log(Storage())
t.Log(Resources())
}
8 changes: 7 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ type Driver interface {
// - Should be created in the driver implementation.
Dock() (d Docker, ok bool)

Storage() Storer
// Resources returns the location of the resources directory.
Resources() string

// Storage returns the location of the app storage directory.
Storage() string

// JavascriptBridge is the javascript function to call when a driver want to
// pass data to the native platform.
JavascriptBridge() string
}

Expand Down
8 changes: 6 additions & 2 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ func (d *AbstractDriver) Dock() (dock Docker, ok bool) {
return
}

func (d *AbstractDriver) Storage() Storer {
return StorageTest("")
func (d *AbstractDriver) Resources() string {
return "resources"
}

func (d *AbstractDriver) Storage() string {
return ""
}

func (d *AbstractDriver) JavascriptBridge() string {
Expand Down
54 changes: 21 additions & 33 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,52 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/murlokswarm/log"
)

// Storer describes the directory locations to use during app lifecycle.
type Storer interface {
// Resources returns resources directory filename.
// Represents the root location where files related to the operation of
// the app should be located.
Resources() string

// CSS returns the location where the .css files should be located.
CSS() string

// JS returns the location where the .js files should be located.
JS() string

// Storage returns the root location where common files should be stored.
// eg db, cache, downloaded content.
Default() string
}

// IsSupportedExtension returns a boolean indicating whether or not extensions
// contains the extension of name.
// extensions must contain the dot prefix. eg ".png".
func IsSupportedExtension(name string, extensions ...string) bool {
// FileHaveExtension returns a boolean indicating whether or not name have an
// extension defined in exts.
func FileHaveExtension(name string, exts ...string) bool {
ext := filepath.Ext(name)
for _, e := range extensions {
for _, e := range exts {
if ext == e {
return true
}
}
return false
}

// IsSupportedImageExtension returns a boolean indicating whether or not name
// is a .jpg, .jpeg or .png.
func IsSupportedImageExtension(name string) bool {
return IsSupportedExtension(name, ".jpg", ".jpeg", ".png")
// FileIsSupportedIcon returns a boolean indication whether or not name is a
// supported icon.
func FileIsSupportedIcon(name string) bool {
return FileHaveExtension(name, ".jpg", ".jpeg", ".png")
}

// GetFilenamesWithExtensionsFromDir returns the filenames of files within
// dirname. names are not prefixed with dirname.
func GetFilenamesWithExtensionsFromDir(dirname string, extension ...string) (names []string, err error) {
// GetFilenamesFromDir returns the filenames within dirname.
// names are not prefixed by dirname.
func GetFilenamesFromDir(dirname string, extension ...string) (names []string) {
info, err := os.Stat(dirname)
if err != nil {
return
}
if !info.IsDir() {
err = errors.Errorf("%v is not a directory", dirname)
log.Errorf("%v is not a directory", dirname)
return
}

files, _ := ioutil.ReadDir(dirname)
for _, f := range files {
if f.IsDir() {
subdirname := filepath.Join(dirname, f.Name())
subfilenames := GetFilenamesFromDir(subdirname, extension...)

for _, n := range subfilenames {
subfilename := filepath.Join(f.Name(), n)
names = append(names, subfilename)
}
continue
}
if IsSupportedExtension(f.Name(), extension...) {
if FileHaveExtension(f.Name(), extension...) {
names = append(names, f.Name())
}
}
Expand Down
63 changes: 21 additions & 42 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,29 @@ import (
"testing"
)

type StorageTest string

func (s StorageTest) Resources() string {
return "resources"
}

func (s StorageTest) CSS() string {
return filepath.Join(s.Resources(), "css")
}

func (s StorageTest) JS() string {
return filepath.Join(s.Resources(), "js")
}

func (s StorageTest) Default() string {
return filepath.Join(s.Resources(), "default")
}

func TestIsSupportedExtension(t *testing.T) {
if name, ext := "foo.jpg", ".jpg"; !IsSupportedExtension(name, ext) {
func TestFileHaveExtension(t *testing.T) {
if name, ext := "foo.jpg", ".jpg"; !FileHaveExtension(name, ext) {
t.Errorf("%v not found in %v", ext, name)
}
if name, ext := "foo.jpg", "jpg"; IsSupportedExtension(name, ext) {
if name, ext := "foo.jpg", "jpg"; FileHaveExtension(name, ext) {
t.Errorf("%v should not be a valid extension name", ext)
}
if name, ext := "foo.jpg", ".png"; IsSupportedExtension(name, ext) {
if name, ext := "foo.jpg", ".png"; FileHaveExtension(name, ext) {
t.Errorf("%v should have be found in %v", ext, name)
}
}

func TestIsSupportedImageExtension(t *testing.T) {
if name := "foo.jpg"; !IsSupportedImageExtension(name) {
func TestFileIsSupportedIcon(t *testing.T) {
if name := "foo.jpg"; !FileIsSupportedIcon(name) {
t.Errorf("%v should be supported", name)
}

if name := "foo.pnh"; IsSupportedImageExtension(name) {
if name := "foo.pnh"; FileIsSupportedIcon(name) {
t.Errorf("%v should not be supported", name)
}
}

func TestGetFilenamesWithExtensionsFromDir(t *testing.T) {
func TestGetFilenamesFromDir(t *testing.T) {
dirname := "testdir"
os.Mkdir(dirname, os.ModePerm)
os.Mkdir(filepath.Join(dirname, "foo"), os.ModePerm)
Expand All @@ -56,35 +38,32 @@ func TestGetFilenamesWithExtensionsFromDir(t *testing.T) {
filepath.Join(dirname, "hello.css"),
filepath.Join(dirname, "hello.js"),
filepath.Join(dirname, "hello.png"),
filepath.Join(dirname, "foo/hello.png"),
filepath.Join(dirname, "foo/hello.css"),
}
for _, name := range filenames {
f, _ := os.Create(name)
defer f.Close()
}

names, err := GetFilenamesWithExtensionsFromDir(dirname, ".png", ".css")
if err != nil {
t.Fatal(err)
}
if l := len(names); l != 2 {
t.Error("l should be 2:", l)
}
if name, exp := names[0], "hello.css"; name != exp {
t.Errorf("name should be %v: %v", exp, name)
}
if name, exp := names[1], "hello.png"; name != exp {
t.Errorf("name should be %v: %v", exp, name)
names := GetFilenamesFromDir(dirname, ".png", ".css")
if l := len(names); l != 4 {
t.Error("l should be 4:", l)
}
t.Log(names)

if _, err = GetFilenamesWithExtensionsFromDir("hello", ".jpg"); err == nil {
t.Error("err should not be nil")
// No files with ext.
if names = GetFilenamesFromDir("hello", ".jpg"); len(names) != 0 {
t.Fatal("name should be empty")
}

// No directory
name := "foofile"
f, _ := os.Create(name)
defer os.Remove(name)
defer f.Close()
if _, err = GetFilenamesWithExtensionsFromDir(name, ".jpg"); err == nil {
t.Error("err should not be nil")

if names = GetFilenamesFromDir(name, ".jpg"); len(names) != 0 {
t.Fatal("name should be empty")
}
}

0 comments on commit 47430ba

Please sign in to comment.