From bf5099baacb807b7661935ac300fa8550a505bda Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Thu, 6 Sep 2018 23:52:11 -0700 Subject: [PATCH] version subcommand: unified client & server --- client/version.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 15 +++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 client/version.go diff --git a/client/version.go b/client/version.go new file mode 100644 index 0000000..e584a80 --- /dev/null +++ b/client/version.go @@ -0,0 +1,57 @@ +package client + +import ( + "fmt" + "github.com/zrepl/zrepl/config" + "github.com/zrepl/zrepl/daemon" + "github.com/zrepl/zrepl/version" + "os" +) + +type VersionArgs struct { + Show string + Config *config.Config +} + +func RunVersion(args VersionArgs) { + + die := func() { + fmt.Fprintf(os.Stderr, "exiting after error\n") + os.Exit(1) + } + + if args.Show != "daemon" && args.Show != "client" && args.Show != "" { + fmt.Fprintf(os.Stderr, "show flag must be 'client' or 'server' or be left empty") + die() + } + + var clientVersion, daemonVersion *version.ZreplVersionInformation + if args.Show == "client" || args.Show == "" { + clientVersion = version.NewZreplVersionInformation() + fmt.Printf("client: %s\n", clientVersion.String()) + } + if args.Show == "daemon" || args.Show == "" { + + httpc, err := controlHttpClient(args.Config.Global.Control.SockPath) + if err != nil { + fmt.Fprintf(os.Stderr, "server: error: %s\n", err) + die() + } + + var info version.ZreplVersionInformation + err = jsonRequestResponse(httpc, daemon.ControlJobEndpointVersion, "", &info) + if err != nil { + fmt.Fprintf(os.Stderr, "server: error: %s\n", err) + die() + } + daemonVersion = &info + fmt.Printf("server: %s\n", daemonVersion.String()) + } + + if args.Show == "" { + if clientVersion.Version != daemonVersion.Version { + fmt.Fprintf(os.Stderr, "WARNING: client version != daemon version, restart zrepl daemon\n") + } + } + +} diff --git a/main.go b/main.go index 905e2f1..a41b080 100644 --- a/main.go +++ b/main.go @@ -100,6 +100,19 @@ var configcheckCmd = &cobra.Command{ }, } +var versionCmdArgs client.VersionArgs +var versionCmd = &cobra.Command{ + Use: "version", + Short: "print version of zrepl binary (for running daemon 'zrepl control version' command)", + Run: func(cmd *cobra.Command, args []string) { + conf, err := config.ParseConfig(rootArgs.configFile) + if err == nil { + versionCmdArgs.Config = conf + } + client.RunVersion(versionCmdArgs) + }, +} + var rootArgs struct { configFile string } @@ -113,6 +126,8 @@ func init() { rootCmd.AddCommand(stdinserverCmd) rootCmd.AddCommand(bashcompCmd) rootCmd.AddCommand(configcheckCmd) + versionCmd.Flags().StringVar(&versionCmdArgs.Show, "show", "", "version info to show (client|daemon)") + rootCmd.AddCommand(versionCmd) } func main() {