Skip to content

Commit

Permalink
Feature:Inject build information into the binary
Browse files Browse the repository at this point in the history
* update version handler.
* update .gorealeser.
* update .Taskifile.
* update release action.
  • Loading branch information
rugwirobaker committed Oct 1, 2020
1 parent 87cec89 commit ab46d8d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: 1.14

- name: capture current date
id: date
run: echo "::set-output name=date::$(date -u '+%Y-%m-%d-%H:%M:%S-%Z')"

-
name: release
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
DATE: ${{ steps.date.outputs.date }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ builds:
- windows
- darwin
binary: helmes
ldflags:
- -X "github.com/rugwirobaker/helmes.version={{ .Version }}"
- -X "github.com/rugwirobaker/helmes.buildDate={{ .Env.DATE }}"

archives:
- replacements:
darwin: Darwin
Expand Down
8 changes: 7 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ tasks:
build:
cmds:
- echo "compiling binary..."
- CGO_ENABLED=0 go build -o bin/helmes ./cmd/helmes
- CGO_ENABLED=0 go build -ldflags="{{.LDFLAGS}}" -o bin/helmes ./cmd/helmes
vars:
VERSION:
sh: git rev-parse --short HEAD
DATE:
sh: date -u +%Y-%m-%d-%H:%M:%S-%Z
LDFLAGS: "-X github.com/rugwirobaker/helmes.version={{.VERSION}} -X github.com/rugwirobaker/helmes.buildDate={{.DATE}}"

run:
deps: [build]
Expand Down
4 changes: 2 additions & 2 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func SMSHandler(svc helmes.Service) http.HandlerFunc {
// VersionHandler ...
func VersionHandler(svc helmes.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ver, err := svc.Version(r.Context())
build, err := svc.Version(r.Context())
if err != nil {
http.Error(w, err.Error(), 500)
}
w.Write([]byte(ver))
JSON(w, build, http.StatusOK)
}
}

Expand Down
24 changes: 24 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package helmes

import "fmt"

// Build information
type Build struct {
Version string `json:"version,omitempty"`
Date string `json:"date,omitempty"`
}

var version, buildDate string = "unset", "unset"

// String version of Build
func String() string {
return fmt.Sprintf("Build Details:\n\tVersion:\t%s\n\tDate:\t\t%s", version, buildDate)
}

// Data returns build details as a struct
func Data() *Build {
return &Build{
Version: version,
Date: buildDate,
}
}
6 changes: 3 additions & 3 deletions helmes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Service interface {
Send(context.Context, *SMS) (*Report, error)

//Version returns helmes's current running version
Version(context.Context) (string, error)
Version(context.Context) (*Build, error)
}

type service struct {
Expand Down Expand Up @@ -74,8 +74,8 @@ func (s *service) Send(ctx context.Context, message *SMS) (*Report, error) {
return convertReport(report), nil
}

func (s *service) Version(ctx context.Context) (string, error) {
return "v0.1.0", nil
func (s *service) Version(ctx context.Context) (*Build, error) {
return Data(), nil
}

func convertReport(report *sms.Report) *Report {
Expand Down

0 comments on commit ab46d8d

Please sign in to comment.