Skip to content

Commit

Permalink
Add buildinfo flag
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Bozhenko <[email protected]>
  • Loading branch information
alexbozhenko committed Dec 19, 2024
1 parent 470a7ac commit b5fe72d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Profiling Options:
Common Options:
-h, --help Show this message
-v, --version Show version
--buildinfo Show buildinfo
--help_tls TLS help
`

Expand All @@ -106,6 +107,7 @@ func main() {
opts, err := server.ConfigureOptions(fs, os.Args[1:],
server.PrintServerAndExit,
fs.Usage,
server.PrintBuildinfoAndExit,
server.PrintTLSHelpAndDie)
if err != nil {
server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err))
Expand Down
16 changes: 13 additions & 3 deletions server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5676,10 +5676,11 @@ func getDefaultAuthTimeout(tls *tls.Config, tlsTimeout float64) float64 {
// specific flags. On success, an options structure is returned configured
// based on the selected flags and/or configuration file.
// The command line options take precedence to the ones in the configuration file.
func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp, printTLSHelp func()) (*Options, error) {
func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp, printBuildinfo, printTLSHelp func()) (*Options, error) {
opts := &Options{}
var (
showVersion bool
showBuildInfo bool
showHelp bool
showTLSHelp bool
signal string
Expand Down Expand Up @@ -5735,6 +5736,7 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
fs.StringVar(&opts.RemoteSyslog, "remote_syslog", _EMPTY_, "Syslog server addr (udp://127.0.0.1:514).")
fs.BoolVar(&showVersion, "version", false, "Print version information.")
fs.BoolVar(&showVersion, "v", false, "Print version information.")
fs.BoolVar(&showBuildInfo, "buildinfo", false, "Print buildinfo.")
fs.IntVar(&opts.ProfPort, "profile", 0, "Profiling HTTP port.")
fs.StringVar(&opts.RoutesStr, "routes", _EMPTY_, "Routes to actively solicit a connection.")
fs.StringVar(&opts.Cluster.ListenStr, "cluster", _EMPTY_, "Cluster url from which members can solicit routes.")
Expand Down Expand Up @@ -5772,6 +5774,11 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
return nil, nil
}

if showBuildInfo {
printBuildinfo()
return nil, nil
}

if showHelp {
printHelp()
return nil, nil
Expand All @@ -5783,8 +5790,8 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
}

// Process args looking for non-flag options,
// 'version' and 'help' only for now
showVersion, showHelp, err = ProcessCommandLineArgs(fs)
// 'version', 'help' and 'buildinfo' only for now
showVersion, showHelp, showBuildInfo, err = ProcessCommandLineArgs(fs)
if err != nil {
return nil, err
} else if showVersion {
Expand All @@ -5793,6 +5800,9 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
} else if showHelp {
printHelp()
return nil, nil
} else if showBuildInfo {
printBuildinfo()
return nil, nil
}

// Snapshot flag options.
Expand Down
22 changes: 12 additions & 10 deletions server/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1434,19 +1434,21 @@ func TestConfigureOptions(t *testing.T) {
usage := func() { panic("should not get there") }
var fs *flag.FlagSet
type testPrint struct {
args []string
version, help, tlsHelp func()
args []string
version, help, tlsHelp, buildinfo func()
}
testFuncs := []testPrint{
{[]string{"-v"}, checkPrintInvoked, usage, PrintTLSHelpAndDie},
{[]string{"version"}, checkPrintInvoked, usage, PrintTLSHelpAndDie},
{[]string{"-h"}, PrintServerAndExit, checkPrintInvoked, PrintTLSHelpAndDie},
{[]string{"help"}, PrintServerAndExit, checkPrintInvoked, PrintTLSHelpAndDie},
{[]string{"-help_tls"}, PrintServerAndExit, usage, checkPrintInvoked},
{[]string{"-v"}, checkPrintInvoked, usage, PrintTLSHelpAndDie, checkPrintInvoked},
{[]string{"version"}, checkPrintInvoked, usage, PrintTLSHelpAndDie, checkPrintInvoked},
{[]string{"-h"}, PrintServerAndExit, checkPrintInvoked, PrintTLSHelpAndDie, checkPrintInvoked},
{[]string{"help"}, PrintServerAndExit, checkPrintInvoked, PrintTLSHelpAndDie, checkPrintInvoked},
{[]string{"-help_tls"}, PrintServerAndExit, usage, checkPrintInvoked, checkPrintInvoked},
{[]string{"buildinfo"}, PrintServerAndExit, usage, PrintTLSHelpAndDie, checkPrintInvoked},
{[]string{"-buildinfo"}, PrintServerAndExit, usage, PrintTLSHelpAndDie, checkPrintInvoked},
}
for _, tf := range testFuncs {
fs = flag.NewFlagSet("test", flag.ContinueOnError)
opts, err := ConfigureOptions(fs, tf.args, tf.version, tf.help, tf.tlsHelp)
opts, err := ConfigureOptions(fs, tf.args, tf.version, tf.help, tf.buildinfo, tf.tlsHelp)
if err != nil {
t.Fatalf("Error on configure: %v", err)
}
Expand All @@ -1463,7 +1465,7 @@ func TestConfigureOptions(t *testing.T) {
// Helper function that expect parsing with given args to not produce an error.
mustNotFail := func(args []string) *Options {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
opts, err := ConfigureOptions(fs, args, PrintServerAndExit, fs.Usage, PrintTLSHelpAndDie)
opts, err := ConfigureOptions(fs, args, PrintServerAndExit, fs.Usage, PrintBuildinfoAndExit, PrintTLSHelpAndDie)
if err != nil {
stackFatalf(t, "Error on configure: %v", err)
}
Expand All @@ -1477,7 +1479,7 @@ func TestConfigureOptions(t *testing.T) {
// (flagSet would print error message about unknown flags, etc..)
silenceOuput := &bytes.Buffer{}
fs.SetOutput(silenceOuput)
opts, err := ConfigureOptions(fs, args, PrintServerAndExit, fs.Usage, PrintTLSHelpAndDie)
opts, err := ConfigureOptions(fs, args, PrintServerAndExit, fs.Usage, PrintBuildinfoAndExit, PrintTLSHelpAndDie)
if opts != nil || err == nil {
stackFatalf(t, "Expected no option and an error, got opts=%v and err=%v", opts, err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3818,7 +3818,7 @@ func TestConfigReloadBoolFlags(t *testing.T) {
if test.cmdLine != nil {
args = append(args, test.cmdLine...)
}
opts, err = ConfigureOptions(fs, args, nil, nil, nil)
opts, err = ConfigureOptions(fs, args, nil, nil, nil, nil)
if err != nil {
t.Fatalf("Error processing config: %v", err)
}
Expand Down
23 changes: 18 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"runtime/pprof"
"strconv"
"strings"
Expand Down Expand Up @@ -1583,23 +1584,35 @@ func PrintServerAndExit() {
os.Exit(0)
}

// PrintBuildinfoAndExit will print out https://pkg.go.dev/runtime/debug#BuildInfo and exit.
func PrintBuildinfoAndExit() {
bi, ok := debug.ReadBuildInfo()
if !ok || bi == nil {
fmt.Println("failed to read Buildinfo")
}
fmt.Println(bi)
os.Exit(0)
}

// ProcessCommandLineArgs takes the command line arguments
// validating and setting flags for handling in case any
// sub command was present.
func ProcessCommandLineArgs(cmd *flag.FlagSet) (showVersion bool, showHelp bool, err error) {
func ProcessCommandLineArgs(cmd *flag.FlagSet) (showVersion bool, showHelp bool, showBuildinfo bool, err error) {
if len(cmd.Args()) > 0 {
arg := cmd.Args()[0]
switch strings.ToLower(arg) {
case "version":
return true, false, nil
return true, false, false, nil
case "help":
return false, true, nil
return false, true, false, nil
case "buildinfo":
return false, false, true, nil
default:
return false, false, fmt.Errorf("unrecognized command: %q", arg)
return false, false, false, fmt.Errorf("unrecognized command: %q", arg)
}
}

return false, false, nil
return false, false, false, nil
}

// Public version.
Expand Down
31 changes: 26 additions & 5 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,16 @@ func TestProcessCommandLineArgs(t *testing.T) {
cmd.IntVar(&port, "p", 4222, "Port.")

cmd.Parse([]string{"-a", "127.0.0.1", "-p", "9090"})
showVersion, showHelp, err := ProcessCommandLineArgs(cmd)
showVersion, showHelp, showBuildinfo, err := ProcessCommandLineArgs(cmd)
if err != nil {
t.Errorf("Expected no errors, got: %s", err)
}
if showVersion || showHelp {
if showVersion || showHelp || showBuildinfo {
t.Errorf("Expected not having to handle subcommands")
}

cmd.Parse([]string{"version"})
showVersion, showHelp, err = ProcessCommandLineArgs(cmd)
showVersion, showHelp, showBuildinfo, err = ProcessCommandLineArgs(cmd)
if err != nil {
t.Errorf("Expected no errors, got: %s", err)
}
Expand All @@ -678,9 +678,12 @@ func TestProcessCommandLineArgs(t *testing.T) {
if showHelp {
t.Errorf("Expected not having to handle help command")
}
if showBuildinfo {
t.Errorf("Expected not having to handle buildinfo command")
}

cmd.Parse([]string{"help"})
showVersion, showHelp, err = ProcessCommandLineArgs(cmd)
showVersion, showHelp, showBuildinfo, err = ProcessCommandLineArgs(cmd)
if err != nil {
t.Errorf("Expected no errors, got: %s", err)
}
Expand All @@ -690,9 +693,27 @@ func TestProcessCommandLineArgs(t *testing.T) {
if !showHelp {
t.Errorf("Expected having to handle help command")
}
if showBuildinfo {
t.Errorf("Expected not having to handle buildinfo command")
}

cmd.Parse([]string{"buildinfo"})
showVersion, showHelp, showBuildinfo, err = ProcessCommandLineArgs(cmd)
if err != nil {
t.Errorf("Expected no errors, got: %s", err)
}
if showVersion {
t.Errorf("Expected not having to handle version command")
}
if showHelp {
t.Errorf("Expected not having to handle help command")
}
if !showBuildinfo {
t.Errorf("Expected having to handle buildinfo command")
}

cmd.Parse([]string{"foo", "-p", "9090"})
_, _, err = ProcessCommandLineArgs(cmd)
_, _, _, err = ProcessCommandLineArgs(cmd)
if err == nil {
t.Errorf("Expected an error handling the command arguments")
}
Expand Down

0 comments on commit b5fe72d

Please sign in to comment.