Skip to content

Commit

Permalink
1) Improve Local Source 2) Improve Must Getters
Browse files Browse the repository at this point in the history
  • Loading branch information
moisespsena committed Apr 15, 2019
1 parent 8bcf230 commit d0accd0
Show file tree
Hide file tree
Showing 28 changed files with 674 additions and 432 deletions.
13 changes: 11 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 6 additions & 25 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package xbindata
import (
"crypto/sha256"
"fmt"
"io"
"os"
"time"

"github.com/go-errors/errors"
"github.com/moisespsena-go/xbindata/digest"

"github.com/djherbis/times"
)
Expand Down Expand Up @@ -87,32 +86,14 @@ func (a *Asset) SourceCode(c *Config, start int64) (code string, err error) {
a.Name, info, readerFunc, *digest), nil
}

func (a *Asset) Digest() (digest *[sha256.Size]byte, err error) {
func (a *Asset) Digest() (dig *[sha256.Size]byte, err error) {
if a.digest != nil {
return a.digest, nil
}
var fi os.FileInfo
if fi, err = a.Info(); err != nil {
return nil, err
}

f, err := os.Open(a.Path)
if err != nil {
return nil, err
}

defer f.Close()

h := sha256.New()
var n int64
if n, err = io.Copy(h, f); err != nil {
return nil, err
}
if n != fi.Size() {
return nil, errors.New("Writed size is not eq to file size.")
if dig, err = digest.Digest(a.Path); err != nil {
return
}
var d [sha256.Size]byte
copy(d[:], h.Sum(nil))
a.digest = &d
return a.digest, nil
a.digest = dig
return
}
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ type Config struct {
NoStore bool

OulinedSkipApi bool

FileSystemLoadCallbacks []string
}

// NewConfig returns a default configuration struct.
Expand Down
74 changes: 53 additions & 21 deletions config_many.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package xbindata

import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"

"github.com/moisespsena-go/path-helpers"

"github.com/mitchellh/mapstructure"

"github.com/gobwas/glob"
Expand Down Expand Up @@ -84,19 +88,22 @@ func (i ManyConfigInput) Config() (c *InputConfig, err error) {
}

type ManyConfigCommon struct {
NoAutoLoad bool `mapstructure:"no_auto_load" yaml:"no_auto_load"`
NoCompress bool `mapstructure:"no_compress" yaml:"no_compress"`
NoMetadata bool `mapstructure:"no_metadata" yaml:"no_metadata"`
Mode uint
ModTime int64 `mapstructure:"mod_time" yaml:"mod_time"`
Ignore IgnoreSlice
IgnoreGlob IgnoreGlobSlice `mapstructure:"ignore_glob" yaml:"ignore_glob"`
Inputs ManyConfigInputSlice
Pkg string
Output string
Prefix string
Fs bool
Hybrid bool
Disabled bool
NoAutoLoad bool `mapstructure:"no_auto_load" yaml:"no_auto_load"`
NoCompress bool `mapstructure:"no_compress" yaml:"no_compress"`
NoMetadata bool `mapstructure:"no_metadata" yaml:"no_metadata"`
NoMemCopy bool `mapstructure:"no_mem_copy" yaml:"no_mem_copy"`
Mode uint
ModTime int64 `mapstructure:"mod_time" yaml:"mod_time"`
Ignore IgnoreSlice
IgnoreGlob IgnoreGlobSlice `mapstructure:"ignore_glob" yaml:"ignore_glob"`
Inputs ManyConfigInputSlice
Pkg string
Output string
Prefix string
Hybrid bool
Fs bool
FsLoadCallbacks []string `mapstructure:"fs_load_callbacks" yaml:"fs_load_callbacks"`
}

func (a *ManyConfigCommon) Validate() (err error) {
Expand Down Expand Up @@ -132,9 +139,11 @@ func (a *ManyConfigCommon) Config() (c *Config, err error) {
c = NewConfig()
c.Package = a.Pkg
c.FileSystem = a.Fs
c.FileSystemLoadCallbacks = a.FsLoadCallbacks
c.NoAutoLoad = a.NoAutoLoad
c.NoCompress = a.NoCompress
c.NoMetadata = a.NoMetadata
c.NoMemCopy = a.NoMemCopy
c.Mode = a.Mode
c.ModTime = a.ModTime
c.Prefix = a.Prefix
Expand All @@ -144,6 +153,18 @@ func (a *ManyConfigCommon) Config() (c *Config, err error) {
c.Output = a.Output
}

if len(a.FsLoadCallbacks) > 0 {
var cwd, _ = os.Getwd()
gph := path_helpers.PkgFromPath(cwd)

for i, cb := range a.FsLoadCallbacks {
if cb[0] == '.' {
cb = path.Join(gph, cb[1:])
c.FileSystemLoadCallbacks[i] = cb
}
}
}

if c.IgnoreGlob, err = a.IgnoreGlob.Items(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -262,25 +283,36 @@ func (c *ManyConfig) InputsRelTo(baseDir string) {
}

func (c *ManyConfig) Validate() (err error) {
var hasOutlinedEmbeded bool
for i, o := range c.Outlined {
if o.Program {
var (
hasOutlinedEmbeded bool
embedded []ManyConfigEmbedded
outlined []ManyConfigOutlined
)
for i, cfg := range c.Outlined {
if cfg.Disabled {
continue
}
if cfg.Program {
if hasOutlinedEmbeded {
return fmt.Errorf("multiples outlined with embeded enabled")
}
hasOutlinedEmbeded = true
}
if err = o.Validate(); err != nil {
if err = cfg.Validate(); err != nil {
return fmt.Errorf("Outlined #d validate failed: %v", i, err)
}
c.Outlined[i] = o
outlined = append(outlined, cfg)
}
for i, e := range c.Embedded {
if err = e.Validate(); err != nil {
for i, cfg := range c.Embedded {
if cfg.Disabled {
continue
}
if err = cfg.Validate(); err != nil {
return fmt.Errorf("Embeded #d validate failed: %v", i, err)
}
c.Embedded[i] = e
embedded = append(embedded, cfg)
}
c.Outlined, c.Embedded = outlined, embedded
return nil
}

Expand Down
34 changes: 34 additions & 0 deletions digest/digest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package digest

import (
"crypto/sha256"
"errors"
"io"
"os"
)

func Digest(pth string) (digest *[sha256.Size]byte, err error) {
f, err := os.Open(pth)
if err != nil {
return nil, err
}

defer f.Close()

info, err := f.Stat()
if err != nil {
return nil, err
}

h := sha256.New()
var n int64
if n, err = io.Copy(h, f); err != nil {
return nil, err
}
if n != info.Size() {
return nil, errors.New("Digest of `" + pth + "` failed: readed size is not eq to file size.")
}
var d [sha256.Size]byte
copy(d[:], h.Sum(nil))
return &d, nil
}
23 changes: 18 additions & 5 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,36 @@ func write_imports(w io.Writer, c *Config, imports ...string) (err error) {
if c.FileSystem {
imports = append(
imports,
`fs "github.com/moisespsena-go/xbindata/xbfs"`,
`"github.com/moisespsena-go/xbindata/xbfs"`,
`fsapi "github.com/moisespsena-go/assetfs/assetfsapi"`,
)
}

var (
importsMap = map[string]bool{}
newImports []string
importsMap = map[string]bool{}
newImports, excludes []string
)

for _, imp := range imports {
if _, ok := importsMap[imp]; !ok {
newImports = append(newImports, imp)
importsMap[imp] = true
if imp[0] == '-' {
excludes = append(excludes, imp[0:])
} else {
importsMap[imp] = true
}
}
}

for _, imp := range excludes {
if _, ok := importsMap[imp]; ok {
delete(importsMap, imp)
}
}

for imp, _ := range importsMap {
newImports = append(newImports, imp)
}

imports = newImports

sort.Slice(imports, func(i, j int) bool {
Expand Down
Loading

0 comments on commit d0accd0

Please sign in to comment.