From c93127a59b93524eb04db0f93f2791cc937d6911 Mon Sep 17 00:00:00 2001 From: Maxime Le Conte des Floris Date: Tue, 27 Apr 2021 23:41:07 +0200 Subject: [PATCH] feat: display dyndns version in discord username And display release date when running dyndns --version --- .github/workflows/release.yml | 10 +++++----- internal/build/build.go | 19 +++++++++++++++++++ internal/discord/discord.go | 3 +++ main.go | 17 ++--------------- 4 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 internal/build/build.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ab72c19..3884b89 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,11 +17,11 @@ jobs: uses: actions/checkout@v2 - name: Build binaries run: | - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags release -o "dyndns-$(git describe --tags)-linux-amd64" -ldflags "-X main.Version=$(git describe --tags)" - CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -tags release -o "dyndns-$(git describe --tags)-linux-arm" -ldflags "-X main.Version=$(git describe --tags)" - CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -tags release -o "dyndns-$(git describe --tags)-linux-arm64" -ldflags "-X main.Version=$(git describe --tags)" - CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -tags release -o "dyndns-$(git describe --tags)-darwin-amd64" -ldflags "-X main.Version=$(git describe --tags)" - CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -tags release -o "dyndns-$(git describe --tags)-windows-amd64.exe" -ldflags "-X main.Version=$(git describe --tags)" + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags release -o "dyndns-$(git describe --tags)-linux-amd64" -ldflags "-X 'github.com/mlcdf/dyndns/internal/build.Version=$(git describe --tags)' -X 'github.com/mlcdf/dyndns/internal/build.Time=$(date)'" + CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -tags release -o "dyndns-$(git describe --tags)-linux-arm" -ldflags "-X 'github.com/mlcdf/dyndns/internal/build.Version=$(git describe --tags)' -X 'github.com/mlcdf/dyndns/internal/build.Time=$(date)'" + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -tags release -o "dyndns-$(git describe --tags)-linux-arm64" -ldflags "-X 'github.com/mlcdf/dyndns/internal/build.Version=$(git describe --tags)' -X 'github.com/mlcdf/dyndns/internal/build.Time=$(date)'" + CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -tags release -o "dyndns-$(git describe --tags)-darwin-amd64" -ldflags "-X 'github.com/mlcdf/dyndns/internal/build.Version=$(git describe --tags)' -X 'github.com/mlcdf/dyndns/internal/build.Time=$(date)'" + CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -tags release -o "dyndns-$(git describe --tags)-windows-amd64.exe" -ldflags "-X 'github.com/mlcdf/dyndns/internal/build.Version=$(git describe --tags)' -X 'github.com/mlcdf/dyndns/internal/build.Time=$(date)'" - name: Upload release artifacts uses: actions/github-script@v3 with: diff --git a/internal/build/build.go b/internal/build/build.go new file mode 100644 index 0000000..89d5217 --- /dev/null +++ b/internal/build/build.go @@ -0,0 +1,19 @@ +package build + +// Version is set at build time using -ldflags="-X 'github.com/mlcdf/dyndns/internal/build.Version=dyndns v1.0.0' +var Version = "(devel)" + +// Time is set at build time using -ldflags="-X 'github.com/mlcdf/dyndns/internal/build.Time=$(date)' +var Time string + +func Short() string { + return "dyndns " + Version +} + +func Long() string { + info := Short() + if Time != "" { + info = info + " (release date: " + Time + ")" + } + return info +} diff --git a/internal/discord/discord.go b/internal/discord/discord.go index a82d5b8..410e701 100644 --- a/internal/discord/discord.go +++ b/internal/discord/discord.go @@ -8,6 +8,8 @@ import ( "net/http" "os" "time" + + "github.com/mlcdf/dyndns/internal/build" ) type Client struct { @@ -86,6 +88,7 @@ func (c *Client) PostSuccess(webhook *Webhook) error { func (c *Client) Post(webhook *Webhook) error { webhook.Embeds[0].TimeStamp = time.Now().UTC().Format(time.RFC3339) webhook.Embeds[0].Footer.Text = hostname + webhook.Username = build.Short() payload, err := json.Marshal(webhook) if err != nil { diff --git a/main.go b/main.go index 34df999..916a141 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,8 @@ import ( "io" "log" "os" - "runtime/debug" + "github.com/mlcdf/dyndns/internal/build" "github.com/mlcdf/dyndns/internal/discord" "github.com/mlcdf/dyndns/internal/dyndns" "github.com/mlcdf/dyndns/internal/gandi" @@ -31,11 +31,6 @@ How to create a Discord webhook: https://support.discord.com/hc/en-us/articles/2 How to generate your Gandi token: https://docs.gandi.net/en/domain_names/advanced_users/api.html ` -// Version can be set at link time to override debug.BuildInfo.Main.Version, -// which is "(devel)" when building from within the module. See -// golang.org/issue/29814 and golang.org/issue/29228. -var Version string - func main() { log.SetFlags(0) flag.Usage = func() { fmt.Fprint(os.Stderr, usage) } @@ -65,15 +60,7 @@ func main() { flag.Parse() if versionFlag { - if Version != "" { - fmt.Println(Version) - return - } - if buildInfo, ok := debug.ReadBuildInfo(); ok { - fmt.Println(buildInfo.Main.Version) - return - } - fmt.Println("(unknown)") + fmt.Println(build.Long()) return }