Skip to content

Commit

Permalink
refactor: add version endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bhunter234 committed Jan 11, 2025
1 parent 3252c20 commit 7fa4a38
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 69 deletions.
17 changes: 1 addition & 16 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,7 @@ jobs:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- uses: pnpm/action-setup@v4
with:
version: 9

- name: Build UI
run: |
git clone https://github.com/tgdrive/teldrive-ui
cd teldrive-ui
pnpm install --frozen-lockfile
export VITE_SERVER_VERSION=$GITHUB_REF_NAME
pnpm run build
cd ..
mv teldrive-ui/dist ui/dist
rm -rf teldrive-ui

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
Expand Down
27 changes: 21 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ env:

before:
hooks:
- go generate ./...
- make frontend
- make gen

builds:
- env:
Expand All @@ -16,7 +17,8 @@ builds:
ldflags:
- -extldflags=-static
- -s -w
- -X {{ .ModulePath }}/internal/config.Version={{ .Version }}
- -X {{ .ModulePath }}/internal/version.Version={{ .Version }}
- -X {{ .ModulePath }}/internal/version.CommitSHA={{ .ShortCommit }}
mod_timestamp: "{{ .CommitTimestamp }}"
goos:
- linux
Expand Down Expand Up @@ -64,15 +66,28 @@ docker_manifests:
image_templates:
- "ghcr.io/tgdrive/{{ .ProjectName }}:{{ .Tag }}-amd64"
- "ghcr.io/tgdrive/{{ .ProjectName }}:{{ .Tag }}-arm64"

changelog:
use: github
sort: asc
groups:
- title: Features
regexp: "^.*feat[(\\w)]*:+.*$"
order: 0
- title: Bug fixes
regexp: "^.*fix[(\\w)]*:+.*$"
order: 1
- title: Refactoring
regexp: "^.*refactor[(\\w)]*:+.*$"
order: 2
- title: Others
order: 3
filters:
exclude:
- '^docs:'
- '^test:'
- '^ci:'
- '^README'
- '^Update'
- Merge pull request
- Merge branch

release:
draft: true
137 changes: 100 additions & 37 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,49 +1,88 @@
ifdef ComSpec
SHELL := powershell.exe
IS_WINDOWS := true
else
SHELL := /bin/bash
IS_WINDOWS := false
endif

APP_NAME := teldrive
BUILD_DIR := bin
FRONTEND_DIR := ui/dist
FRONTEND_ASSET := https://github.com/tgdrive/teldrive-ui/releases/download/v1/teldrive-ui.zip
GIT_TAG := $(shell git describe --tags --abbrev=0)
FRONTEND_ASSET := https://github.com/tgdrive/teldrive-ui/releases/download/latest/teldrive-ui.zip
GIT_TAG := $(shell git tag -l '[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n 1)
ifeq ($(GIT_TAG),)
GIT_TAG := 1.0.0
endif
GIT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_LINK := $(shell git remote get-url origin)
MODULE_PATH := $(shell go list -m)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
VERSION:= $(GIT_TAG)
GIT_COMMIT := $(shell git rev-parse --short HEAD)
VERSION_PACKAGE := $(MODULE_PATH)/internal/version
VERSION := $(GIT_TAG)
BINARY_EXTENSION :=
BUILD_TIME :=

ifeq ($(IS_WINDOWS),true)
BINARY_EXTENSION := .exe
RM := powershell -Command "Remove-Item"
RMDIR := powershell -Command "Remove-Item -Recurse -Force"
MKDIR := powershell -Command "New-Item -ItemType Directory -Force"
DOWNLOAD := powershell -Command "Invoke-WebRequest -Uri"
UNZIP := powershell -Command "Expand-Archive"
else
RM := rm -f
RMDIR := rm -rf
MKDIR := mkdir -p
DOWNLOAD := curl -sLO
UNZIP := unzip -q -d
endif

ifeq ($(IS_WINDOWS),true)
BUILD_TIME := $(shell powershell -Command "(Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.fffZ')")
else
BUILD_TIME := $(shell date -u '+%Y-%m-%dT%H:%M:%S.000Z')
endif

.PHONY: all build run clean frontend backend run sync-ui retag patch-version minor-version major-version gen check-semver install-semver

.PHONY: all build run clean frontend backend run sync-ui retag patch-version minor-version gen

all: build

check-semver:
ifeq ($(IS_WINDOWS),true)
@powershell -Command "if (-not (Get-Command semver -ErrorAction SilentlyContinue)) { Write-Host 'Installing semver...'; npm install -g semver }"
else
@which semver > /dev/null || (echo "Installing semver..." && npm install -g semver)
endif

frontend:
@echo "Extract UI"
ifeq ($(OS),Windows_NT)
powershell -Command "Remove-Item -Path $(FRONTEND_DIR) -Recurse -Force"
powershell -Command "Invoke-WebRequest -Uri $(FRONTEND_ASSET) -OutFile teldrive-ui.zip"
powershell -Command "if (!(Test-Path -Path $(subst /,\\,$(FRONTEND_DIR)))) { New-Item -ItemType Directory -Force -Path $(subst /,\\,$(FRONTEND_DIR)) }"
powershell -Command "Expand-Archive -Path teldrive-ui.zip -DestinationPath $(FRONTEND_DIR) -Force"
powershell -Command "Remove-Item -Path teldrive-ui.zip -Force"
$(RMDIR) $(FRONTEND_DIR)
ifeq ($(IS_WINDOWS),true)
$(DOWNLOAD) $(FRONTEND_ASSET) -OutFile teldrive-ui.zip
$(MKDIR) $(subst /,\\,$(FRONTEND_DIR))
$(UNZIP) -Path teldrive-ui.zip -DestinationPath $(FRONTEND_DIR) -Force
$(RM) teldrive-ui.zip
else
rm -rf $(FRONTEND_DIR)
curl -LO $(FRONTEND_ASSET) -o teldrive-ui.zip
mkdir -p $(FRONTEND_DIR)
unzip -d $(FRONTEND_DIR) teldrive-ui.zip
rm -rf teldrive-ui.zip
$(DOWNLOAD) $(FRONTEND_ASSET)
$(MKDIR) $(FRONTEND_DIR)
$(UNZIP) $(FRONTEND_DIR) teldrive-ui.zip
$(RM) teldrive-ui.zip
endif

ifeq ($(OS),Windows_NT)
BINARY_EXTENSION := .exe
endif
gen:
go generate ./...

backend:
backend: gen
@echo "Building backend for $(GOOS)/$(GOARCH)..."
go build -trimpath -ldflags "-s -w -X $(MODULE_PATH)/internal/config.Version=$(VERSION) -extldflags=-static" -o $(BUILD_DIR)/$(APP_NAME)$(BINARY_EXTENSION)
go build -trimpath \
-ldflags "-s -w \
-X '$(VERSION_PACKAGE).Version=$(VERSION)' \
-X '$(VERSION_PACKAGE).CommitSHA=$(GIT_COMMIT)' \
-extldflags=-static" \
-o $(BUILD_DIR)/$(APP_NAME)$(BINARY_EXTENSION)

build: frontend backend
@echo "Building complete."
Expand All @@ -54,29 +93,53 @@ run:

clean:
@echo "Cleaning up..."
rm -rf $(BUILD_DIR)
cd $(FRONTEND_DIR) && rm -rf dist node_modules
$(RMDIR) $(BUILD_DIR)
ifeq ($(IS_WINDOWS),true)
if exist "$(FRONTEND_DIR)" $(RMDIR) "$(FRONTEND_DIR)"
else
$(RMDIR) $(FRONTEND_DIR)
endif

deps:
@echo "Installing Go dependencies..."
go mod download

retag:
@echo "Retagging..."
git tag -d $(GIT_TAG)
git push --delete origin $(GIT_TAG)
@echo "Retagging $(GIT_TAG)..."
-git tag -d $(GIT_TAG)
-git push --delete origin $(GIT_TAG)
git tag -a $(GIT_TAG) -m "Recreated tag $(GIT_TAG)"
git push origin $(GIT_TAG)

patch-version:
@echo "Patching version..."
git tag -a $(shell semver -i patch $(GIT_TAG)) -m "Release $(shell semver -i patch $(GIT_TAG))"
git push origin $(shell semver -i patch $(GIT_TAG))
patch-version: check-semver
@echo "Current version: $(GIT_TAG)"
ifeq ($(GIT_TAG),)
$(eval NEW_VERSION := 1.0.0)
else
$(eval NEW_VERSION := $(shell semver -i patch $(GIT_TAG)))
endif
@echo "Creating new patch version: $(NEW_VERSION)"
git tag -a $(NEW_VERSION) -m "Release $(NEW_VERSION)"
git push origin $(NEW_VERSION)

minor-version:
@echo "Minoring version..."
git tag -a $(shell semver -i minor $(GIT_TAG)) -m "Release $(shell semver -i minor $(GIT_TAG))"
git push origin $(shell semver -i minor $(GIT_TAG))

gen:
go generate ./...
minor-version: check-semver
@echo "Current version: $(GIT_TAG)"
ifeq ($(GIT_TAG),)
$(eval NEW_VERSION := 1.0.0)
else
$(eval NEW_VERSION := $(shell semver -i minor $(GIT_TAG)))
endif
@echo "Creating new minor version: $(NEW_VERSION)"
git tag -a $(NEW_VERSION) -m "Release $(NEW_VERSION)"
git push origin $(NEW_VERSION)

major-version: check-semver
@echo "Current version: $(GIT_TAG)"
ifeq ($(GIT_TAG),)
$(eval NEW_VERSION := 1.0.0)
else
$(eval NEW_VERSION := $(shell semver -i major $(GIT_TAG)))
endif
@echo "Creating new major version: $(NEW_VERSION)"
git tag -a $(NEW_VERSION) -m "Release $(NEW_VERSION)"
git push origin $(NEW_VERSION)
14 changes: 7 additions & 7 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package cmd

import (
"runtime"

"github.com/spf13/cobra"
"github.com/tgdrive/teldrive/internal/config"
"github.com/tgdrive/teldrive/internal/version"
)

func NewVersion() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Check the version info",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Printf("teldrive %s\n", config.Version)
cmd.Printf("- os/type: %s\n", runtime.GOOS)
cmd.Printf("- os/arch: %s\n", runtime.GOARCH)
cmd.Printf("- go/version: %s\n", runtime.Version())
v := version.GetVersionInfo()
cmd.Printf("teldrive %s\n", v.Version)
cmd.Printf("- commit: %s\n", v.CommitSHA)
cmd.Printf("- os/type: %s\n", v.Os)
cmd.Printf("- os/arch: %s\n", v.Arch)
cmd.Printf("- go/version: %s\n", v.GoVersion)
return nil
},
}
Expand Down
3 changes: 0 additions & 3 deletions internal/config/version.go

This file was deleted.

22 changes: 22 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package version

import (
"runtime"

"github.com/tgdrive/teldrive/internal/api"
)

var (
Version = "development"
CommitSHA = "unknown"
)

func GetVersionInfo() *api.ApiVersion {
return &api.ApiVersion{
Version: Version,
CommitSHA: CommitSHA,
GoVersion: runtime.Version(),
Os: runtime.GOOS,
Arch: runtime.GOARCH,
}
}
5 changes: 5 additions & 0 deletions pkg/services/api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/tgdrive/teldrive/internal/config"
"github.com/tgdrive/teldrive/internal/kv"
"github.com/tgdrive/teldrive/internal/tgc"
"github.com/tgdrive/teldrive/internal/version"
"gorm.io/gorm"
)

Expand All @@ -26,6 +27,10 @@ type apiService struct {
middlewares []telegram.Middleware
}

func (a *apiService) VersionVersion(ctx context.Context) (*api.ApiVersion, error) {
return version.GetVersionInfo(), nil
}

func (a *apiService) NewError(ctx context.Context, err error) *api.ErrorStatusCode {
var (
code = http.StatusInternalServerError
Expand Down

0 comments on commit 7fa4a38

Please sign in to comment.