Skip to content

Commit

Permalink
put version in binary properly (#73)
Browse files Browse the repository at this point in the history
Addresses #71 :

    Set version on the CLI framework.
    Add a build.sh script that injects variables into the build tooling using git and a version file.
    Set version in config.
  • Loading branch information
tsmethurst authored Jun 28, 2021
1 parent 87cf621 commit 4f3b3f5
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ To get started, you first need to have Go installed. GTS was developed with Go 1

Once you've got go installed, clone this repository into your Go path. Normally, this should be `~/go/src/github.com/superseriousbusiness/gotosocial`.

Once that's done, you can try building the project: `go build ./cmd/gotosocial`. This will build the `gotosocial` binary.
Once that's done, you can try building the project: `./build.sh`. This will build the `gotosocial` binary.

If there are no errors, great, you're good to go!

Expand Down
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM golang:1.16.4-alpine3.13 AS builder
RUN apk update && apk upgrade --no-cache
RUN apk add git

# create build dir
RUN mkdir -p /go/src/github.com/superseriousbusiness/gotosocial
Expand All @@ -11,8 +13,15 @@ ADD testrig /go/src/github.com/superseriousbusiness/gotosocial/testrig
ADD go.mod /go/src/github.com/superseriousbusiness/gotosocial/go.mod
ADD go.sum /go/src/github.com/superseriousbusiness/gotosocial/go.sum

# move .git dir and version for versioning
ADD .git /go/src/github.com/superseriousbusiness/gotosocial/.git
ADD version /go/src/github.com/superseriousbusiness/gotosocial/version

# move the build script
ADD build.sh /go/src/github.com/superseriousbusiness/gotosocial/build.sh

# do the build step
RUN go build ./cmd/gotosocial
RUN ./build.sh

FROM alpine:3.13 AS executor
RUN apk update && apk upgrade --no-cache
Expand Down
6 changes: 5 additions & 1 deletion GETTINGSTARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ docker run -d --network host --user postgres -e POSTGRES_PASSWORD=some_password

### 5: Build the Binary

On your local machine (not your server), with Go installed, clone the GoToSocial repository, and build the binary with `go build ./cmd/gotosocial`.
On your local machine (not your server), with Go installed, clone the GoToSocial repository, and build the binary with the provided build script:

```bash
./build/sh
```

### 6: Prepare VPS

Expand Down
8 changes: 8 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

set -eu

export COMMIT=$(git rev-list -1 HEAD)
export VERSION=$(cat ./version)

go build -ldflags="-X 'main.Commit=$COMMIT' -X 'main.Version=$VERSION'" ./cmd/gotosocial
11 changes: 9 additions & 2 deletions cmd/gotosocial/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ import (
"github.com/urfave/cli/v2"
)

// Version is the software version of GtS being used
var Version string

// Commit is the git commit of GtS being used
var Commit string

func main() {
flagNames := config.GetFlagNames()
envNames := config.GetEnvNames()
defaults := config.GetDefaults()
app := &cli.App{
Usage: "a fediverse social media server",
Version: Version + " " + Commit[:7],
Usage: "a fediverse social media server",
Flags: []cli.Flag{
// GENERAL FLAGS
&cli.StringFlag{
Expand Down Expand Up @@ -399,7 +406,7 @@ func runAction(c *cli.Context, a cliactions.GTSAction) error {
return fmt.Errorf("error creating config: %s", err)
}
// ... and the flags set on the *cli.Context by urfave
if err := conf.ParseCLIFlags(c); err != nil {
if err := conf.ParseCLIFlags(c, c.App.Version); err != nil {
return fmt.Errorf("error parsing config: %s", err)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func loadFromFile(path string) (*Config, error) {
}

// ParseCLIFlags sets flags on the config using the provided Flags object
func (c *Config) ParseCLIFlags(f KeyedFlags) error {
func (c *Config) ParseCLIFlags(f KeyedFlags, version string) error {
fn := GetFlagNames()

// For all of these flags, we only want to set them on the config if:
Expand Down Expand Up @@ -261,6 +261,8 @@ func (c *Config) ParseCLIFlags(f KeyedFlags) error {
c.AccountCLIFlags[EmailFlag] = f.String(EmailFlag)
c.AccountCLIFlags[PasswordFlag] = f.String(PasswordFlag)

c.SoftwareVersion = version

return nil
}

Expand Down
4 changes: 0 additions & 4 deletions internal/config/default.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package config

const softwareVersion = "0.1.0-SNAPSHOT"

// TestDefault returns a default config for testing
func TestDefault() *Config {
defaults := GetTestDefaults()
Expand Down Expand Up @@ -121,7 +119,6 @@ func GetDefaults() Defaults {
ConfigPath: "",
Host: "",
Protocol: "https",
SoftwareVersion: softwareVersion,

DbType: "postgres",
DbAddress: "localhost",
Expand Down Expand Up @@ -168,7 +165,6 @@ func GetTestDefaults() Defaults {
ConfigPath: "",
Host: "localhost:8080",
Protocol: "http",
SoftwareVersion: softwareVersion,

DbType: "postgres",
DbAddress: "localhost",
Expand Down
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0-SNAPSHOT
2 changes: 1 addition & 1 deletion web/template/footer.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- footer.tmpl -->
<footer>
<div id="version">
Running GoToSocial version: <span class="accent">{{.instance.Version}}</span><br>
GoToSocial: <span class="accent">{{.instance.Version}}</span><br>
<a href="https://github.com/superseriousbusiness/gotosocial">Source Code</a>
</div>
<div id="contact">
Expand Down

0 comments on commit 4f3b3f5

Please sign in to comment.