Skip to content

Commit

Permalink
Merge pull request #298 from cfergeau/versioning
Browse files Browse the repository at this point in the history
Add --version argument to commands
  • Loading branch information
openshift-merge-bot[bot] authored Dec 21, 2023
2 parents 3a47ad7 + e13e93c commit f01fd1c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pkg/types/version.go export-subst
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TAG ?= $(shell git describe --match=NeVeRmAtCh --always --abbrev=40 --dirty)
GIT_VERSION ?= $(shell git describe --always --dirty)
CONTAINER_RUNTIME ?= podman

.PHONY: build
Expand All @@ -7,7 +8,8 @@ build: gvproxy qemu-wrapper vm
TOOLS_DIR := tools
include tools/tools.mk

LDFLAGS = -s -w
VERSION_LDFLAGS=-X github.com/containers/gvisor-tap-vsock/pkg/types.gitVersion=$(GIT_VERSION)
LDFLAGS = -s -w $(VERSION_LDFLAGS)

.PHONY: gvproxy
gvproxy:
Expand Down
8 changes: 8 additions & 0 deletions cmd/gvproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const (
)

func main() {
version := types.NewVersion("gvproxy")
version.AddFlag()
flag.Var(&endpoints, "listen", "control endpoint")
flag.BoolVar(&debug, "debug", false, "Print debug info")
flag.IntVar(&mtu, "mtu", 1500, "Set the MTU")
Expand All @@ -72,6 +74,12 @@ func main() {
flag.StringVar(&pidFile, "pid-file", "", "Generate a file with the PID in it")
flag.Parse()

if version.ShowVersion() {
fmt.Println(version.String())
os.Exit(0)
}

log.Infof(version.String())
ctx, cancel := context.WithCancel(context.Background())
// Make this the last defer statement in the stack
defer os.Exit(exitCode)
Expand Down
9 changes: 9 additions & 0 deletions cmd/ssh-over-vsock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"flag"
"fmt"
"net"
"os"

"github.com/containers/gvisor-tap-vsock/pkg/transport"
"github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand All @@ -19,13 +21,20 @@ var (
)

func main() {
version := types.NewVersion("ssh-over-vsock")
version.AddFlag()
flag.StringVar(&ip, "ip", "192.168.127.2", "ip of the host")
flag.IntVar(&port, "port", 22, "port of the host")
flag.StringVar(&user, "user", "", "ssh user")
flag.StringVar(&key, "key", "", "ssh key")
flag.StringVar(&endpoint, "url", "/tmp/network.sock", "url of the daemon")
flag.Parse()

if version.ShowVersion() {
fmt.Println(version.String())
os.Exit(0)
}

if err := run(); err != nil {
logrus.Fatal(err)
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/vm/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (
)

func main() {
version := types.NewVersion("gvforwarder")
version.AddFlag()
flag.StringVar(&endpoint, "url", fmt.Sprintf("vsock://2:1024%s", types.ConnectPath), "url where the tap send packets")
flag.StringVar(&iface, "iface", "tap0", "tap interface name")
flag.StringVar(&stopIfIfaceExist, "stop-if-exist", "eth0,ens3,enp0s1", "stop if one of these interfaces exists at startup")
Expand All @@ -44,6 +46,11 @@ func main() {
flag.BoolVar(&tapPreexists, "preexisting", false, "use preexisting/preconfigured TAP interface")
flag.Parse()

if version.ShowVersion() {
fmt.Println(version.String())
os.Exit(0)
}

expected := strings.Split(stopIfIfaceExist, ",")
links, err := netlink.LinkList()
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions cmd/win-sshproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"unsafe"

"github.com/containers/gvisor-tap-vsock/pkg/sshclient"
"github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/winquit/pkg/winquit"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
Expand All @@ -33,10 +34,15 @@ var (
func main() {
args := os.Args
if len(args) > 1 {
if args[1] == "-debug" {
switch args[1] {
case "-version":
version := types.NewVersion("win-sshproxy")
fmt.Println(version.String())
os.Exit(0)
case "-debug":
debug = true
args = args[2:]
} else {
default:
args = args[1:]
}
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/types/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package types

import (
"flag"
"fmt"
"runtime/debug"
"strings"
)

var (
// set using the '-X github.com/containers/gvisor-tap-vsock/pkg/types.gitVersion' linker flag
gitVersion = ""
// set through .gitattributes when `git archive` is used
// see https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/
gitArchiveVersion = "$Format:%(describe)$"
)

type version struct {
binaryName string
showVersion bool
}

func NewVersion(binaryName string) *version { //nolint:revive
return &version{
binaryName: binaryName,
}
}

func (ver *version) String() string {
return fmt.Sprintf("%s version %s", ver.binaryName, moduleVersion())
}

func (ver *version) AddFlag() {
flag.BoolVar(&ver.showVersion, "version", false, "Print version information")
}

func (ver *version) ShowVersion() bool {
return ver.showVersion
}

func moduleVersion() string {
switch {
// This will be substituted when building from a GitHub tarball
case !strings.HasPrefix(gitArchiveVersion, "$Format:"):
return gitArchiveVersion
// This will be set when building from git using make
case gitVersion != "":
return gitVersion
// moduleVersionFromBuildInfo() will be set when using `go install`
default:
return moduleVersionFromBuildInfo()
}
}

func moduleVersionFromBuildInfo() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
if info.Main.Version == "(devel)" {
return ""
}
return info.Main.Version
}

0 comments on commit f01fd1c

Please sign in to comment.