Skip to content

Commit

Permalink
Expose app.Main(), add version to binaries, fix docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Apr 1, 2024
1 parent 5a10aad commit 169584a
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 142 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ go tool dist list

rm -rf dist

GOOS=linux GOARCH=amd64 go build -buildmode exe -o ./dist/xtemplate-amd64-linux/xtemplate ./cmd
GOOS=darwin GOARCH=amd64 go build -buildmode exe -o ./dist/xtemplate-amd64-darwin/xtemplate ./cmd
GOOS=windows GOARCH=amd64 go build -buildmode exe -o ./dist/xtemplate-amd64-windows/xtemplate.exe ./cmd
VERSION="$(go list -f '{{.Version}}' -m github.com/infogulch/xtemplate@`git rev-parse HEAD`)"
LDFLAGS="-X 'github.com/infogulch/xtemplate/app.version=$VERSION'"

GOOS=linux GOARCH=amd64 go build -ldflags="$LDFLAGS" -buildmode exe -o ./dist/xtemplate-amd64-linux/xtemplate ./app/cmd
GOOS=darwin GOARCH=amd64 go build -ldflags="$LDFLAGS" -buildmode exe -o ./dist/xtemplate-amd64-darwin/xtemplate ./app/cmd
GOOS=windows GOARCH=amd64 go build -ldflags="$LDFLAGS" -buildmode exe -o ./dist/xtemplate-amd64-windows/xtemplate.exe ./app/cmd

docker build -t "xtemplate:$VERSION" --build-arg LDFLAGS="$LDFLAGS" .

# Get version from image to spot check that the binary can run:
echo "Build docker image with version: $(docker run -i --rm "xtemplate:$VERSION" --version)"

cd dist

printf '%s\n' * | while read D; do
cp ../README.md ../LICENSE "$D"
tar czvf "$D.tar.gz" "$D/"
zip -r9 "$D.zip" "$D/"
tar czvf "${D}_$VERSION.tar.gz" "$D/"
zip -r9 "${D}_$VERSION.zip" "$D/"
rm -r "$D"
done

Expand Down
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM golang:1-alpine AS builder

RUN apk add --no-cache build-base

ARG LDFLAGS

ENV USER=appuser
ENV UID=10001
RUN adduser \
Expand All @@ -13,16 +15,17 @@ RUN adduser \
--uid "${UID}" \
"${USER}"

WORKDIR /build
COPY go.mod go.sum ./
WORKDIR /build/app
COPY app/go.mod app/go.sum /build/app/
COPY go.mod go.sum /build/
RUN go mod download

COPY . .
COPY . /build/
RUN CGO_ENABLED=1 \
GOFLAGS='-tags="sqlite_json"' \
GOOS=linux \
GOARCH=amd64 \
go build -ldflags="-w -s" -o /dist/xtemplate ./cmd
go build -ldflags="${LDFLAGS}" -o /dist/xtemplate ./cmd
RUN ldd /dist/xtemplate | tr -s [:blank:] '\n' | grep ^/ | xargs -I % install -D % /dist/%
RUN ln -s ld-musl-x86_64.so.1 /dist/lib/libc.musl-x86_64.so.1

Expand Down
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@

## next

- [x] Add version information to binaries
- [x] Fix docker build, add to ci
- [x] Expose Main() so Go lib users can use it, moving cmd to app/cmd
- Documentation
- [x] Fix formatting; see https://pkg.go.dev/github.com/fluhus/godoc-tricks
- [x] Have .Resp handle response serving for fs files.
Expand Down
File renamed without changes.
File renamed without changes.
27 changes: 14 additions & 13 deletions cmd/main.go → app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Default CLI package. To customize, copy this file to a new unique package and
// import dbs and provide config overrides.
package main
package app

import (
"bytes"
Expand All @@ -15,14 +13,9 @@ import (
"github.com/infogulch/watch"

_ "github.com/infogulch/xtemplate/providers"
_ "modernc.org/sqlite"
)

func main() {
Main()
}

type configt struct {
type Args struct {
xtemplate.Config
Watch []string `json:"watch_dirs" arg:",separate"`
WatchTemplates bool `json:"watch_templates" default:"true"`
Expand All @@ -32,11 +25,19 @@ type configt struct {
ConfigFiles []string `json:"-" arg:"-f,--config-file,separate"`
}

var version = "development"

func (Args) Version() string {
return version
}

// Main can be called from your func main() if you want your program to act like
// the default xtemplate cli, or use it as a reference for making your own.
// Provide configs to override the defaults like: `xtemplate.Main(xtemplate.WithFooConfig())`
// Provide configs to override the defaults like:
//
// app.Main(xtemplate.WithFooConfig())
func Main(overrides ...xtemplate.ConfigOverride) {
var config configt
var config Args
var log *slog.Logger

{
Expand All @@ -46,7 +47,7 @@ func Main(overrides ...xtemplate.ConfigOverride) {
level := config.LogLevel
log = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.Level(level)}))

var jsonConfig configt
var jsonConfig Args
var decoded bool
for _, name := range config.ConfigFiles {
func() {
Expand Down Expand Up @@ -96,7 +97,7 @@ func Main(overrides ...xtemplate.ConfigOverride) {
os.Exit(2)
}

if config.WatchTemplates {
if config.WatchTemplates && config.TemplatesFS == nil {
config.Watch = append(config.Watch, config.TemplatesDir)
}
if len(config.Watch) != 0 {
Expand Down
14 changes: 14 additions & 0 deletions app/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Default CLI package. To customize, copy this file to a new unique package and
// import dbs and provide config overrides.
package main

import (
"github.com/infogulch/xtemplate/app"

_ "github.com/infogulch/xtemplate/providers"
_ "github.com/mattn/go-sqlite3"
)

func main() {
app.Main()
}
30 changes: 11 additions & 19 deletions cmd/go.mod → app/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/infogulch/xtemplate/cmd
module github.com/infogulch/xtemplate/app

go 1.22.1

Expand All @@ -9,49 +9,41 @@ replace github.com/infogulch/xtemplate v0.0.0-local => ../
require (
github.com/alexflint/go-arg v1.4.3
github.com/infogulch/watch v0.2.0
modernc.org/sqlite v1.29.5
github.com/mattn/go-sqlite3 v1.14.22
)

replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.16

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/alecthomas/chroma/v2 v2.12.0 // indirect
github.com/alexflint/go-scalar v1.1.0 // indirect
github.com/alecthomas/chroma/v2 v2.13.0 // indirect
github.com/alexflint/go-scalar v1.2.0 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/imdario/mergo v1.0.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/tdewolff/minify/v2 v2.20.18 // indirect
github.com/tdewolff/minify/v2 v2.20.19 // indirect
github.com/tdewolff/parse/v2 v2.7.12 // indirect
github.com/yuin/goldmark v1.7.0 // indirect
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.41.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)
Loading

0 comments on commit 169584a

Please sign in to comment.