Add --version flag to print version and exit.

This commit is contained in:
Leo R. Lundgren 2018-03-21 01:49:19 +01:00 committed by Zlatko Čalušić
parent a6961e877b
commit cbafb98113
3 changed files with 11 additions and 1 deletions

View file

@ -73,6 +73,7 @@ Flags:
--tls turn on TLS support --tls turn on TLS support
--tls-cert string TLS certificate path --tls-cert string TLS certificate path
--tls-key string TLS key path --tls-key string TLS key path
-V, --version show version and quit
``` ```

View file

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -20,6 +21,8 @@ var cmdRoot = &cobra.Command{
SilenceErrors: true, SilenceErrors: true,
SilenceUsage: true, SilenceUsage: true,
RunE: runRoot, RunE: runRoot,
// Use this instead of other --version code when the Cobra dependency can be updated.
//Version: fmt.Sprintf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH),
} }
func init() { func init() {
@ -35,6 +38,7 @@ func init() {
flags.BoolVar(&restserver.Config.AppendOnly, "append-only", restserver.Config.AppendOnly, "enable append only mode") flags.BoolVar(&restserver.Config.AppendOnly, "append-only", restserver.Config.AppendOnly, "enable append only mode")
flags.BoolVar(&restserver.Config.PrivateRepos, "private-repos", restserver.Config.PrivateRepos, "users can only access their private repo") flags.BoolVar(&restserver.Config.PrivateRepos, "private-repos", restserver.Config.PrivateRepos, "users can only access their private repo")
flags.BoolVar(&restserver.Config.Prometheus, "prometheus", restserver.Config.Prometheus, "enable Prometheus metrics") flags.BoolVar(&restserver.Config.Prometheus, "prometheus", restserver.Config.Prometheus, "enable Prometheus metrics")
flags.BoolVarP(&restserver.Config.Version, "version", "V", restserver.Config.Version, "output version and exit")
} }
var version = "manually" var version = "manually"
@ -61,9 +65,13 @@ func tlsSettings() (bool, string, string, error) {
} }
func runRoot(cmd *cobra.Command, args []string) error { func runRoot(cmd *cobra.Command, args []string) error {
if restserver.Config.Version {
fmt.Printf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
log.SetFlags(0) log.SetFlags(0)
log.Printf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
log.Printf("Data directory: %s", restserver.Config.Path) log.Printf("Data directory: %s", restserver.Config.Path)
if restserver.Config.CPUProfile != "" { if restserver.Config.CPUProfile != "" {

1
mux.go
View file

@ -25,6 +25,7 @@ var Config = struct {
PrivateRepos bool PrivateRepos bool
Prometheus bool Prometheus bool
Debug bool Debug bool
Version bool
}{ }{
Path: "/tmp/restic", Path: "/tmp/restic",
Listen: ":8000", Listen: ":8000",