Replace packr with go embed (#1751)

* Embed performer images
* Embed schema migrations
* Update dependencies
* Embed UI
* Remove remaining packr references
This commit is contained in:
WithoutPants 2021-09-22 13:08:34 +10:00 committed by GitHub
parent f292238e7f
commit 56111433a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
429 changed files with 39923 additions and 23061 deletions

View file

@ -15,16 +15,10 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Packr2 artifacts
**/*-packr.go
# GraphQL generated output
pkg/models/generated_*.go
ui/v2.5/src/core/generated-*.tsx
# packr generated files
*-packr.go
####
# Jetbrains
####
@ -60,4 +54,4 @@ ui/v2.5/build
stash
dist
docker
docker

View file

@ -77,11 +77,10 @@ jobs:
# this means that the build version/time may be incorrect if the UI is
# not changed in a pull request
if: ${{ github.event_name != 'pull_request' || steps.cache-ui.outputs.cache-hit != 'true' }}
run: docker exec -t build /bin/bash -c "make ui-only"
run: docker exec -t build /bin/bash -c "make ui"
- name: Compile for all supported platforms
run: |
docker exec -t build /bin/bash -c "make packr"
docker exec -t build /bin/bash -c "make cross-compile-windows"
docker exec -t build /bin/bash -c "make cross-compile-osx-intel"
docker exec -t build /bin/bash -c "make cross-compile-osx-applesilicon"

6
.gitignore vendored
View file

@ -15,16 +15,10 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Packr2 artifacts
**/*-packr.go
# GraphQL generated output
pkg/models/generated_*.go
ui/v2.5/src/core/generated-*.tsx
# packr generated files
*-packr.go
####
# Jetbrains
####

View file

@ -23,9 +23,8 @@ ifdef OUTPUT
endif
export CGO_ENABLED = 1
export GO111MODULE = on
.PHONY: release pre-build install clean
.PHONY: release pre-build
release: generate ui build-release
@ -113,12 +112,6 @@ cross-compile-all:
make cross-compile-linux-arm32v7
make cross-compile-pi
install:
packr2 install
clean:
packr2 clean
# Regenerates GraphQL files
generate: generate-backend generate-frontend
@ -175,17 +168,13 @@ generate-test-mocks:
pre-ui:
cd ui/v2.5 && yarn install --frozen-lockfile
.PHONY: ui-only
ui-only: pre-build
.PHONY: ui
ui: pre-build
$(SET) REACT_APP_DATE="$(BUILD_DATE)" $(SEPARATOR) \
$(SET) REACT_APP_GITHASH=$(GITHASH) $(SEPARATOR) \
$(SET) REACT_APP_STASH_VERSION=$(STASH_VERSION) $(SEPARATOR) \
cd ui/v2.5 && yarn build
.PHONY: ui
ui: ui-only
packr2
.PHONY: ui-start
ui-start: pre-build
$(SET) REACT_APP_DATE="$(BUILD_DATE)" $(SEPARATOR) \
@ -202,13 +191,6 @@ fmt-ui:
ui-validate:
cd ui/v2.5 && yarn run validate
# just repacks the packr files - use when updating migrations and packed files without
# rebuilding the UI
.PHONY: packr
packr:
$(SET) GO111MODULE=on
packr2
# runs all of the tests and checks required for a PR to be accepted
.PHONY: validate
validate: ui-validate fmt-check vet lint it
@ -216,4 +198,4 @@ validate: ui-validate fmt-check vet lint it
# locally builds and tags a 'stash/build' docker image
.PHONY: docker-build
docker-build:
docker build -t stash/build -f docker/build/x86_64/Dockerfile .
docker build -t stash/build -f docker/build/x86_64/Dockerfile .

View file

@ -96,9 +96,6 @@ For issues not addressed there, there are a few options.
* [Go](https://golang.org/dl/)
* [Revive](https://github.com/mgechev/revive) - Configurable linter
* Go Install: `go get github.com/mgechev/revive`
* [Packr2](https://github.com/gobuffalo/packr/) - Static asset bundler
* Go Install: `go get github.com/gobuffalo/packr/v2/packr2`
* [Binary Download](https://github.com/gobuffalo/packr/releases)
* [Yarn](https://yarnpkg.com/en/docs/install) - Yarn package manager
* Run `yarn install --frozen-lockfile` in the `stash/ui/v2.5` folder (before running make generate for first time).
@ -126,9 +123,8 @@ NOTE: The `make` command in Windows will be `mingw32-make` with MingW.
* `make build` - Builds the binary (make sure to build the UI as well... see below)
* `make docker-build` - Locally builds and tags a complete 'stash/build' docker image
* `make pre-ui` - Installs the UI dependencies. Only needs to be run once before building the UI for the first time, or if the dependencies are updated
* `make fmt-ui` - Formats the UI source code.
* `make ui` - Builds the frontend and the packr2 files
* `make packr` - Generate packr2 files (sub-target of `ui`. Use to regenerate packr2 files without rebuilding UI)
* `make fmt-ui` - Formats the UI source code
* `make ui` - Builds the frontend
* `make vet` - Run `go vet`
* `make lint` - Run the linter
* `make fmt` - Run `go fmt`

View file

@ -14,34 +14,24 @@ COPY ./.git /stash/.git
COPY ./graphql /stash/graphql/
COPY ./ui /stash/ui/
RUN make generate-frontend
RUN BUILD_DATE=$(date +"%Y-%m-%d %H:%M:%S") make ui-only
RUN BUILD_DATE=$(date +"%Y-%m-%d %H:%M:%S") make ui
# Build Backend
FROM golang:1.17-alpine as backend
RUN apk add --no-cache xz make alpine-sdk
## install packr, ffmpeg
ENV PACKR2_VERSION=2.8.1
ENV PACKR2_SHA=1cb2a0113550bc7962a8fda31a29877fcbbabd56b46c25e1fffbc225334162e7
ENV PACKR2_DOWNLOAD_FILE=packr_${PACKR2_VERSION}_linux_amd64.tar.gz
ENV PACKR2_DOWNLOAD_URL=https://github.com/gobuffalo/packr/releases/download/v${PACKR2_VERSION}/${PACKR2_DOWNLOAD_FILE}
## install ffmpeg
WORKDIR /
RUN wget ${PACKR2_DOWNLOAD_URL}; \
echo "$PACKR2_SHA $PACKR2_DOWNLOAD_FILE" | sha256sum -c - || exit 1; \
tar -xzf $PACKR2_DOWNLOAD_FILE -C /usr/bin/ packr2; \
rm $PACKR2_DOWNLOAD_FILE;
RUN wget -O /ffmpeg.tar.xz https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz && \
tar xf /ffmpeg.tar.xz && \
rm ffmpeg.tar.xz && \
mv /ffmpeg*/ /ffmpeg/
WORKDIR /stash
COPY ./go* ./*.go Makefile gqlgen.yml .gqlgenc.yml /stash/
COPY ./static /stash/static/
COPY ./scripts /stash/scripts/
COPY ./vendor /stash/vendor/
COPY ./pkg /stash/pkg/
COPY --from=frontend /stash /stash/
RUN make generate-backend
RUN make packr
RUN make build
# Final Runnable Image

View file

@ -2,11 +2,6 @@ FROM golang:1.17
LABEL maintainer="https://discord.gg/2TsNFKt"
ENV PACKR2_VERSION=2.8.1
ENV PACKR2_SHA=1cb2a0113550bc7962a8fda31a29877fcbbabd56b46c25e1fffbc225334162e7
ENV PACKR2_DOWNLOAD_FILE=packr_${PACKR2_VERSION}_linux_amd64.tar.gz
ENV PACKR2_DOWNLOAD_URL=https://github.com/gobuffalo/packr/releases/download/v${PACKR2_VERSION}/${PACKR2_DOWNLOAD_FILE}
# Install tools
RUN apt-get update && apt-get install -y apt-transport-https
RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash -
@ -52,14 +47,6 @@ RUN mkdir -p /root/.ssh; \
chmod 0700 /root/.ssh; \
ssh-keyscan github.com > /root/.ssh/known_hosts;
RUN wget ${PACKR2_DOWNLOAD_URL}; \
echo "$PACKR2_SHA $PACKR2_DOWNLOAD_FILE" | sha256sum -c - || exit 1; \
tar -xzf $PACKR2_DOWNLOAD_FILE -C /usr/bin/ packr2; \
rm $PACKR2_DOWNLOAD_FILE;
CMD ["packr2", "version"]
# Notes for self:
# Windows:
# GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ go build -ldflags "-extldflags '-static'" -tags extended

31
go.mod
View file

@ -12,17 +12,14 @@ require (
github.com/disintegration/imaging v1.6.0
github.com/fvbommel/sortorder v1.0.2
github.com/go-chi/chi v4.0.2+incompatible
github.com/gobuffalo/logger v1.0.4 // indirect
github.com/gobuffalo/packr/v2 v2.8.1
github.com/golang-migrate/migrate/v4 v4.3.1
github.com/golang-migrate/migrate/v4 v4.15.0-beta.1
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.0
github.com/gorilla/websocket v1.4.2
github.com/h2non/filetype v1.0.8
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/jmoiron/sqlx v1.2.0
github.com/jmoiron/sqlx v1.3.1
github.com/json-iterator/go v1.1.9
github.com/karrick/godirwalk v1.16.1 // indirect
github.com/mattn/go-sqlite3 v1.14.6
github.com/natefinch/pie v0.0.0-20170715172608-9a0d72014007
github.com/remeh/sizedwaitgroup v1.0.0
@ -33,19 +30,19 @@ require (
github.com/spf13/afero v1.2.0 // indirect
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
github.com/tidwall/gjson v1.8.1
github.com/tidwall/pretty v1.2.0 // indirect
github.com/vektah/gqlparser/v2 v2.0.1
github.com/vektra/mockery/v2 v2.2.1
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/tools v0.0.0-20200915031644-64986481280e // indirect
golang.org/x/tools v0.1.0 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v2 v2.4.0
)
require (
@ -55,29 +52,25 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/gobuffalo/packd v1.0.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.1.0-rc.5 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/markbates/errx v1.1.0 // indirect
github.com/markbates/oncer v1.0.0 // indirect
github.com/markbates/safe v1.0.1 // indirect
github.com/matryer/moq v0.0.0-20200106131100-75d0ddfc0007 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pelletier/go-toml v1.7.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/zerolog v1.18.0 // indirect
@ -86,15 +79,17 @@ require (
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/cobra v1.0.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/urfave/cli/v2 v2.1.1 // indirect
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e // indirect
golang.org/x/mod v0.3.0 // indirect
golang.org/x/text v0.3.3 // indirect
go.uber.org/atomic v1.6.0 // indirect
golang.org/x/mod v0.4.1 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999

705
go.sum

File diff suppressed because it is too large Load diff

View file

@ -2,6 +2,7 @@
package main
import (
"embed"
"os"
"os/signal"
"runtime/pprof"
@ -15,9 +16,15 @@ import (
_ "github.com/golang-migrate/migrate/v4/source/file"
)
//go:embed ui/v2.5/build
var uiBox embed.FS
//go:embed ui/login
var loginUIBox embed.FS
func main() {
manager.Initialize()
api.Start()
api.Start(uiBox, loginUIBox)
// stop any profiling at exit
defer pprof.StopCPUProfile()

View file

@ -1,24 +1,36 @@
package api
import (
"io/fs"
"io/ioutil"
"os"
"strings"
"github.com/gobuffalo/packr/v2"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
"github.com/stashapp/stash/pkg/static"
"github.com/stashapp/stash/pkg/utils"
)
type imageBox struct {
box *packr.Box
box fs.FS
files []string
}
func newImageBox(box *packr.Box) *imageBox {
return &imageBox{
box: box,
files: box.List(),
func newImageBox(box fs.FS) (*imageBox, error) {
ret := &imageBox{
box: box,
}
err := fs.WalkDir(box, ".", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
ret.files = append(ret.files, path)
}
return nil
})
return ret, err
}
var performerBox *imageBox
@ -26,8 +38,15 @@ var performerBoxMale *imageBox
var performerBoxCustom *imageBox
func initialiseImages() {
performerBox = newImageBox(packr.New("Performer Box", "../../static/performer"))
performerBoxMale = newImageBox(packr.New("Male Performer Box", "../../static/performer_male"))
var err error
performerBox, err = newImageBox(&static.Performer)
if err != nil {
logger.Warnf("error loading performer images: %v", err)
}
performerBoxMale, err = newImageBox(&static.PerformerMale)
if err != nil {
logger.Warnf("error loading male performer images: %v", err)
}
initialiseCustomImages()
}
@ -36,7 +55,11 @@ func initialiseCustomImages() {
if customPath != "" {
logger.Debugf("Loading custom performer images from %s", customPath)
// We need to set performerBoxCustom at runtime, as this is a custom path, and store it in a pointer.
performerBoxCustom = newImageBox(packr.Folder(customPath))
var err error
performerBoxCustom, err = newImageBox(os.DirFS(customPath))
if err != nil {
logger.Warnf("error loading custom performer from %s: %v", customPath, err)
}
} else {
performerBoxCustom = nil
}
@ -63,5 +86,11 @@ func getRandomPerformerImageUsingName(name, gender, customPath string) ([]byte,
imageFiles := box.files
index := utils.IntFromString(name) % uint64(len(imageFiles))
return box.box.Find(imageFiles[index])
img, err := box.box.Open(imageFiles[index])
if err != nil {
return nil, err
}
defer img.Close()
return ioutil.ReadAll(img)
}

View file

@ -3,8 +3,10 @@ package api
import (
"context"
"crypto/tls"
"embed"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"net/http"
"net/url"
@ -21,7 +23,6 @@ import (
gqlPlayground "github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/gobuffalo/packr/v2"
"github.com/gorilla/websocket"
"github.com/rs/cors"
"github.com/stashapp/stash/pkg/logger"
@ -36,11 +37,6 @@ var version string
var buildstamp string
var githash string
var uiBox *packr.Box
//var legacyUiBox *packr.Box
var loginUIBox *packr.Box
func allowUnauthenticated(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, "/login") || r.URL.Path == "/css"
}
@ -100,11 +96,7 @@ func authenticateHandler() func(http.Handler) http.Handler {
const loginEndPoint = "/login"
func Start() {
uiBox = packr.New("UI Box", "../../ui/v2.5/build")
//legacyUiBox = packr.New("UI Box", "../../ui/v1/dist/stash-frontend")
loginUIBox = packr.New("Login UI Box", "../../ui/login")
func Start(uiBox embed.FS, loginUIBox embed.FS) {
initialiseImages()
r := chi.NewRouter()
@ -172,10 +164,10 @@ func Start() {
r.HandleFunc("/playground", gqlPlayground.Handler("GraphQL playground", "/graphql"))
// session handlers
r.Post(loginEndPoint, handleLogin)
r.Get("/logout", handleLogout)
r.Post(loginEndPoint, handleLogin(loginUIBox))
r.Get("/logout", handleLogout(loginUIBox))
r.Get(loginEndPoint, getLoginHandler)
r.Get(loginEndPoint, getLoginHandler(loginUIBox))
r.Mount("/performer", performerRoutes{
txnManager: txnManager,
@ -216,11 +208,14 @@ func Start() {
r.HandleFunc("/login*", func(w http.ResponseWriter, r *http.Request) {
ext := path.Ext(r.URL.Path)
if ext == ".html" || ext == "" {
data, _ := loginUIBox.Find("login.html")
_, _ = w.Write(data)
_, _ = w.Write(getLoginPage(loginUIBox))
} else {
r.URL.Path = strings.Replace(r.URL.Path, loginEndPoint, "", 1)
http.FileServer(loginUIBox).ServeHTTP(w, r)
loginRoot, err := fs.Sub(loginUIBox, loginRootDir)
if err != nil {
panic(err)
}
http.FileServer(http.FS(loginRoot)).ServeHTTP(w, r)
}
})
@ -245,6 +240,8 @@ func Start() {
// Serve the web app
r.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
const uiRootDir = "ui/v2.5/build"
ext := path.Ext(r.URL.Path)
if customUILocation != "" {
@ -257,7 +254,10 @@ func Start() {
}
if ext == ".html" || ext == "" {
data, _ := uiBox.Find("index.html")
data, err := uiBox.ReadFile(uiRootDir + "/index.html")
if err != nil {
panic(err)
}
prefix := ""
if r.Header.Get("X-Forwarded-Prefix") != "" {
@ -272,7 +272,11 @@ func Start() {
if isStatic {
w.Header().Add("Cache-Control", "max-age=604800000")
}
http.FileServer(uiBox).ServeHTTP(w, r)
uiRoot, err := fs.Sub(uiBox, uiRootDir)
if err != nil {
panic(error.Error(err))
}
http.FileServer(http.FS(uiRoot)).ServeHTTP(w, r)
}
})

View file

@ -1,6 +1,7 @@
package api
import (
"embed"
"fmt"
"html/template"
"net/http"
@ -10,15 +11,24 @@ import (
"github.com/stashapp/stash/pkg/session"
)
const loginRootDir = "ui/login"
const returnURLParam = "returnURL"
func getLoginPage(loginUIBox embed.FS) []byte {
data, err := loginUIBox.ReadFile(loginRootDir + "/login.html")
if err != nil {
panic(err)
}
return data
}
type loginTemplateData struct {
URL string
Error string
}
func redirectToLogin(w http.ResponseWriter, returnURL string, loginError string) {
data, _ := loginUIBox.Find("login.html")
func redirectToLogin(loginUIBox embed.FS, w http.ResponseWriter, returnURL string, loginError string) {
data := getLoginPage(loginUIBox)
templ, err := template.New("Login").Parse(string(data))
if err != nil {
http.Error(w, fmt.Sprintf("error: %s", err), http.StatusInternalServerError)
@ -31,42 +41,48 @@ func redirectToLogin(w http.ResponseWriter, returnURL string, loginError string)
}
}
func getLoginHandler(w http.ResponseWriter, r *http.Request) {
if !config.GetInstance().HasCredentials() {
http.Redirect(w, r, "/", http.StatusFound)
return
}
func getLoginHandler(loginUIBox embed.FS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !config.GetInstance().HasCredentials() {
http.Redirect(w, r, "/", http.StatusFound)
return
}
redirectToLogin(w, r.URL.Query().Get(returnURLParam), "")
redirectToLogin(loginUIBox, w, r.URL.Query().Get(returnURLParam), "")
}
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
url := r.FormValue(returnURLParam)
if url == "" {
url = "/"
}
func handleLogin(loginUIBox embed.FS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
url := r.FormValue(returnURLParam)
if url == "" {
url = "/"
}
err := manager.GetInstance().SessionStore.Login(w, r)
if err == session.ErrInvalidCredentials {
// redirect back to the login page with an error
redirectToLogin(w, url, "Username or password is invalid")
return
}
err := manager.GetInstance().SessionStore.Login(w, r)
if err == session.ErrInvalidCredentials {
// redirect back to the login page with an error
redirectToLogin(loginUIBox, w, url, "Username or password is invalid")
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, url, http.StatusFound)
http.Redirect(w, r, url, http.StatusFound)
}
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
if err := manager.GetInstance().SessionStore.Logout(w, r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
func handleLogout(loginUIBox embed.FS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := manager.GetInstance().SessionStore.Logout(w, r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// redirect to the login page if credentials are required
getLoginHandler(w, r)
// redirect to the login page if credentials are required
getLoginHandler(loginUIBox)(w, r)
}
}

View file

@ -2,6 +2,7 @@ package database
import (
"database/sql"
"embed"
"errors"
"fmt"
"os"
@ -9,10 +10,9 @@ import (
"time"
"github.com/fvbommel/sortorder"
"github.com/gobuffalo/packr/v2"
"github.com/golang-migrate/migrate/v4"
sqlite3mig "github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jmoiron/sqlx"
sqlite3 "github.com/mattn/go-sqlite3"
@ -26,6 +26,9 @@ var dbPath string
var appSchemaVersion uint = 28
var databaseSchemaVersion uint
//go:embed migrations/*.sql
var migrationsBox embed.FS
var (
// ErrMigrationNeeded indicates that a database migration is needed
// before the database can be initialized
@ -200,17 +203,13 @@ func Version() uint {
}
func getMigrate() (*migrate.Migrate, error) {
migrationsBox := packr.New("Migrations Box", "./migrations")
packrSource := &Packr2Source{
Box: migrationsBox,
Migrations: source.NewMigrations(),
migrations, err := iofs.New(migrationsBox, "migrations")
if err != nil {
panic(err.Error())
}
databasePath := utils.FixWindowsPath(dbPath)
s, _ := WithInstance(packrSource)
const disableForeignKeys = true
conn := open(databasePath, disableForeignKeys)
conn := open(dbPath, disableForeignKeys)
driver, err := sqlite3mig.WithInstance(conn.DB, &sqlite3mig.Config{})
if err != nil {
@ -219,9 +218,9 @@ func getMigrate() (*migrate.Migrate, error) {
// use sqlite3Driver so that migration has access to durationToTinyInt
return migrate.NewWithInstance(
"packr2",
s,
databasePath,
"iofs",
migrations,
dbPath,
driver,
)
}

View file

@ -1,92 +0,0 @@
package database
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/gobuffalo/packr/v2"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/source"
)
type Packr2Source struct {
Box *packr.Box
Migrations *source.Migrations
}
func init() {
source.Register("packr2", &Packr2Source{})
}
func WithInstance(instance *Packr2Source) (source.Driver, error) {
for _, fi := range instance.Box.List() {
m, err := source.DefaultParse(fi)
if err != nil {
continue // ignore files that we can't parse
}
if !instance.Migrations.Append(m) {
return nil, fmt.Errorf("unable to parse file %v", fi)
}
}
return instance, nil
}
func (s *Packr2Source) Open(url string) (source.Driver, error) {
return nil, fmt.Errorf("not implemented")
}
func (s *Packr2Source) Close() error {
s.Migrations = nil
return nil
}
func (s *Packr2Source) First() (version uint, err error) {
if v, ok := s.Migrations.First(); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *Packr2Source) Prev(version uint) (prevVersion uint, err error) {
if v, ok := s.Migrations.Prev(version); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *Packr2Source) Next(version uint) (nextVersion uint, err error) {
if v, ok := s.Migrations.Next(version); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *Packr2Source) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
if migration, ok := s.Migrations.Up(version); !ok {
return nil, "", os.ErrNotExist
} else {
b, _ := s.Box.Find(migration.Raw)
return ioutil.NopCloser(bytes.NewBuffer(b)),
migration.Identifier,
nil
}
}
func (s *Packr2Source) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
if migration, ok := s.Migrations.Down(version); !ok {
return nil, "", migrate.ErrNilVersion
} else {
b, _ := s.Box.Find(migration.Raw)
return ioutil.NopCloser(bytes.NewBuffer(b)),
migration.Identifier,
nil
}
}

9
pkg/static/embed.go Normal file
View file

@ -0,0 +1,9 @@
package static
import "embed"
//go:embed performer
var Performer embed.FS
//go:embed performer_male
var PerformerMale embed.FS

View file

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 9.7 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

View file

Before

Width:  |  Height:  |  Size: 8.7 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View file

Before

Width:  |  Height:  |  Size: 9.6 KiB

After

Width:  |  Height:  |  Size: 9.6 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View file

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

View file

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

View file

Before

Width:  |  Height:  |  Size: 9.6 KiB

After

Width:  |  Height:  |  Size: 9.6 KiB

View file

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View file

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -8,25 +8,16 @@ GITHASH=`git rev-parse --short HEAD`
STASH_VERSION=`git describe --tags --exclude latest_develop`
SETENV="BUILD_DATE=\"$BUILD_DATE\" GITHASH=$GITHASH STASH_VERSION=\"$STASH_VERSION\""
SETUP="export CGO_ENABLED=1; set -e; echo '=== Running packr ==='; make packr;"
SETUP_FAST="export CGO_ENABLED=1;"
SETUP="export CGO_ENABLED=1;"
WINDOWS="echo '=== Building Windows binary ==='; $SETENV make cross-compile-windows;"
DARWIN="echo '=== Building OSX binary ==='; $SETENV make cross-compile-osx-intel;"
DARWIN-ARM64="echo '=== Building OSX (arm64) binary ==='; $SETENV make cross-compile-osx-applesilicon;"
DARWIN_ARM64="echo '=== Building OSX (arm64) binary ==='; $SETENV make cross-compile-osx-applesilicon;"
LINUX_AMD64="echo '=== Building Linux (amd64) binary ==='; $SETENV make cross-compile-linux;"
LINUX_ARM64v8="echo '=== Building Linux (armv8/arm64) binary ==='; $SETENV make cross-compile-linux-arm64v8;"
LINUX_ARM32v7="echo '=== Building Linux (armv7/armhf) binary ==='; $SETENV make cross-compile-linux-arm32v7;"
LINUX_ARM32v6="echo '=== Building Linux (armv6 | Raspberry Pi 1) binary ==='; $SETENV make cross-compile-pi;"
BUILD_COMPLETE="echo '=== Build complete ==='"
# if build target ends with -fast then use prebuilt packr2. eg amd64-fast or all-fast
FAST=`echo "$1" | cut -d - -f 2`
if [ "$FAST" == "fast" ]
then
echo "Building without Packr2"
SETUP=$SETUP_FAST
fi
BUILD=`echo "$1" | cut -d - -f 1`
if [ "$BUILD" == "windows" ]
then
@ -54,7 +45,7 @@ then
COMMAND="$SETUP $LINUX_ARM32v7 $BUILD_COMPLETE"
else
echo "Building All"
COMMAND="$SETUP $WINDOWS $DARWIN $LINUX_AMD64 $LINUX_ARM64v8 $LINUX_ARM32v7 $LINUX_ARM32v6 $BUILD_COMPLETE"
COMMAND="$SETUP $WINDOWS $DARWIN $DARWIN_ARM64 $LINUX_AMD64 $LINUX_ARM64v8 $LINUX_ARM32v7 $LINUX_ARM32v6 $BUILD_COMPLETE"
fi
# Pull Latest Image

View file

@ -1,29 +0,0 @@
*.log
.DS_Store
doc
tmp
pkg
*.gem
*.pid
coverage
coverage.data
build/*
*.pbxuser
*.mode1v3
.svn
profile
.console_history
.sass-cache/*
.rake_tasks~
*.log.lck
solr/
.jhw-cache/
jhw.*
*.sublime*
node_modules/
dist/
generated/
.vendor/
bin/*
gin-bin
.idea/

View file

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2019 Mark Bates
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,61 +0,0 @@
TAGS ?= ""
GO_BIN ?= "go"
install:
$(GO_BIN) install -tags ${TAGS} -v .
make tidy
tidy:
ifeq ($(GO111MODULE),on)
$(GO_BIN) mod tidy
else
echo skipping go mod tidy
endif
deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
make tidy
build:
$(GO_BIN) build -v .
make tidy
test:
$(GO_BIN) test -cover -tags ${TAGS} ./...
make tidy
ci-deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
ci-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
lint:
go get github.com/golangci/golangci-lint/cmd/golangci-lint
golangci-lint run --enable-all
make tidy
update:
ifeq ($(GO111MODULE),on)
rm go.*
$(GO_BIN) mod init
$(GO_BIN) mod tidy
else
$(GO_BIN) get -u -tags ${TAGS}
endif
make test
make install
make tidy
release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
make tidy
release:
$(GO_BIN) get github.com/gobuffalo/release
make tidy
release -y -f version.go --skip-packr
make tidy

View file

@ -1,22 +0,0 @@
<p align="center"><img src="https://github.com/gobuffalo/buffalo/blob/master/logo.svg" width="360"></p>
<p align="center">
<a href="https://godoc.org/github.com/gobuffalo/logger"><img src="https://godoc.org/github.com/gobuffalo/logger?status.svg" alt="GoDoc" /></a>
<a href="https://travis-ci.org/gobuffalo/logger"><img src="https://travis-ci.org/gobuffalo/logger.svg?branch=master" alt="Build Status" /></a>
<a href="https://goreportcard.com/report/github.com/gobuffalo/logger"><img src="https://goreportcard.com/badge/github.com/gobuffalo/logger" alt="Go Report Card" /></a>
</p>
# Logger
The [`logger.Logger`](https://godoc.org/github.com/gobuffalo/logger#Logger) interface is used throughout Buffalo apps, and other systems, to log a whole manner of things.
## Installation
```bash
$ go get -u github.com/gobuffalo/logger
```
## Documentation
* [GoDoc](https://godoc.org/github.com/gobuffalo/logger)
* [General Buffalo Documentation](https://gobuffalo.io)

View file

@ -1,18 +0,0 @@
# github.com/gobuffalo/logger Stands on the Shoulders of Giants
github.com/gobuffalo/logger does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants, this project would not be possible. Please make sure to check them out and thank them for all of their hard work.
Thank you to the following **GIANTS**:
* [github.com/gobuffalo/envy](https://godoc.org/github.com/gobuffalo/envy)
* [github.com/konsorten/go-windows-terminal-sequences](https://godoc.org/github.com/konsorten/go-windows-terminal-sequences)
* [github.com/rogpeppe/go-internal](https://godoc.org/github.com/rogpeppe/go-internal)
* [github.com/sirupsen/logrus](https://godoc.org/github.com/sirupsen/logrus)
* [golang.org/x/term](https://godoc.org/golang.org/x/term)
* [golang.org/x/sys](https://godoc.org/golang.org/x/sys)

View file

@ -1,154 +0,0 @@
package logger
// I really don't want to have this, but until (if) https://github.com/sirupsen/logrus/pull/606 is merged we're stuck with all this code. And yes, this is ALL needed just to remove some blank space in the logs
import (
"bytes"
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
)
const (
red = 31
yellow = 33
blue = 36
gray = 37
)
// textFormatter formats logs into text
type textFormatter struct {
ForceColors bool
isTerminal bool
sync.Once
}
func (f *textFormatter) init(entry *logrus.Entry) {
if entry.Logger != nil {
f.isTerminal = checkIfTerminal(entry.Logger.Out)
}
}
const defaultTimestampFormat = time.RFC3339
// Format renders a single log entry
func (f *textFormatter) Format(entry *logrus.Entry) ([]byte, error) {
prefixFieldClashes(entry.Data)
keys := make([]string, 0, len(entry.Data))
for k := range entry.Data {
keys = append(keys, k)
}
sort.Strings(keys)
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
f.Do(func() { f.init(entry) })
isColored := (f.ForceColors || f.isTerminal)
if isColored {
f.printColored(b, entry, keys)
} else {
f.appendKeyValue(b, "level", entry.Level.String())
f.appendKeyValue(b, "time", entry.Time.Format(defaultTimestampFormat))
if entry.Message != "" {
f.appendKeyValue(b, "msg", entry.Message)
}
for _, key := range keys {
f.appendKeyValue(b, key, entry.Data[key])
}
}
b.WriteByte('\n')
return b.Bytes(), nil
}
func (f *textFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys []string) {
var levelColor int
switch entry.Level {
case logrus.DebugLevel:
levelColor = gray
case logrus.WarnLevel:
levelColor = yellow
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
levelColor = red
default:
levelColor = blue
}
levelText := strings.ToUpper(entry.Level.String())[0:4]
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]", levelColor, levelText, entry.Time.Format(defaultTimestampFormat))
if entry.Message != "" {
fmt.Fprintf(b, " %s", entry.Message)
}
for _, k := range keys {
v := entry.Data[k]
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k)
f.appendValue(b, v)
}
}
func (f *textFormatter) needsQuoting(text string) bool {
if len(text) == 0 {
return true
}
for _, ch := range text {
if !((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') {
return true
}
}
return false
}
func (f *textFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
if b.Len() > 0 {
b.WriteByte(' ')
}
b.WriteString(key)
b.WriteByte('=')
f.appendValue(b, value)
}
func (f *textFormatter) appendValue(b *bytes.Buffer, value interface{}) {
stringVal, ok := value.(string)
if !ok {
stringVal = fmt.Sprint(value)
}
if !f.needsQuoting(stringVal) {
b.WriteString(stringVal)
} else {
b.WriteString(fmt.Sprintf("%q", stringVal))
}
}
func prefixFieldClashes(data logrus.Fields) {
if t, ok := data["time"]; ok {
data["fields.time"] = t
}
if m, ok := data["msg"]; ok {
data["fields.msg"] = m
}
if l, ok := data["level"]; ok {
data["fields.level"] = l
}
}

View file

@ -1,25 +0,0 @@
package logger
import "github.com/sirupsen/logrus"
// Level of the logger
type Level = logrus.Level
const (
// PanicLevel level, highest level of severity. Logs and then calls panic with the
// message passed to Debug, Info, ...
PanicLevel = logrus.PanicLevel
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
// logging level is set to Panic.
FatalLevel = logrus.FatalLevel
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
// Commonly used for hooks to send errors to an error tracking service.
ErrorLevel = logrus.ErrorLevel
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel = logrus.WarnLevel
// InfoLevel level. General operational entries about what's going on inside the
// application.
InfoLevel = logrus.InfoLevel
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
DebugLevel = logrus.DebugLevel
)

View file

@ -1,67 +0,0 @@
package logger
import (
"os"
"github.com/sirupsen/logrus"
)
// FieldLogger interface
type FieldLogger interface {
Logger
WithField(string, interface{}) FieldLogger
WithFields(map[string]interface{}) FieldLogger
}
// Logger interface is used throughout Buffalo
// apps to log a whole manner of things.
type Logger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
Printf(string, ...interface{})
Warnf(string, ...interface{})
Errorf(string, ...interface{})
Fatalf(string, ...interface{})
Debug(...interface{})
Info(...interface{})
Warn(...interface{})
Error(...interface{})
Fatal(...interface{})
Panic(...interface{})
}
func ParseLevel(level string) (Level, error) {
l, err := logrus.ParseLevel(level)
return Level(l), err
}
// NewLogger based on the specified log level, defaults to "debug".
// See `New` for more details.
func NewLogger(level string) FieldLogger {
lvl, err := logrus.ParseLevel(level)
if err != nil {
lvl = logrus.DebugLevel
}
return New(lvl)
}
// New based on the specified log level, defaults to "debug".
// This logger will log to the STDOUT in a human readable,
// but parseable form.
/*
Example: time="2016-12-01T21:02:07-05:00" level=info duration=225.283µs human_size="106 B" method=GET path="/" render=199.79µs request_id=2265736089 size=106 status=200
*/
func New(lvl Level) FieldLogger {
e := os.Getenv("GO_ENV")
if len(e) == 0 {
e = "development"
}
dev := e == "development"
l := logrus.New()
l.SetOutput(os.Stdout)
l.Level = lvl
l.Formatter = &textFormatter{
ForceColors: dev,
}
return Logrus{l}
}

View file

@ -1,34 +0,0 @@
package logger
import (
"io"
"github.com/sirupsen/logrus"
)
var _ Logger = Logrus{}
var _ FieldLogger = Logrus{}
var _ Outable = Logrus{}
// Logrus is a Logger implementation backed by sirupsen/logrus
type Logrus struct {
logrus.FieldLogger
}
// SetOutput will try and set the output of the underlying
// logrus.FieldLogger if it can
func (l Logrus) SetOutput(w io.Writer) {
if lg, ok := l.FieldLogger.(Outable); ok {
lg.SetOutput(w)
}
}
// WithField returns a new Logger with the field added
func (l Logrus) WithField(s string, i interface{}) FieldLogger {
return Logrus{l.FieldLogger.WithField(s, i)}
}
// WithFields returns a new Logger with the fields added
func (l Logrus) WithFields(m map[string]interface{}) FieldLogger {
return Logrus{l.FieldLogger.WithFields(m)}
}

View file

@ -1,8 +0,0 @@
package logger
import "io"
// Outable interface for loggers that allow setting the output writer
type Outable interface {
SetOutput(out io.Writer)
}

View file

@ -1,19 +0,0 @@
// +build !appengine
package logger
import (
"io"
"os"
"golang.org/x/term"
)
func checkIfTerminal(w io.Writer) bool {
switch v := w.(type) {
case *os.File:
return term.IsTerminal(int(v.Fd()))
default:
return false
}
}

View file

@ -1,11 +0,0 @@
// +build appengine
package logger
import (
"io"
)
func checkIfTerminal(w io.Writer) bool {
return true
}

View file

@ -1,4 +0,0 @@
package logger
// Version of the logger
const Version = "v1.0.1"

View file

@ -1,29 +0,0 @@
*.log
.DS_Store
doc
tmp
pkg
*.gem
*.pid
coverage
coverage.data
build/*
*.pbxuser
*.mode1v3
.svn
profile
.console_history
.sass-cache/*
.rake_tasks~
*.log.lck
solr/
.jhw-cache/
jhw.*
*.sublime*
node_modules/
dist/
generated/
.vendor/
bin/*
gin-bin
.idea/

View file

@ -1,61 +0,0 @@
TAGS ?= ""
GO_BIN ?= "go"
install:
$(GO_BIN) install -tags ${TAGS} -v .
make tidy
tidy:
ifeq ($(GO111MODULE),on)
$(GO_BIN) mod tidy
else
echo skipping go mod tidy
endif
deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
make tidy
build:
$(GO_BIN) build -v .
make tidy
test:
$(GO_BIN) test -cover -tags ${TAGS} ./...
make tidy
ci-deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
ci-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
lint:
go get github.com/golangci/golangci-lint/cmd/golangci-lint
golangci-lint run --enable-all
make tidy
update:
ifeq ($(GO111MODULE),on)
rm go.*
$(GO_BIN) mod init
$(GO_BIN) mod tidy
else
$(GO_BIN) get -u -tags ${TAGS}
endif
make test
make install
make tidy
release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
make tidy
release:
$(GO_BIN) get github.com/gobuffalo/release
make tidy
release -y -f version.go --skip-packr
make tidy

View file

@ -1,24 +0,0 @@
<p align="center"><img src="https://github.com/gobuffalo/buffalo/blob/master/logo.svg" width="360"></p>
<p align="center">
<a href="https://godoc.org/github.com/gobuffalo/packd"><img src="https://godoc.org/github.com/gobuffalo/packd?status.svg" alt="GoDoc" /></a>
<a href="https://travis-ci.org/gobuffalo/packd"><img src="https://travis-ci.org/gobuffalo/packd.svg?branch=master" alt="Build Status" /></a>
<a href="https://goreportcard.com/report/github.com/gobuffalo/packd"><img src="https://goreportcard.com/badge/github.com/gobuffalo/packd" alt="Go Report Card" /></a>
</p>
# github.com/gobuffalo/packd
This is a collection of interfaces designed to make using [github.com/gobuffalo/packr](https://github.com/gobuffalo/packr) easier, and to make the transition between v1 and v2 as seamless as possible.
They can, and should, be used for testing, alternate Box implementations, etc...
## Installation
```bash
$ go get -u -v github.com/gobuffalo/packd
```
## Memory Box
The [`packd#MemoryBox`](https://godoc.org/github.com/gobuffalo/packd#MemoryBox) is a complete, thread-safe, implementation of [`packd#Box`](https://godoc.org/github.com/gobuffalo/packd#Box)

View file

@ -1,10 +0,0 @@
# github.com/gobuffalo/packd Stands on the Shoulders of Giants
github.com/gobuffalo/packd does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants, this project would not be possible. Please make sure to check them out and thank them for all of their hard work.
Thank you to the following **GIANTS**:
* [github.com/davecgh/go-spew](https://godoc.org/github.com/davecgh/go-spew)
* [github.com/stretchr/testify](https://godoc.org/github.com/stretchr/testify)

View file

@ -1,126 +0,0 @@
package packd
import (
"bytes"
"fmt"
"io"
"os"
"time"
)
var _ File = &virtualFile{}
var _ io.Reader = &virtualFile{}
var _ io.Writer = &virtualFile{}
var _ fmt.Stringer = &virtualFile{}
type virtualFile struct {
io.Reader
name string
info fileInfo
original []byte
}
func (f virtualFile) Name() string {
return f.name
}
func (f *virtualFile) Seek(offset int64, whence int) (int64, error) {
return f.Reader.(*bytes.Reader).Seek(offset, whence)
}
func (f virtualFile) FileInfo() (os.FileInfo, error) {
return f.info, nil
}
func (f *virtualFile) Close() error {
return nil
}
func (f virtualFile) Readdir(count int) ([]os.FileInfo, error) {
return []os.FileInfo{f.info}, nil
}
func (f virtualFile) Stat() (os.FileInfo, error) {
return f.info, nil
}
func (f virtualFile) String() string {
return string(f.original)
}
// Read reads the next len(p) bytes from the virtualFile and
// rewind read offset to 0 when it met EOF.
func (f *virtualFile) Read(p []byte) (int, error) {
i, err := f.Reader.Read(p)
if i == 0 || err == io.EOF {
f.Seek(0, io.SeekStart)
}
return i, err
}
// Write copies byte slice p to content of virtualFile.
func (f *virtualFile) Write(p []byte) (int, error) {
return f.write(p)
}
// write copies byte slice or data from io.Reader to content of the
// virtualFile and update related information of the virtualFile.
func (f *virtualFile) write(d interface{}) (c int, err error) {
bb := &bytes.Buffer{}
switch d.(type) {
case []byte:
c, err = bb.Write(d.([]byte))
case io.Reader:
if d != nil {
i64, e := io.Copy(bb, d.(io.Reader))
c = int(i64)
err = e
}
default:
err = fmt.Errorf("unknown type of argument")
}
if err != nil {
return c, err
}
f.info.size = int64(c)
f.info.modTime = time.Now()
f.original = bb.Bytes()
f.Reader = bytes.NewReader(f.original)
return c, nil
}
// NewFile returns a new "virtual" file
func NewFile(name string, r io.Reader) (File, error) {
return buildFile(name, r)
}
// NewDir returns a new "virtual" directory
func NewDir(name string) (File, error) {
v, err := buildFile(name, nil)
if err != nil {
return v, err
}
v.info.isDir = true
return v, nil
}
func buildFile(name string, r io.Reader) (*virtualFile, error) {
vf := &virtualFile{
name: name,
info: fileInfo{
Path: name,
modTime: time.Now(),
},
}
var err error
if r != nil {
_, err = vf.write(r)
} else {
_, err = vf.write([]byte{}) // for safety
}
return vf, err
}

View file

@ -1,39 +0,0 @@
package packd
import (
"os"
"time"
)
var _ os.FileInfo = fileInfo{}
type fileInfo struct {
Path string
size int64
modTime time.Time
isDir bool
}
func (f fileInfo) Name() string {
return f.Path
}
func (f fileInfo) Size() int64 {
return f.size
}
func (f fileInfo) Mode() os.FileMode {
return 0444
}
func (f fileInfo) ModTime() time.Time {
return f.modTime
}
func (f fileInfo) IsDir() bool {
return f.isDir
}
func (f fileInfo) Sys() interface{} {
return nil
}

View file

@ -1,83 +0,0 @@
package packd
import (
"fmt"
"io"
"net/http"
"os"
)
type WalkFunc func(string, File) error
// Box represents the entirety of the necessary
// interfaces to form a "full" box.
// github.com/gobuffalo/packr#Box is an example of this interface.
type Box interface {
HTTPBox
Lister
Addable
Finder
Walkable
Haser
}
type Haser interface {
Has(string) bool
}
type Walker interface {
Walk(wf WalkFunc) error
}
type Walkable interface {
Walker
WalkPrefix(prefix string, wf WalkFunc) error
}
type Finder interface {
Find(string) ([]byte, error)
FindString(name string) (string, error)
}
type HTTPBox interface {
Open(name string) (http.File, error)
}
type Lister interface {
List() []string
}
type Addable interface {
AddString(path string, t string) error
AddBytes(path string, t []byte) error
}
type SimpleFile interface {
fmt.Stringer
io.Reader
io.Writer
Name() string
}
type HTTPFile interface {
SimpleFile
io.Closer
io.Seeker
Readdir(count int) ([]os.FileInfo, error)
Stat() (os.FileInfo, error)
}
type File interface {
HTTPFile
FileInfo() (os.FileInfo, error)
}
// LegacyBox represents deprecated methods
// that older Box implementations might have had.
// github.com/gobuffalo/packr v1 is an example of a LegacyBox.
type LegacyBox interface {
String(name string) string
MustString(name string) (string, error)
Bytes(name string) []byte
MustBytes(name string) ([]byte, error)
}

View file

@ -1,29 +0,0 @@
*.log
.DS_Store
doc
tmp
pkg
*.gem
*.pid
coverage
coverage.data
build/*
*.pbxuser
*.mode1v3
.svn
profile
.console_history
.sass-cache/*
.rake_tasks~
*.log.lck
solr/
.jhw-cache/
jhw.*
*.sublime*
node_modules/
dist/
generated/
.vendor/
bin/*
gin-bin
.idea/

View file

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2019 Mark Bates
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,61 +0,0 @@
TAGS ?= ""
GO_BIN ?= "go"
install:
$(GO_BIN) install -tags ${TAGS} -v .
make tidy
tidy:
ifeq ($(GO111MODULE),on)
$(GO_BIN) mod tidy
else
echo skipping go mod tidy
endif
deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
make tidy
build:
$(GO_BIN) build -v .
make tidy
test:
$(GO_BIN) test -cover -tags ${TAGS} ./...
make tidy
ci-deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
ci-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
lint:
go get github.com/golangci/golangci-lint/cmd/golangci-lint
golangci-lint run --enable-all
make tidy
update:
ifeq ($(GO111MODULE),on)
rm go.*
$(GO_BIN) mod init
$(GO_BIN) mod tidy
else
$(GO_BIN) get -u -tags ${TAGS}
endif
make test
make install
make tidy
release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
make tidy
release:
$(GO_BIN) get github.com/gobuffalo/release
make tidy
release -y -f version.go --skip-packr
make tidy

View file

@ -1,6 +0,0 @@
# github.com/markbates/errx Stands on the Shoulders of Giants
github.com/markbates/errx does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants, this project would not be possible. Please make sure to check them out and thank them for all of their hard work.
Thank you to the following **GIANTS**:

View file

@ -1,71 +0,0 @@
variables:
GOBIN: "$(GOPATH)/bin" # Go binaries path
GOPATH: "$(system.defaultWorkingDirectory)/gopath" # Go workspace path
modulePath: "$(GOPATH)/src/github.com/$(build.repository.name)" # Path to the module"s code
jobs:
- job: Windows
pool:
vmImage: "vs2017-win2016"
strategy:
matrix:
go 1.10:
go_version: "1.10"
go 1.11 (on):
go_version: "1.11.5"
GO111MODULE: "on"
go 1.11 (off):
go_version: "1.11.5"
GO111MODULE: "off"
go 1.12 (on):
go_version: "1.12"
GO111MODULE: "on"
go 1.12 (off):
go_version: "1.12"
GO111MODULE: "off"
steps:
- template: azure-tests.yml
- job: macOS
pool:
vmImage: "macOS-10.13"
strategy:
matrix:
go 1.10:
go_version: "1.10"
go 1.11 (on):
go_version: "1.11.5"
GO111MODULE: "on"
go 1.11 (off):
go_version: "1.11.5"
GO111MODULE: "off"
go 1.12 (on):
go_version: "1.12"
GO111MODULE: "on"
go 1.12 (off):
go_version: "1.12"
GO111MODULE: "off"
steps:
- template: azure-tests.yml
- job: Linux
pool:
vmImage: "ubuntu-16.04"
strategy:
matrix:
go 1.10:
go_version: "1.10"
go 1.11 (on):
go_version: "1.11.5"
GO111MODULE: "on"
go 1.11 (off):
go_version: "1.11.5"
GO111MODULE: "off"
go 1.12 (on):
go_version: "1.12"
GO111MODULE: "on"
go 1.12 (off):
go_version: "1.12"
GO111MODULE: "off"
steps:
- template: azure-tests.yml

View file

@ -1,19 +0,0 @@
steps:
- task: GoTool@0
inputs:
version: $(go_version)
- task: Bash@3
inputs:
targetType: inline
script: |
mkdir -p "$(GOBIN)"
mkdir -p "$(GOPATH)/pkg"
mkdir -p "$(modulePath)"
shopt -s extglob
mv !(gopath) "$(modulePath)"
displayName: "Setup Go Workspace"
- script: |
go get -t -v ./...
go test -race ./...
workingDirectory: "$(modulePath)"
displayName: "Tests"

View file

@ -1,23 +0,0 @@
package errx
// go2 errors
type Wrapper interface {
Unwrap() error
}
// pkg/errors
type Causer interface {
Cause() error
}
func Unwrap(err error) error {
switch e := err.(type) {
case Wrapper:
return e.Unwrap()
case Causer:
return e.Cause()
}
return err
}
var Cause = Unwrap

View file

@ -1,4 +0,0 @@
package errx
// Version of errx
const Version = "v1.0.0"

View file

@ -1,70 +0,0 @@
package packd
import (
"sort"
"sync"
)
// ByteMap wraps sync.Map and uses the following types:
// key: string
// value: []byte
type ByteMap struct {
data sync.Map
}
// Delete the key from the map
func (m *ByteMap) Delete(key string) {
m.data.Delete(key)
}
// Load the key from the map.
// Returns []byte or bool.
// A false return indicates either the key was not found
// or the value is not of type []byte
func (m *ByteMap) Load(key string) ([]byte, bool) {
i, ok := m.data.Load(key)
if !ok {
return []byte(``), false
}
s, ok := i.([]byte)
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *ByteMap) LoadOrStore(key string, value []byte) ([]byte, bool) {
i, _ := m.data.LoadOrStore(key, value)
s, ok := i.([]byte)
return s, ok
}
// Range over the []byte values in the map
func (m *ByteMap) Range(f func(key string, value []byte) bool) {
m.data.Range(func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return false
}
value, ok := v.([]byte)
if !ok {
return false
}
return f(key, value)
})
}
// Store a []byte in the map
func (m *ByteMap) Store(key string, value []byte) {
m.data.Store(key, value)
}
// Keys returns a list of keys in the map
func (m *ByteMap) Keys() []string {
var keys []string
m.Range(func(key string, value []byte) bool {
keys = append(keys, key)
return true
})
sort.Strings(keys)
return keys
}

View file

@ -1,156 +0,0 @@
package packd
import (
"bytes"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"github.com/gobuffalo/packd/internal/takeon/github.com/markbates/errx"
)
var _ Addable = NewMemoryBox()
var _ Finder = NewMemoryBox()
var _ Lister = NewMemoryBox()
var _ HTTPBox = NewMemoryBox()
var _ Haser = NewMemoryBox()
var _ Walkable = NewMemoryBox()
var _ Box = NewMemoryBox()
// MemoryBox is a thread-safe, in-memory, implementation of the Box interface.
type MemoryBox struct {
files *ByteMap
}
func (m *MemoryBox) Has(path string) bool {
_, ok := m.files.Load(path)
return ok
}
func (m *MemoryBox) List() []string {
var names []string
m.files.Range(func(key string, value []byte) bool {
names = append(names, key)
return true
})
sort.Strings(names)
return names
}
func (m *MemoryBox) Open(path string) (http.File, error) {
cpath := strings.TrimPrefix(path, "/")
if filepath.Ext(cpath) == "" {
// it's a directory
return NewDir(path)
}
if len(cpath) == 0 {
cpath = "index.html"
}
b, err := m.Find(cpath)
if err != nil {
return nil, err
}
cpath = filepath.FromSlash(cpath)
f, err := NewFile(cpath, bytes.NewReader(b))
if err != nil {
return nil, err
}
return f, nil
}
func (m *MemoryBox) FindString(path string) (string, error) {
bb, err := m.Find(path)
return string(bb), err
}
func (m *MemoryBox) Find(path string) (ret []byte, e error) {
res, ok := m.files.Load(path)
if !ok {
var b []byte
lpath := strings.ToLower(path)
err := m.Walk(func(p string, file File) error {
lp := strings.ToLower(p)
if lp != lpath {
return nil
}
res := file.String()
b = []byte(res)
return nil
})
if err != nil {
return b, os.ErrNotExist
}
if len(b) == 0 {
return b, os.ErrNotExist
}
return b, nil
}
return res, nil
}
func (m *MemoryBox) AddString(path string, t string) error {
return m.AddBytes(path, []byte(t))
}
func (m *MemoryBox) AddBytes(path string, t []byte) error {
m.files.Store(path, t)
return nil
}
func (m *MemoryBox) Walk(wf WalkFunc) error {
var err error
m.files.Range(func(path string, b []byte) bool {
var f File
f, err = NewFile(path, bytes.NewReader(b))
if err != nil {
return false
}
err = wf(path, f)
if err != nil {
if errx.Unwrap(err) == filepath.SkipDir {
err = nil
return true
}
return false
}
return true
})
if errx.Unwrap(err) == filepath.SkipDir {
return nil
}
return err
}
func (m *MemoryBox) WalkPrefix(pre string, wf WalkFunc) error {
return m.Walk(func(path string, file File) error {
if strings.HasPrefix(path, pre) {
return wf(path, file)
}
return nil
})
}
func (m *MemoryBox) Remove(path string) {
m.files.Delete(path)
m.files.Delete(strings.ToLower(path))
}
// NewMemoryBox returns a configured *MemoryBox
func NewMemoryBox() *MemoryBox {
return &MemoryBox{
files: &ByteMap{},
}
}

View file

@ -1,43 +0,0 @@
package packd
import (
"path/filepath"
"strings"
)
var CommonSkipPrefixes = []string{".", "_", "node_modules", "vendor"}
// SkipWalker will walk the Walker and call the WalkFunc for files who's directories
// do no match any of the skipPrefixes. If no skipPrefixes are passed, then
// CommonSkipPrefixes is used
func SkipWalker(walker Walker, skipPrefixes []string, wf WalkFunc) error {
if len(skipPrefixes) == 0 {
skipPrefixes = append(skipPrefixes, CommonSkipPrefixes...)
}
return walker.Walk(func(path string, file File) error {
fi, err := file.FileInfo()
if err != nil {
return err
}
path = strings.Replace(path, "\\", "/", -1)
parts := strings.Split(path, "/")
if !fi.IsDir() {
parts = parts[:len(parts)-1]
}
for _, base := range parts {
if base != "." {
for _, skip := range skipPrefixes {
skip = strings.ToLower(skip)
lbase := strings.ToLower(base)
if strings.HasPrefix(lbase, skip) {
return filepath.SkipDir
}
}
}
}
return wf(path, file)
})
}

View file

@ -1,4 +0,0 @@
package packd
// Version of packd
const Version = "v0.3.0"

View file

@ -1,3 +0,0 @@
{
"Enable": ["vet", "golint", "goimports", "deadcode", "gotype", "ineffassign", "misspell", "nakedret", "unconvert", "megacheck", "varcheck"]
}

View file

@ -1,42 +0,0 @@
# Code generated by github.com/gobuffalo/release. DO NOT EDIT.
# Edit .goreleaser.yml.plush instead
builds:
-
goos:
- darwin
- linux
- windows
goarch:
- ppc64le
- 386
- amd64
env:
- CGO_ENABLED=0
ignore:
- goos: darwin
goarch: ppc64le
- goos: windows
goarch: ppc64le
main: ./packr2/main.go
binary: packr2
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
brews:
-
github:
owner: gobuffalo
name: homebrew-tap

View file

@ -1,39 +0,0 @@
builds:
-
goos:
- darwin
- linux
- windows
goarch:
- ppc64le
- 386
- amd64
env:
- CGO_ENABLED=0
ignore:
- goos: darwin
goarch: ppc64le
- goos: windows
goarch: ppc64le
main: ./packr2/main.go
binary: packr2
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
<%= if (brew) { %>
brews:
-
github:
owner: gobuffalo
name: homebrew-tap
<% } %>

View file

@ -1,8 +0,0 @@
The MIT License (MIT)
Copyright (c) 2016 Mark Bates
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -1,46 +0,0 @@
TAGS ?= "sqlite"
GO_BIN ?= go
install: deps
echo "installing packr v2"
packr2
$(GO_BIN) install -v ./packr2
tidy:
ifeq ($(GO111MODULE),on)
$(GO_BIN) mod tidy
else
echo skipping go mod tidy
endif
deps:
$(GO_BIN) get -tags ${TAGS} -t ./...
$(GO_BIN) install -v ./packr2
make tidy
build: deps
packr2
$(GO_BIN) build -v ./packr2
make tidy
test:
packr2
$(GO_BIN) test -tags ${TAGS} ./...
make tidy
lint:
gometalinter --vendor ./... --deadline=1m --skip=internal
update:
$(GO_BIN) get -u -tags ${TAGS} ./...
make tidy
make install
make test
make tidy
release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
release:
release -y -f version.go
make tidy

View file

@ -1,239 +0,0 @@
**NOTICE: Please consider migrating your projects to github.com/markbates/pkger. It has an idiomatic API, minimal dependencies, a stronger test suite (tested directly against the std lib counterparts), transparent tooling, and more.**
https://blog.gobuffalo.io/introducing-pkger-static-file-embedding-in-go-1ce76dc79c65
# Packr (v2)
[![GoDoc](https://godoc.org/github.com/gobuffalo/packr/v2?status.svg)](https://godoc.org/github.com/gobuffalo/packr/v2)
Packr is a simple solution for bundling static assets inside of Go binaries. Most importantly it does it in a way that is friendly to developers while they are developing.
## Intro Video
To get an idea of the what and why of Packr, please enjoy this short video: [https://vimeo.com/219863271](https://vimeo.com/219863271).
## Library Installation
```text
$ go get -u github.com/gobuffalo/packr/v2/...
```
## Binary Installation
```text
$ go get -u github.com/gobuffalo/packr/v2/packr2
```
## New File Format FAQs
In version `v2.0.0` the file format changed and is not backward compatible with the `packr-v1.x` library.
#### Can `packr-v1.x` read the new format?
No, it can not. Because of the way the new file format works porting it to `packr-v1.x` would be difficult. PRs are welcome though. :)
#### Can `packr-v2.x` read `packr-v1.x` files?
Yes it can, but that ability will eventually be phased out. Because of that we recommend moving to the new format.
#### Can `packr-v2.x` generate `packr-v1.x` files?
Yes it can, but that ability will eventually be phased out. Because of that we recommend moving to the new format.
The `--legacy` command is available on all commands that generate `-packr.go` files.
```bash
$ packr2 --legacy
```
## Usage
### In Code
The first step in using Packr is to create a new box. A box represents a folder on disk. Once you have a box you can get `string` or `[]byte` representations of the file.
```go
// set up a new box by giving it a name and an optional (relative) path to a folder on disk:
box := packr.New("My Box", "./templates")
// Get the string representation of a file, or an error if it doesn't exist:
html, err := box.FindString("index.html")
// Get the []byte representation of a file, or an error if it doesn't exist:
html, err := box.Find("index.html")
```
### What is a Box?
A box represents a folder, and any sub-folders, on disk that you want to have access to in your binary. When compiling a binary using the `packr2` CLI the contents of the folder will be converted into Go files that can be compiled inside of a "standard" go binary. Inside of the compiled binary the files will be read from memory. When working locally the files will be read directly off of disk. This is a seamless switch that doesn't require any special attention on your part.
#### Example
Assume the follow directory structure:
```
├── main.go
└── templates
├── admin
│   └── index.html
└── index.html
```
The following program will read the `./templates/admin/index.html` file and print it out.
```go
package main
import (
"fmt"
"github.com/gobuffalo/packr/v2"
)
func main() {
box := packr.New("myBox", "./templates")
s, err := box.FindString("admin/index.html")
if err != nil {
log.Fatal(err)
}
fmt.Println(s)
}
```
### Development Made Easy
In order to get static files into a Go binary, those files must first be converted to Go code. To do that, Packr, ships with a few tools to help build binaries. See below.
During development, however, it is painful to have to keep running a tool to compile those files.
Packr uses the following resolution rules when looking for a file:
1. Look for the file in-memory (inside a Go binary)
1. Look for the file on disk (during development)
Because Packr knows how to fall through to the file system, developers don't need to worry about constantly compiling their static files into a binary. They can work unimpeded.
Packr takes file resolution a step further. When declaring a new box you use a relative path, `./templates`. When Packr receives this call it calculates out the absolute path to that directory. By doing this it means you can be guaranteed that Packr can find your files correctly, even if you're not running in the directory that the box was created in. This helps with the problem of testing, where Go changes the `pwd` for each package, making relative paths difficult to work with. This is not a problem when using Packr.
---
## Usage with HTTP
A box implements the [`http.FileSystem`](https://golang.org/pkg/net/http/#FileSystem) interface, meaning it can be used to serve static files.
```go
package main
import (
"net/http"
"github.com/gobuffalo/packr/v2"
)
func main() {
box := packr.New("someBoxName", "./templates")
http.Handle("/", http.FileServer(box))
http.ListenAndServe(":3000", nil)
}
```
---
## Building a Binary
Before you build your Go binary, run the `packr2` command first. It will look for all the boxes in your code and then generate `.go` files that pack the static files into bytes that can be bundled into the Go binary.
```
$ packr2
```
Then run your `go build command` like normal.
*NOTE*: It is not recommended to check-in these generated `-packr.go` files. They can be large, and can easily become out of date if not careful. It is recommended that you always run `packr2 clean` after running the `packr2` tool.
#### Cleaning Up
When you're done it is recommended that you run the `packr2 clean` command. This will remove all of the generated files that Packr created for you.
```
$ packr2 clean
```
Why do you want to do this? Packr first looks to the information stored in these generated files, if the information isn't there it looks to disk. This makes it easy to work with in development.
---
## Debugging
The `packr2` command passes all arguments down to the underlying `go` command, this includes the `-v` flag to print out `go build` information. Packr looks for the `-v` flag, and will turn on its own verbose logging. This is very useful for trying to understand what the `packr` command is doing when it is run.
---
## FAQ
### Compilation Errors with Go Templates
Q: I have a program with Go template files, those files are named `foo.go` and look like the following:
```
// Copyright {{.Year}} {{.Author}}. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package {{.Project}}
```
When I run `packr2` I get errors like:
```
expected 'IDENT', found '{'
```
A: Packr works by searching your `.go` files for [`github.com/gobuffalo/packr/v2#New`](https://godoc.org/github.com/gobuffalo/packr/v2#New) or [`github.com/gobuffalo/packr/v2#NewBox`](https://godoc.org/github.com/gobuffalo/packr/v2#NewBox) calls. Because those files aren't "proper" Go files, Packr can't parse them to find the box declarations. To fix this you need to tell Packr to ignore those files when searching for boxes. A couple solutions to this problem are:
* Name the files something else. The `.tmpl` extension is the idiomatic way of naming these types of files.
* Rename the folder containing these files to start with an `_`, for example `_templates`. Packr, like Go, will ignore folders starting with the `_` character when searching for boxes.
### Dynamic Box Paths
Q: I need to set the path of a box using a variable, but `packr.New("foo", myVar)` doesn't work correctly.
A: Packr attempts to "automagically" set it's resolution directory when using [`github.com/gobuffalo/packr/v2#New`](https://godoc.org/github.com/gobuffalo/packr/v2#New), however, for dynamic paths you need to set it manually:
```go
box := packr.New("foo", "|")
box.ResolutionDir = myVar
```
### I don't want to pack files, but still use the Packr interface.
Q: I want to write code that using the Packr tools, but doesn't actually pack the files into my binary. How can I do that?
A: Using [`github.com/gobuffalo/packr/v2#Folder`](https://godoc.org/github.com/gobuffalo/packr/v2#Folder) gives you back a `*packr.Box` that can be used as normal, but is excluded by the Packr tool when compiling.
### Packr Finds No Boxes
Q: I run `packr2 -v` but it doesn't find my boxes:
```
DEBU[2019-03-18T18:48:52+01:00] *parser.Parser#NewFromRoots found prospects=0
DEBU[2019-03-18T18:48:52+01:00] found 0 boxes
```
A: Packr works by parsing `.go` files to find [`github.com/gobuffalo/packr/v2#Box`](https://godoc.org/github.com/gobuffalo/packr/v2#Box) and [`github.com/gobuffalo/packr/v2#NewBox`](https://godoc.org/github.com/gobuffalo/packr/v2#NewBox) declarations. If there aren't any `.go` in the folder that `packr2` is run in it can not find those declarations. To fix this problem run the `packr2` command in the directory containing your `.go` files.
### Box Interfaces
Q: I want to be able to easily test my applications by passing in mock boxes. How do I do that?
A: Packr boxes and files conform to the interfaces found at [`github.com/gobuffalo/packd`](https://godoc.org/github.com/gobuffalo/packd). Change your application to use those interfaces instead of the concrete Packr types.
```go
// using concrete type
func myFunc(box *packr.Box) {}
// using interfaces
func myFunc(box packd.Box) {}
```

View file

@ -1,32 +0,0 @@
# /Users/smichalak/dev/packr/v2 Stands on the Shoulders of Giants
/Users/smichalak/dev/packr/v2 does not try to reinvent the wheel! Instead, it uses the already great wheels developed by the Go community and puts them all together in the best way possible. Without these giants, this project would not be possible. Please make sure to check them out and thank them for all of their hard work.
Thank you to the following **GIANTS**:
* [github.com/gobuffalo/logger](https://godoc.org/github.com/gobuffalo/logger)
* [github.com/gobuffalo/packd](https://godoc.org/github.com/gobuffalo/packd)
* [github.com/karrick/godirwalk](https://godoc.org/github.com/karrick/godirwalk)
* [github.com/konsorten/go-windows-terminal-sequences](https://godoc.org/github.com/konsorten/go-windows-terminal-sequences)
* [github.com/markbates/errx](https://godoc.org/github.com/markbates/errx)
* [github.com/markbates/oncer](https://godoc.org/github.com/markbates/oncer)
* [github.com/markbates/safe](https://godoc.org/github.com/markbates/safe)
* [github.com/rogpeppe/go-internal](https://godoc.org/github.com/rogpeppe/go-internal)
* [github.com/sirupsen/logrus](https://godoc.org/github.com/sirupsen/logrus)
* [github.com/spf13/cobra](https://godoc.org/github.com/spf13/cobra)
* [github.com/stretchr/testify](https://godoc.org/github.com/stretchr/testify)
* [golang.org/x/sync](https://godoc.org/golang.org/x/sync)
* [golang.org/x/tools](https://godoc.org/golang.org/x/tools)

View file

@ -1,240 +0,0 @@
package packr
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/gobuffalo/packd"
"github.com/gobuffalo/packr/v2/file"
"github.com/gobuffalo/packr/v2/file/resolver"
"github.com/gobuffalo/packr/v2/plog"
"github.com/markbates/oncer"
)
var _ packd.Box = &Box{}
var _ packd.HTTPBox = &Box{}
var _ packd.Addable = &Box{}
var _ packd.Walkable = &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)
}
// 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)
if b != nil {
return b
}
b = construct(name, path)
plog.Debug(b, "New", "Box", b, "ResolutionDir", b.ResolutionDir)
b, err := placeBox(b)
if err != nil {
panic(err)
}
return b
}
// Folder returns a Box that will NOT be packed.
// This is useful for writing tests or tools that
// need to work with a folder at runtime.
func Folder(path string) *Box {
return New(path, path)
}
// 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)
plog.Debug(b, "SetResolver", "file", file, "resolver", fmt.Sprintf("%T", res))
b.resolvers.Store(resolver.Key(file), res)
}
// AddString converts t to a byteslice and delegates to AddBytes to add to b.data
func (b *Box) AddString(path string, t string) error {
return b.AddBytes(path, []byte(t))
}
// AddBytes sets t in b.data by the given path
func (b *Box) AddBytes(path string, t []byte) error {
m := map[string]file.File{}
f, err := file.NewFile(path, t)
if err != nil {
return err
}
m[resolver.Key(path)] = f
res := resolver.NewInMemory(m)
b.SetResolver(path, res)
return nil
}
// 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) {
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) {
f, err := b.Resolve(name)
if err != nil {
return []byte(""), err
}
bb := &bytes.Buffer{}
io.Copy(bb, f)
return bb.Bytes(), nil
}
// Has returns true if the resource exists in the box
func (b *Box) Has(name string) bool {
_, err := b.Find(name)
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"+b.Name, func() {
for _, f := range b.List() {
for d := filepath.Dir(f); d != "."; d = filepath.Dir(d) {
b.dirs.Store(d, true)
}
}
})
if name == "/" {
return b.Has("index.html")
}
_, ok := b.dirs.Load(name)
return ok
}
// Open returns a File using the http.File interface
func (b *Box) Open(name string) (http.File, error) {
plog.Debug(b, "Open", "name", name)
f, err := b.Resolve(name)
if err != nil {
if len(filepath.Ext(name)) == 0 {
return b.openWoExt(name)
}
return f, err
}
f, err = file.NewFileR(name, f)
plog.Debug(b, "Open", "name", f.Name(), "file", f.Name())
return f, err
}
func (b *Box) openWoExt(name string) (http.File, error) {
if !b.HasDir(name) {
id := path.Join(name, "index.html")
if b.Has(id) {
return b.Open(id)
}
return nil, os.ErrNotExist
}
d, err := file.NewDir(name)
plog.Debug(b, "Open", "name", name, "dir", d)
return d, err
}
// List shows "What's in the box?"
func (b *Box) List() []string {
var keys []string
b.Walk(func(path string, info File) error {
if info == nil {
return nil
}
finfo, _ := info.FileInfo()
if !finfo.IsDir() {
keys = append(keys, path)
}
return nil
})
sort.Strings(keys)
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, "/")
var r resolver.Resolver
b.resolvers.Range(func(k string, vr resolver.Resolver) bool {
lk := strings.ToLower(resolver.Key(k))
lkey := strings.ToLower(resolver.Key(key))
if lk == lkey {
r = vr
return false
}
return true
})
if r == nil {
r = b.DefaultResolver
if r == nil {
r = resolver.DefaultResolver
if r == nil {
return nil, fmt.Errorf("resolver.DefaultResolver is nil")
}
}
}
plog.Debug(r, "Resolve", "box", b.Name, "key", key)
f, err := r.Resolve(b.Name, key)
if err != nil {
z, err := resolver.ResolvePathInBase(resolver.OsPath(b.ResolutionDir), filepath.FromSlash(path.Clean("/"+resolver.OsPath(key))))
if err != nil {
plog.Debug(r, "Resolve", "box", b.Name, "key", key, "err", err)
return f, err
}
f, err = r.Resolve(b.Name, z)
if err != nil {
plog.Debug(r, "Resolve", "box", b.Name, "key", z, "err", err)
return f, err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return f, err
}
f, err = file.NewFile(key, b)
if err != nil {
return f, err
}
}
plog.Debug(r, "Resolve", "box", b.Name, "key", key, "file", f.Name())
return f, nil
}

View file

@ -1,73 +0,0 @@
//go:generate mapgen -name "box" -zero "nil" -go-type "*Box" -pkg "" -a "New(`test-a`, ``)" -b "New(`test-b`, ``)" -c "New(`test-c`, ``)" -bb "New(`test-bb`, ``)" -destination "packr"
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
package packr
import (
"sort"
"sync"
)
// boxMap wraps sync.Map and uses the following types:
// key: string
// value: *Box
type boxMap struct {
data sync.Map
}
// Delete the key from the map
func (m *boxMap) Delete(key string) {
m.data.Delete(key)
}
// Load the key from the map.
// Returns *Box or bool.
// A false return indicates either the key was not found
// or the value is not of type *Box
func (m *boxMap) Load(key string) (*Box, bool) {
i, ok := m.data.Load(key)
if !ok {
return nil, false
}
s, ok := i.(*Box)
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *boxMap) LoadOrStore(key string, value *Box) (*Box, bool) {
i, _ := m.data.LoadOrStore(key, value)
s, ok := i.(*Box)
return s, ok
}
// Range over the *Box values in the map
func (m *boxMap) Range(f func(key string, value *Box) bool) {
m.data.Range(func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return false
}
value, ok := v.(*Box)
if !ok {
return false
}
return f(key, value)
})
}
// Store a *Box in the map
func (m *boxMap) Store(key string, value *Box) {
m.data.Store(key, value)
}
// Keys returns a list of keys in the map
func (m *boxMap) Keys() []string {
var keys []string
m.Range(func(key string, value *Box) bool {
keys = append(keys, key)
return true
})
sort.Strings(keys)
return keys
}

Some files were not shown because too many files have changed in this diff Show more