version subcommand: unified client & server

This commit is contained in:
Christian Schwarz 2018-09-06 23:52:11 -07:00
parent 7836ea36fc
commit bf5099baac
2 changed files with 72 additions and 0 deletions

57
client/version.go Normal file
View File

@ -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")
}
}
}

15
main.go
View File

@ -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() {