mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Add --version option
* Add flag to display the version for each binary. It is based on `git describe` but adds a number to the PATCH level if we are between annotated tags, so makes it semver.org compatible. Use the "-ldflags" method that also works with go 1.17. * Use Makefile bash and sed magic to do PATCH level increase if needed. Co-authored-by: Bernhard Reiter <bernhard@intevation.de>
This commit is contained in:
parent
de9c2b9663
commit
6fe6907c1d
5 changed files with 57 additions and 6 deletions
18
Makefile
18
Makefile
|
|
@ -37,6 +37,22 @@ tag_checked_out:
|
||||||
git checkout -q tags/${BUILDTAG}
|
git checkout -q tags/${BUILDTAG}
|
||||||
@echo Don\'t forget that we are in checked out tag $(BUILDTAG) now.
|
@echo Don\'t forget that we are in checked out tag $(BUILDTAG) now.
|
||||||
|
|
||||||
|
# use bash shell arithmetic and sed to turn a `git describe` version
|
||||||
|
# into a semver version. For this we increase the PATCH number, so that
|
||||||
|
# any commit after a tag is considered newer than the semver from the tag
|
||||||
|
# without an optional 'v'
|
||||||
|
GITDESC := $(shell git describe)
|
||||||
|
GITDESCPATCH := $(shell echo '$(GITDESC)' | sed -E 's/v?[0-9]+\.[0-9]+\.([0-9]+)[-+]?.*/\1/')
|
||||||
|
SEMVERPATCH := $(shell echo $$(( $(GITDESCPATCH) + 1 )))
|
||||||
|
# Hint: The regexp in the next line only matches if there is a hyphen (`-`)
|
||||||
|
# and we can assume that git describe has added a string after the tag
|
||||||
|
SEMVER := $(shell echo '$(GITDESC)' | sed -E 's/v?([0-9]+\.[0-9]+\.)([0-9]+)(-.*)/\1$(SEMVERPATCH)\3/' )
|
||||||
|
testsemver:
|
||||||
|
@echo from \'$(GITDESC)\' transformed to \'$(SEMVER)\'
|
||||||
|
|
||||||
|
|
||||||
|
# Set -ldflags parameter to pass the semversion.
|
||||||
|
LDFLAGS = -ldflags "-X github.com/csaf-poc/csaf_distribution/util.SemVersion=$(SEMVER)"
|
||||||
|
|
||||||
# Build binaries and place them under bin-$(GOOS)-$(GOARCH)
|
# Build binaries and place them under bin-$(GOOS)-$(GOARCH)
|
||||||
# Using 'Target-specific Variable Values' to specify the build target system
|
# Using 'Target-specific Variable Values' to specify the build target system
|
||||||
|
|
@ -48,7 +64,7 @@ build_win: GOOS = windows
|
||||||
build_linux build_win:
|
build_linux build_win:
|
||||||
$(eval BINDIR = bin-$(GOOS)-$(GOARCH)/ )
|
$(eval BINDIR = bin-$(GOOS)-$(GOARCH)/ )
|
||||||
$(MKDIR) $(BINDIR)
|
$(MKDIR) $(BINDIR)
|
||||||
env GOARCH=$(GOARCH) GOOS=$(GOOS) $(BUILD) -o $(BINDIR) -v ./cmd/...
|
env GOARCH=$(GOARCH) GOOS=$(GOOS) $(BUILD) -o $(BINDIR) $(LDFLAGS) -v ./cmd/...
|
||||||
|
|
||||||
|
|
||||||
# Remove bin-*-* directories
|
# Remove bin-*-* directories
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
//
|
//
|
||||||
// SPDX-FileCopyrightText: 2021 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
||||||
// Software-Engineering: 2021 Intevation GmbH <https://intevation.de>
|
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
|
@ -12,11 +12,13 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
_ "embed" // Used for embedding.
|
_ "embed" // Used for embedding.
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/csaf-poc/csaf_distribution/util"
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -29,6 +31,7 @@ type options struct {
|
||||||
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider"`
|
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider"`
|
||||||
ClientCert *string `long:"client-cert" description:"TLS client certificate file (PEM encoded data)" value-name:"CERT-FILE"`
|
ClientCert *string `long:"client-cert" description:"TLS client certificate file (PEM encoded data)" value-name:"CERT-FILE"`
|
||||||
ClientKey *string `long:"client-key" description:"TLS client private key file (PEM encoded data)" value-name:"KEY-FILE"`
|
ClientKey *string `long:"client-key" description:"TLS client private key file (PEM encoded data)" value-name:"KEY-FILE"`
|
||||||
|
Version bool `long:"version" description:"Display version of the binary"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func errCheck(err error) {
|
func errCheck(err error) {
|
||||||
|
|
@ -132,6 +135,11 @@ func main() {
|
||||||
domains, err := flags.Parse(opts)
|
domains, err := flags.Parse(opts)
|
||||||
errCheck(err)
|
errCheck(err)
|
||||||
|
|
||||||
|
if opts.Version {
|
||||||
|
fmt.Println(util.SemVersion)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if len(domains) == 0 {
|
if len(domains) == 0 {
|
||||||
log.Println("No domains given.")
|
log.Println("No domains given.")
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -9,16 +9,32 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http/cgi"
|
"net/http/cgi"
|
||||||
|
|
||||||
|
"github.com/csaf-poc/csaf_distribution/util"
|
||||||
|
"github.com/jessevdk/go-flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type options struct {
|
||||||
|
Version bool `long:"version" description:"Display version of the binary"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg, err := loadConfig()
|
cfg, err := loadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: %v\n", err)
|
log.Fatalf("error: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var opts options
|
||||||
|
parser := flags.NewParser(&opts, flags.Default)
|
||||||
|
parser.Parse()
|
||||||
|
if opts.Version {
|
||||||
|
fmt.Println(util.SemVersion)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c, err := newController(cfg)
|
c, err := newController(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: %v\n", err)
|
log.Fatalf("error: %v\n", err)
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
|
|
||||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||||
"github.com/csaf-poc/csaf_distribution/csaf"
|
"github.com/csaf-poc/csaf_distribution/csaf"
|
||||||
|
"github.com/csaf-poc/csaf_distribution/util"
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
@ -48,7 +49,8 @@ type options struct {
|
||||||
|
|
||||||
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider"`
|
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider"`
|
||||||
|
|
||||||
Config *string `short:"c" long:"config" description:"Path to config ini file" value-name:"INI-FILE" no-ini:"true"`
|
Config *string `short:"c" long:"config" description:"Path to config ini file" value-name:"INI-FILE" no-ini:"true"`
|
||||||
|
Version bool `long:"version" description:"Display version of the binary"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type processor struct {
|
type processor struct {
|
||||||
|
|
@ -359,6 +361,11 @@ func main() {
|
||||||
args, err := parser.Parse()
|
args, err := parser.Parse()
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
|
if opts.Version {
|
||||||
|
fmt.Println(util.SemVersion)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if opts.Config != nil {
|
if opts.Config != nil {
|
||||||
iniParser := flags.NewIniParser(parser)
|
iniParser := flags.NewIniParser(parser)
|
||||||
iniParser.ParseAsDefaults = true
|
iniParser.ParseAsDefaults = true
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
//
|
//
|
||||||
// SPDX-FileCopyrightText: 2021 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
||||||
// Software-Engineering: 2021 Intevation GmbH <https://intevation.de>
|
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>
|
||||||
|
|
||||||
package util
|
package util
|
||||||
|
|
||||||
|
|
@ -17,6 +17,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SemVersion the version in semver.org format, MUST be overwritten during
|
||||||
|
// the linking stage of the build process
|
||||||
|
var SemVersion = "0.0.0"
|
||||||
|
|
||||||
// NWriter is an io.Writer counting the bytes copied through it.
|
// NWriter is an io.Writer counting the bytes copied through it.
|
||||||
type NWriter struct {
|
type NWriter struct {
|
||||||
io.Writer
|
io.Writer
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue