From b0dc8ae47dc76a183765618fbece41cd433a8e2c Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Thu, 31 Jan 2019 09:30:08 -0500 Subject: [PATCH] prep for v2 (#149) --- box.go | 13 ++---- go.mod | 6 +-- go.sum | 16 ++++++- v2/box.go | 109 +++++++++++------------------------------------ v2/deprecated.go | 8 ++-- v2/dirs_map.go | 4 +- v2/go.mod | 2 +- v2/go.sum | 4 +- v2/helpers.go | 73 +++++++++++++++++++++++++++++++ v2/pointer.go | 4 ++ v2/version.go | 1 + v2/walk.go | 5 ++- 12 files changed, 135 insertions(+), 110 deletions(-) create mode 100644 v2/helpers.go diff --git a/box.go b/box.go index 0c9396e..58d43b0 100644 --- a/box.go +++ b/box.go @@ -12,7 +12,6 @@ import ( "strings" "github.com/gobuffalo/packd" - "github.com/markbates/oncer" "github.com/pkg/errors" ) @@ -74,29 +73,25 @@ func (b Box) AddBytes(path string, t []byte) error { return nil } -// String is deprecated. Use Find instead +// Deprecated: Use FindString instead. func (b Box) String(name string) string { - oncer.Deprecate(0, "github.com/gobuffalo/packr#Box.String", "Use github.com/gobuffalo/packr#Box.FindString instead.") bb, _ := b.FindString(name) return bb } -// MustString is deprecated. Use FindString instead +// Deprecated: Use FindString instead. func (b Box) MustString(name string) (string, error) { - oncer.Deprecate(0, "github.com/gobuffalo/packr#Box.MustString", "Use github.com/gobuffalo/packr#Box.FindString instead.") return b.FindString(name) } -// Bytes is deprecated. Use Find instead +// Deprecated: Use Find instead. func (b Box) Bytes(name string) []byte { - oncer.Deprecate(0, "github.com/gobuffalo/packr#Box.Bytes", "Use github.com/gobuffalo/packr#Box.Find instead.") bb, _ := b.Find(name) return bb } -// Bytes is deprecated. Use Find instead +// Deprecated: Use Find instead. func (b Box) MustBytes(name string) ([]byte, error) { - oncer.Deprecate(0, "github.com/gobuffalo/packr#Box.MustBytes", "Use github.com/gobuffalo/packr#Box.Find instead.") return b.Find(name) } diff --git a/go.mod b/go.mod index 414daaf..143c286 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,12 @@ module github.com/gobuffalo/packr require ( - github.com/gobuffalo/buffalo-plugins v1.11.0 // indirect github.com/gobuffalo/envy v1.6.12 - github.com/gobuffalo/meta v0.0.0-20190120163247-50bbb1fa260d // indirect github.com/gobuffalo/packd v0.0.0-20181212173646-eca3b8fd6687 - github.com/gobuffalo/packr/v2 v2.0.0-rc.14 + github.com/gobuffalo/packr/v2 v2.0.0-rc.15 github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2 github.com/pkg/errors v0.8.1 github.com/spf13/cobra v0.0.3 github.com/stretchr/testify v1.3.0 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 - golang.org/x/sys v0.0.0-20190116161447-11f53e031339 // indirect - golang.org/x/tools v0.0.0-20190118193359-16909d206f00 // indirect ) diff --git a/go.sum b/go.sum index ca42457..3f7a704 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= @@ -9,6 +10,7 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= @@ -109,9 +111,11 @@ github.com/gobuffalo/logger v0.0.0-20181027144941-73d08d2bb969/go.mod h1:7uGg2du github.com/gobuffalo/logger v0.0.0-20181027193913-9cf4dd0efe46/go.mod h1:7uGg2duHKpWnN4+YmyKBdLXfhopkAdVM6H3nKbyFbz8= github.com/gobuffalo/logger v0.0.0-20181109185836-3feeab578c17/go.mod h1:oNErH0xLe+utO+OW8ptXMSA5DkiSEDW1u3zGIt8F9Ew= github.com/gobuffalo/logger v0.0.0-20181117211126-8e9b89b7c264/go.mod h1:5etB91IE0uBlw9k756fVKZJdS+7M7ejVhmpXXiSFj0I= +github.com/gobuffalo/logger v0.0.0-20181127160119-5b956e21995c h1:Z/ppYX6EtPEysbW4VEGz2dO+4F4VTthWp2sWRUCANdU= github.com/gobuffalo/logger v0.0.0-20181127160119-5b956e21995c/go.mod h1:+HxKANrR9VGw9yN3aOAppJKvhO05ctDi63w4mDnKv2U= github.com/gobuffalo/makr v1.1.5/go.mod h1:Y+o0btAH1kYAMDJW/TX3+oAXEu0bmSLLoC9mIFxtzOw= github.com/gobuffalo/mapi v1.0.0/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= +github.com/gobuffalo/mapi v1.0.1 h1:JRuTiZzDEZhBHkFiHTxJkYRT6CbYuL0K/rn+1byJoEA= github.com/gobuffalo/mapi v1.0.1/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= github.com/gobuffalo/meta v0.0.0-20181018155829-df62557efcd3/go.mod h1:XTTOhwMNryif3x9LkTTBO/Llrveezd71u3quLd0u7CM= github.com/gobuffalo/meta v0.0.0-20181018192820-8c6cef77dab3/go.mod h1:E94EPzx9NERGCY69UWlcj6Hipf2uK/vnfrF4QD0plVE= @@ -150,8 +154,9 @@ github.com/gobuffalo/packr/v2 v2.0.0-rc.10/go.mod h1:4CWWn4I5T3v4c1OsJ55HbHlUEKN github.com/gobuffalo/packr/v2 v2.0.0-rc.11/go.mod h1:JoieH/3h3U4UmatmV93QmqyPUdf4wVM9HELaHEu+3fk= github.com/gobuffalo/packr/v2 v2.0.0-rc.12/go.mod h1:FV1zZTsVFi1DSCboO36Xgs4pzCZBjB/tDV9Cz/lSaR8= github.com/gobuffalo/packr/v2 v2.0.0-rc.13/go.mod h1:2Mp7GhBFMdJlOK8vGfl7SYtfMP3+5roE39ejlfjw0rA= -github.com/gobuffalo/packr/v2 v2.0.0-rc.14 h1:K41dNilNHDbDgCL3UE6K02JGuN89pjvD9oG99X7Om2s= github.com/gobuffalo/packr/v2 v2.0.0-rc.14/go.mod h1:06otbrNvDKO1eNQ3b8hst+1010UooI2MFg+B2Ze4MV8= +github.com/gobuffalo/packr/v2 v2.0.0-rc.15 h1:vSmYcMO6CtuNQvMSbEJeIJlaeZzz2zoxGLTy8HrDh80= +github.com/gobuffalo/packr/v2 v2.0.0-rc.15/go.mod h1:IMe7H2nJvcKXSF90y4X1rjYIRlNMJYCxEhssBXNZwWs= github.com/gobuffalo/plush v3.7.16+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI= github.com/gobuffalo/plush v3.7.20+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI= github.com/gobuffalo/plush v3.7.21+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI= @@ -180,6 +185,7 @@ github.com/gobuffalo/release v1.1.3/go.mod h1:CuXc5/m+4zuq8idoDt1l4va0AXAn/OSs08 github.com/gobuffalo/release v1.1.6/go.mod h1:18naWa3kBsqO0cItXZNJuefCKOENpbbUIqRL1g+p6z0= github.com/gobuffalo/shoulders v1.0.1/go.mod h1:V33CcVmaQ4gRUmHKwq1fiTXuf8Gp/qjQBUL5tHPmvbA= github.com/gobuffalo/syncx v0.0.0-20181120191700-98333ab04150/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= +github.com/gobuffalo/syncx v0.0.0-20181120194010-558ac7de985f h1:S5EeH1reN93KR0L6TQvkRpu9YggCYXrUqFh1iEgvdC0= github.com/gobuffalo/syncx v0.0.0-20181120194010-558ac7de985f/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= github.com/gobuffalo/tags v2.0.11+incompatible/go.mod h1:9XmhOkyaB7UzvuY4UoZO4s67q8/xRMVJEaakauVQYeY= github.com/gobuffalo/tags v2.0.14+incompatible/go.mod h1:9XmhOkyaB7UzvuY4UoZO4s67q8/xRMVJEaakauVQYeY= @@ -202,11 +208,13 @@ github.com/gorilla/sessions v1.1.2/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ= github.com/jackc/pgx v3.2.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I= github.com/jmoiron/sqlx v0.0.0-20180614180643-0dae4fefe7c0/go.mod h1:IiEW3SEiiErVyFdH8NTuWjSifiEQKUoyK3LNqr2kCHU= github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/karrick/godirwalk v1.7.5/go.mod h1:2c9FRhkDxdIbgkOnCEvnSWs71Bhugbl46shStcFDJ34= github.com/karrick/godirwalk v1.7.7/go.mod h1:2c9FRhkDxdIbgkOnCEvnSWs71Bhugbl46shStcFDJ34= @@ -214,6 +222,7 @@ github.com/karrick/godirwalk v1.7.8 h1:VfG72pyIxgtC7+3X9CMHI0AOl4LwyRAg98WAgsvff github.com/karrick/godirwalk v1.7.8/go.mod h1:2c9FRhkDxdIbgkOnCEvnSWs71Bhugbl46shStcFDJ34= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -233,9 +242,11 @@ github.com/markbates/inflect v1.0.4/go.mod h1:1fR9+pO2KHEO9ZRtto13gDwwZaAKstQzfe github.com/markbates/oncer v0.0.0-20180924031910-e862a676800b/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/oncer v0.0.0-20180924034138-723ad0170a46/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/oncer v0.0.0-20181014194634-05fccaae8fc4/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= +github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2 h1:JgVTCPf0uBVcUSWpyXmGpgOc62nK5HWUBKAGc3Qqa5k= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/refresh v1.4.10/go.mod h1:NDPHvotuZmTmesXxr95C9bjlw1/0frJwtME2dzcVKhc= github.com/markbates/safe v1.0.0/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= +github.com/markbates/safe v1.0.1 h1:yjZkbvRM6IzKj9tlu/zMJLS0n/V351OZWRnF3QfaUxI= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= github.com/markbates/sigtx v1.0.0/go.mod h1:QF1Hv6Ic6Ca6W+T+DL0Y/ypborFKyvUY9HmuCD4VeTc= github.com/markbates/willie v1.0.9/go.mod h1:fsrFVWl91+gXpx/6dv715j7i11fYPfZ9ZGfH0DQzY7w= @@ -258,6 +269,7 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.0.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.1.0 h1:g0fH8RicVgNl+zVZDCDfbdWxAWoAEJyI7I3TZYXFiig= @@ -284,9 +296,11 @@ github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B github.com/spf13/afero v1.2.0/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cast v1.2.0/go.mod h1:r2rcYCSwa1IExKTDiTfzaxqT2FNHs8hODu4LnUfgKEg= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.2.1/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaNVlI= github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= diff --git a/v2/box.go b/v2/box.go index 0babea7..3e4c74d 100644 --- a/v2/box.go +++ b/v2/box.go @@ -8,11 +8,9 @@ import ( "net/http" "os" "path/filepath" - "runtime" "sort" "strings" - "github.com/gobuffalo/envy" "github.com/gobuffalo/packd" "github.com/gobuffalo/packr/v2/file" "github.com/gobuffalo/packr/v2/file/resolver" @@ -25,78 +23,30 @@ var _ packd.Box = &Box{} var _ packd.HTTPBox = &Box{} var _ packd.Addable = &Box{} var _ packd.Walkable = &Box{} -var _ packd.Finder = Box{} +var _ packd.Finder = &Box{} + +// Box represent a folder on a disk you want to +// have access to in the built Go binary. +type Box struct { + Path string `json:"path"` + Name string `json:"name"` + ResolutionDir string `json:"resolution_dir"` + DefaultResolver resolver.Resolver `json:"default_resolver"` + resolvers resolversMap + dirs dirsMap +} // NewBox returns a Box that can be used to // retrieve files from either disk or the embedded // binary. +// Deprecated: Use New instead. func NewBox(path string) *Box { oncer.Deprecate(0, "packr.NewBox", "Use packr.New instead.") return New(path, path) } -func resolutionDir(og string) string { - ng, _ := filepath.Abs(og) - - if resolutionDirExists(ng, og) { - return ng - } - - // packr.New - _, filename, _, _ := runtime.Caller(3) - ng, ok := resolutionDirTestFilename(filename, og) - if ok { - return ng - } - - // packr.NewBox (deprecated) - _, filename, _, _ = runtime.Caller(4) - ng, ok = resolutionDirTestFilename(filename, og) - if ok { - return ng - } - - return og -} - -func resolutionDirExists(s, og string) bool { - _, err := os.Stat(s) - if err != nil { - return false - } - plog.Debug("packr", "resolutionDir", "original", og, "resolved", s) - return true -} - -func resolutionDirTestFilename(filename, og string) (string, bool) { - ng := filepath.Join(filepath.Dir(filename), og) - - // // this little hack courtesy of the `-cover` flag!! - cov := filepath.Join("_test", "_obj_test") - ng = strings.Replace(ng, string(filepath.Separator)+cov, "", 1) - - if resolutionDirExists(ng, og) { - return ng, true - } - - ng = filepath.Join(envy.GoPath(), "src", ng) - if resolutionDirExists(ng, og) { - return ng, true - } - - return og, false -} - -func construct(name string, path string) *Box { - return &Box{ - Path: path, - Name: name, - ResolutionDir: resolutionDir(path), - resolvers: resolversMap{}, - dirs: dirsMap{}, - } -} - +// New returns a new Box with the name of the box +// and the path of the box. func New(name string, path string) *Box { plog.Debug("packr", "New", "name", name, "path", path) b, _ := findBox(name) @@ -114,21 +64,11 @@ func New(name string, path string) *Box { return b } -// Box represent a folder on a disk you want to -// have access to in the built Go binary. -type Box struct { - Path string `json:"path"` - Name string `json:"name"` - ResolutionDir string `json:"resolution_dir"` - DefaultResolver resolver.Resolver `json:"default_resolver"` - resolvers resolversMap - dirs dirsMap -} - +// SetResolver allows for the use of a custom resolver for +// the specified file func (b *Box) SetResolver(file string, res resolver.Resolver) { d := filepath.Dir(file) b.dirs.Store(d, true) - b.dirs.Store(strings.TrimPrefix(d, "/"), true) plog.Debug(b, "SetResolver", "file", file, "resolver", fmt.Sprintf("%T", res)) b.resolvers.Store(resolver.Key(file), res) } @@ -153,14 +93,14 @@ func (b *Box) AddBytes(path string, t []byte) error { // FindString returns either the string of the requested // file or an error if it can not be found. -func (b Box) FindString(name string) (string, error) { +func (b *Box) FindString(name string) (string, error) { bb, err := b.Find(name) return string(bb), err } // Find returns either the byte slice of the requested // file or an error if it can not be found. -func (b Box) Find(name string) ([]byte, error) { +func (b *Box) Find(name string) ([]byte, error) { f, err := b.Resolve(name) if err != nil { return []byte(""), err @@ -171,20 +111,17 @@ func (b Box) Find(name string) ([]byte, error) { } // Has returns true if the resource exists in the box -func (b Box) Has(name string) bool { +func (b *Box) Has(name string) bool { _, err := b.Find(name) - if err != nil { - return false - } - return true + return err == nil } +// HasDir returns true if the directory exists in the box func (b *Box) HasDir(name string) bool { oncer.Do("packr2/box/HasDir", func() { for _, f := range b.List() { d := filepath.Dir(f) b.dirs.Store(d, true) - b.dirs.Store(strings.TrimPrefix(d, "/"), true) } }) if name == "/" { @@ -232,6 +169,8 @@ func (b *Box) List() []string { return keys } +// Resolve will attempt to find the file in the box, +// returning an error if the find can not be found. func (b *Box) Resolve(key string) (file.File, error) { key = strings.TrimPrefix(key, "/") diff --git a/v2/deprecated.go b/v2/deprecated.go index 7a19df3..4093410 100644 --- a/v2/deprecated.go +++ b/v2/deprecated.go @@ -54,26 +54,26 @@ func PackJSONBytes(box string, name string, jbb string) error { } // Bytes is deprecated. Use Find instead -func (b Box) Bytes(name string) []byte { +func (b *Box) Bytes(name string) []byte { bb, _ := b.Find(name) oncer.Deprecate(0, "github.com/gobuffalo/packr/v2#Box.Bytes", "Use github.com/gobuffalo/packr/v2#Box.Find instead.") return bb } // MustBytes is deprecated. Use Find instead. -func (b Box) MustBytes(name string) ([]byte, error) { +func (b *Box) MustBytes(name string) ([]byte, error) { oncer.Deprecate(0, "github.com/gobuffalo/packr/v2#Box.MustBytes", "Use github.com/gobuffalo/packr/v2#Box.Find instead.") return b.Find(name) } // String is deprecated. Use FindString instead -func (b Box) String(name string) string { +func (b *Box) String(name string) string { oncer.Deprecate(0, "github.com/gobuffalo/packr/v2#Box.String", "Use github.com/gobuffalo/packr/v2#Box.FindString instead.") return string(b.Bytes(name)) } // MustString is deprecated. Use FindString instead -func (b Box) MustString(name string) (string, error) { +func (b *Box) MustString(name string) (string, error) { oncer.Deprecate(0, "github.com/gobuffalo/packr/v2#Box.MustString", "Use github.com/gobuffalo/packr/v2#Box.FindString instead.") return b.FindString(name) } diff --git a/v2/dirs_map.go b/v2/dirs_map.go index 022c40d..9e64913 100644 --- a/v2/dirs_map.go +++ b/v2/dirs_map.go @@ -59,7 +59,9 @@ func (m *dirsMap) Range(f func(key string, value bool) bool) { // Store a bool in the map func (m *dirsMap) Store(key string, value bool) { - m.data.Store(m.normalizeKey(key), value) + d := m.normalizeKey(key) + m.data.Store(d, value) + m.data.Store(strings.TrimPrefix(d, "/"), value) } // Keys returns a list of keys in the map diff --git a/v2/go.mod b/v2/go.mod index b2b734f..00b28c7 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -14,5 +14,5 @@ require ( github.com/spf13/cobra v0.0.3 github.com/stretchr/testify v1.3.0 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 - golang.org/x/tools v0.0.0-20190130015043-a06a922acc1b + golang.org/x/tools v0.0.0-20190130173957-1c3581914d33 ) diff --git a/v2/go.sum b/v2/go.sum index 4cf293f..cefd91d 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -392,8 +392,8 @@ golang.org/x/tools v0.0.0-20190104182027-498d95493402/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190111214448-fc1d57b08d7b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190118193359-16909d206f00/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190124004107-78ee07aa9465/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190130015043-a06a922acc1b h1:U38c0ex5oDn0qj+Wmwi1j4MYDu9t2MMRNyN+WCujdYk= -golang.org/x/tools v0.0.0-20190130015043-a06a922acc1b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190130173957-1c3581914d33 h1:vFQfd0ZIPMGfY3UauV2bnnBBXs2H11XphDbkwHZQDK8= +golang.org/x/tools v0.0.0-20190130173957-1c3581914d33/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= diff --git a/v2/helpers.go b/v2/helpers.go new file mode 100644 index 0000000..3654dff --- /dev/null +++ b/v2/helpers.go @@ -0,0 +1,73 @@ +package packr + +import ( + "os" + "path/filepath" + "runtime" + "strings" + + "github.com/gobuffalo/envy" + "github.com/gobuffalo/packr/v2/plog" +) + +func construct(name string, path string) *Box { + return &Box{ + Path: path, + Name: name, + ResolutionDir: resolutionDir(path), + resolvers: resolversMap{}, + dirs: dirsMap{}, + } +} + +func resolutionDirTestFilename(filename, og string) (string, bool) { + ng := filepath.Join(filepath.Dir(filename), og) + + // // this little hack courtesy of the `-cover` flag!! + cov := filepath.Join("_test", "_obj_test") + ng = strings.Replace(ng, string(filepath.Separator)+cov, "", 1) + + if resolutionDirExists(ng, og) { + return ng, true + } + + ng = filepath.Join(envy.GoPath(), "src", ng) + if resolutionDirExists(ng, og) { + return ng, true + } + + return og, false +} + +func resolutionDirExists(s, og string) bool { + _, err := os.Stat(s) + if err != nil { + return false + } + plog.Debug("packr", "resolutionDir", "original", og, "resolved", s) + return true +} + +func resolutionDir(og string) string { + ng, _ := filepath.Abs(og) + + if resolutionDirExists(ng, og) { + return ng + } + + // packr.New + _, filename, _, _ := runtime.Caller(3) + ng, ok := resolutionDirTestFilename(filename, og) + if ok { + return ng + } + + // packr.NewBox (deprecated) + _, filename, _, _ = runtime.Caller(4) + ng, ok = resolutionDirTestFilename(filename, og) + if ok { + return ng + } + + return og +} diff --git a/v2/pointer.go b/v2/pointer.go index 70e91c9..fb46a50 100644 --- a/v2/pointer.go +++ b/v2/pointer.go @@ -7,6 +7,8 @@ import ( "github.com/pkg/errors" ) +// Pointer is a resolvr which resolves +// a file from a different box. type Pointer struct { ForwardBox string ForwardPath string @@ -14,6 +16,8 @@ type Pointer struct { var _ resolver.Resolver = Pointer{} +// Resolve attempts to find the file in the specific box +// with the specified key func (p Pointer) Resolve(box string, path string) (file.File, error) { plog.Debug(p, "Resolve", "box", box, "path", path, "forward-box", p.ForwardBox, "forward-path", p.ForwardPath) b, err := findBox(p.ForwardBox) diff --git a/v2/version.go b/v2/version.go index e271899..a427d4b 100644 --- a/v2/version.go +++ b/v2/version.go @@ -1,3 +1,4 @@ package packr +// Version of Packr const Version = "v2.0.0-rc.14" diff --git a/v2/walk.go b/v2/walk.go index acc0604..40cd1f3 100644 --- a/v2/walk.go +++ b/v2/walk.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" ) +// WalkFunc is used to walk a box type WalkFunc = packd.WalkFunc // Walk will traverse the box and call the WalkFunc for each file in the box/folder. @@ -20,7 +21,7 @@ func (b *Box) Walk(wf WalkFunc) error { dr := b.DefaultResolver if dr == nil { cd := resolver.OsPath(b.ResolutionDir) - dr = &resolver.Disk{Root: string(cd)} + dr = &resolver.Disk{Root: cd} } if fm, ok := dr.(file.FileMappable); ok { for n, f := range fm.FileMap() { @@ -36,7 +37,7 @@ func (b *Box) Walk(wf WalkFunc) error { } keep := true for k := range m { - if strings.ToLower(k) == strings.ToLower(n) { + if strings.EqualFold(k, n) { keep = false } }