From 07d888c2c60d727dab49aa8dbc905cb3ccedd7aa Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Sat, 21 Jan 2023 11:25:30 +1000 Subject: Fix build and versioning (borrowed from yarn) --- .gitignore | 5 +++-- Dockerfile | 3 ++- Makefile | 36 ++++++++++++++++++++++++------- cmd/tube/main.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ doc.go | 2 ++ main.go | 63 ------------------------------------------------------ version.go | 46 +++++++++++++++++++++++++++++++++------ 7 files changed, 139 insertions(+), 81 deletions(-) create mode 100644 cmd/tube/main.go create mode 100644 doc.go delete mode 100644 main.go diff --git a/.gitignore b/.gitignore index 1c7bf4f..655edde 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,9 @@ /dist /tube -tube -/.DS_Store +/cmd/tube/tube + +**/.DS_Store videos/* !videos/README.md diff --git a/Dockerfile b/Dockerfile index e581d4e..513c7fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,9 +39,10 @@ COPY ./utils/*.go ./utils/ # COMMIT value to change. ARG VERSION="0.0.0" ARG COMMIT="HEAD" +ARG BUILD="" # Build server binary -RUN make server VERSION=$VERSION COMMIT=$COMMIT +RUN make server VERSION=$VERSION COMMIT=$COMMIT BUILD=$BUILD # Runtime FROM alpine:latest diff --git a/Makefile b/Makefile index 14952b6..750c0b3 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,10 @@ .PHONY: help deps dev build install image release test clean clean-all export CGO_ENABLED=0 -VERSION=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "0.0.0") -COMMIT=$(shell git rev-parse --short HEAD || echo "HEAD") +VERSION=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "$VERSION") +COMMIT=$(shell git rev-parse --short HEAD || echo "$COMMIT") BRANCH=$(shell git rev-parse --abbrev-ref HEAD) +BUILD=$(shell git show -s --pretty=format:%cI) GOCMD=go GOVER=$(shell go version | grep -o -E 'go1\.17\.[0-9]+') @@ -35,12 +36,23 @@ dev : DEBUG=1 dev : server ## Build debug version of tube @./tube -server: generate ## Build the tube server - @$(GOCMD) build -tags "embed static_build" \ +server: generate ## Build the yarnd server +ifeq ($(DEBUG), 1) + @echo "Building in debug mode..." + @$(GOCMD) build $(FLAGS) -tags "netgo static_build" -installsuffix netgo \ + -ldflags "\ + -X $(shell go list).Version=$(VERSION) \ + -X $(shell go list).Commit=$(COMMIT) \ + -X $(shell go list).Build=$(BUILD)" \ + ./cmd/tube/... +else + @$(GOCMD) build $(FLAGS) -tags "netgo static_build" -installsuffix netgo \ -ldflags "-w \ -X $(shell go list).Version=$(VERSION) \ - -X $(shell go list).Commit=$(COMMIT)" \ - . + -X $(shell go list).Commit=$(COMMIT) \ + -X $(shell go list).Build=$(BUILD)" \ + ./cmd/tube/... +endif build: server ## Build the server @@ -54,10 +66,18 @@ install: build ## Install tube to $DESTDIR ifeq ($(PUBLISH), 1) image: generate ## Build the Docker image - @docker buildx build --push --platform linux/arm64,linux/amd64 --tag $(IMAGE):$(TAG) --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" . + @docker buildx build \ + --build-arg VERSION="$(VERSION)" \ + --build-arg COMMIT="$(COMMIT)" \ + --build-arg BUILD="$(BUILD)" \ + --platform linux/amd64,linux/arm64 --push -t $(IMAGE):$(TAG) . else image: generate - @docker buildx build --platform linux/arm64,linux/amd64 --tag $(IMAGE):$(TAG) --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" . + @docker build \ + --build-arg VERSION="$(VERSION)" \ + --build-arg COMMIT="$(COMMIT)" \ + --build-arg BUILD="$(BUILD)" \ + -t $(IMAGE):$(TAG) . endif release: generate ## Release a new version to Gitea diff --git a/cmd/tube/main.go b/cmd/tube/main.go new file mode 100644 index 0000000..cc3bfd2 --- /dev/null +++ b/cmd/tube/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "os" + + log "github.com/sirupsen/logrus" + flag "github.com/spf13/pflag" + + "git.mills.io/prologic/tube" + "git.mills.io/prologic/tube/app" +) + +var ( + debug bool + version bool + config string +) + +func init() { + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: %s [options] [file]\n", os.Args[0]) + flag.PrintDefaults() + } + + flag.BoolVarP(&version, "version", "v", false, "display version information") + flag.BoolVarP(&debug, "debug", "d", false, "enable debug logging") + flag.StringVarP(&config, "config", "c", "config.json", "path to configuration file") +} + +func main() { + flag.Parse() + + if debug { + log.SetLevel(log.DebugLevel) + } else { + log.SetLevel(log.InfoLevel) + } + + if version { + fmt.Printf("tube version %s", tube.FullVersion()) + os.Exit(0) + } + + cfg := app.DefaultConfig() + log.Infof("Reading configuration from %s", config) + err := cfg.ReadFile(config) + if err != nil { + if flag.Lookup("config").Changed { + log.Fatal(err) + } + log.Infof("Reading %s failed. Starting with builtin defaults.", config) + } + // I'd like to add something like this here: log.Debug("Active config: %s", cfg.toJson()) + a, err := app.NewApp(cfg) + if err != nil { + log.Fatal(err) + } + addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) + log.Printf("Local server: http://%s", addr) + err = a.Run() + if err != nil { + log.Fatal(err) + } +} diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..a2ecf83 --- /dev/null +++ b/doc.go @@ -0,0 +1,2 @@ +// Package tube is a Youtube-like (without censorship and features you don't need!) Video Sharing App +package tube diff --git a/main.go b/main.go deleted file mode 100644 index d18e7dc..0000000 --- a/main.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "fmt" - "os" - - "git.mills.io/prologic/tube/app" - log "github.com/sirupsen/logrus" - flag "github.com/spf13/pflag" -) - -var ( - debug bool - version bool - config string -) - -func init() { - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: %s [options] [file]\n", os.Args[0]) - flag.PrintDefaults() - } - - flag.BoolVarP(&version, "version", "v", false, "display version information") - flag.BoolVarP(&debug, "debug", "d", false, "enable debug logging") - flag.StringVarP(&config, "config", "c", "config.json", "path to configuration file") -} - -func main() { - flag.Parse() - - if debug { - log.SetLevel(log.DebugLevel) - } else { - log.SetLevel(log.InfoLevel) - } - - if version { - fmt.Printf("tube version %s", FullVersion()) - os.Exit(0) - } - - cfg := app.DefaultConfig() - log.Infof("Reading configuration from %s", config) - err := cfg.ReadFile(config) - if err != nil { - if flag.Lookup("config").Changed { - log.Fatal(err) - } - log.Infof("Reading %s failed. Starting with builtin defaults.", config) - } - // I'd like to add something like this here: log.Debug("Active config: %s", cfg.toJson()) - a, err := app.NewApp(cfg) - if err != nil { - log.Fatal(err) - } - addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) - log.Printf("Local server: http://%s", addr) - err = a.Run() - if err != nil { - log.Fatal(err) - } -} diff --git a/version.go b/version.go index 86587e6..d4f60a9 100644 --- a/version.go +++ b/version.go @@ -1,18 +1,50 @@ -package main +package tube import ( "fmt" + runtime "runtime/debug" + "strings" +) + +const ( + defaultVersion = "0.0.0" + defaultCommit = "HEAD" + defaultBuild = "0000-01-01:00:00+00:00" ) var ( - // Version release version - Version = "0.0.1" + // Version is the tagged release version in the form .. + // following semantic versioning and is overwritten by the build system. + Version = defaultVersion - // Commit will be overwritten automatically by the build system - Commit = "HEAD" + // Commit is the commit sha of the build (normally from Git) and is overwritten + // by the build system. + Commit = defaultCommit + + // Build is the date and time of the build as an RFC3339 formatted string + // and is overwritten by the build system. + Build = defaultBuild ) -// FullVersion returns the full version and commit hash +// FullVersion display the full version and build func FullVersion() string { - return fmt.Sprintf("%s@%s", Version, Commit) + var sb strings.Builder + + isDefault := Version == defaultVersion && Commit == defaultCommit && Build == defaultBuild + + if !isDefault { + sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build)) + } + + if info, ok := runtime.ReadBuildInfo(); ok { + if isDefault { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Version)) + } + sb.WriteString(fmt.Sprintf(" %s", info.GoVersion)) + if info.Main.Sum != "" { + sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum)) + } + } + + return sb.String() } -- cgit v1.2.3