Add version flags and command, closes #38

This commit is contained in:
Ken-Håvard Lieng 2018-11-26 01:49:16 +01:00
parent fbbcf6457e
commit bf2e7875a5
2 changed files with 28 additions and 0 deletions

View File

@ -36,6 +36,11 @@ var rootCmd = &cobra.Command{
Use: "dispatch", Use: "dispatch",
Short: "Web-based IRC client in Go.", Short: "Web-based IRC client in Go.",
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
if v, _ := cmd.Flags().GetBool("version"); v {
printVersion()
os.Exit(0)
}
if cmd.Use == "dispatch" { if cmd.Use == "dispatch" {
fmt.Printf(logo, version.Tag, version.Commit, version.Date) fmt.Printf(logo, version.Tag, version.Commit, version.Date)
} }
@ -96,12 +101,14 @@ func Execute() {
func init() { func init() {
rootCmd.AddCommand(clearCmd) rootCmd.AddCommand(clearCmd)
rootCmd.AddCommand(configCmd) rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.PersistentFlags().String("dir", storage.DefaultDirectory(), "directory to store config and data in") rootCmd.PersistentFlags().String("dir", storage.DefaultDirectory(), "directory to store config and data in")
rootCmd.PersistentFlags().Bool("reset-config", false, "reset to the default configuration, overwriting the current one") rootCmd.PersistentFlags().Bool("reset-config", false, "reset to the default configuration, overwriting the current one")
rootCmd.Flags().StringP("address", "a", "", "interface to which the server will bind") rootCmd.Flags().StringP("address", "a", "", "interface to which the server will bind")
rootCmd.Flags().IntP("port", "p", 80, "port to listen on") rootCmd.Flags().IntP("port", "p", 80, "port to listen on")
rootCmd.Flags().Bool("dev", false, "development mode") rootCmd.Flags().Bool("dev", false, "development mode")
rootCmd.Flags().BoolP("version", "v", false, "show version")
viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir")) viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir"))
viper.BindPFlag("reset_config", rootCmd.PersistentFlags().Lookup("reset-config")) viper.BindPFlag("reset_config", rootCmd.PersistentFlags().Lookup("reset-config"))

21
commands/version.go Normal file
View File

@ -0,0 +1,21 @@
package commands
import (
"fmt"
"github.com/khlieng/dispatch/version"
"github.com/spf13/cobra"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show version",
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
Run: func(cmd *cobra.Command, args []string) {
printVersion()
},
}
func printVersion() {
fmt.Printf("%s\nCommit: %s\nBuild Date: %s\n", version.Tag, version.Commit, version.Date)
}