Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --version argument to commands #298

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)$"
Copy link
Contributor

@praveenkumar praveenkumar Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This convert to git describe or git log --pretty=format"$Format:%(describe)$" one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for git log --pretty=format"$Format:%(describe)"$ says

%(describe[:<options>])
human-readable name, like git-describe(1);

so they are both the same?

)

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
}