github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/buffalo/cmd/version.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  
     7  	"github.com/gobuffalo/buffalo/runtime"
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var jsonOutput bool
    13  
    14  func init() {
    15  	decorate("version", versionCmd)
    16  	versionCmd.Flags().BoolVar(&jsonOutput, "json", false, "Print information in json format")
    17  
    18  	RootCmd.AddCommand(versionCmd)
    19  }
    20  
    21  var versionCmd = &cobra.Command{
    22  	Use:   "version",
    23  	Short: "Print the version information",
    24  	Run: func(c *cobra.Command, args []string) {
    25  		if jsonOutput {
    26  			build := runtime.BuildInfo{}
    27  			build.Version = runtime.Version
    28  			enc := json.NewEncoder(os.Stderr)
    29  			enc.SetIndent("", "    ")
    30  			enc.Encode(build)
    31  			return
    32  		}
    33  
    34  		logrus.Infof("Buffalo version is: %s", runtime.Version)
    35  	},
    36  	// needed to override the root level pre-run func
    37  	PersistentPreRunE: func(c *cobra.Command, args []string) error {
    38  		return nil
    39  	},
    40  }