@@ -9,33 +9,23 @@ import (
99
1010 "github.com/spf13/cobra"
1111 "github.com/spf13/viper"
12+ pkg "github.com/zph/polylint/pkg"
1213)
1314
1415var (
15- version = "unknown "
16- commit = "none "
17- date = "unknown "
16+ version = "v0.0.1 "
17+ commit = ""
18+ date = ""
1819)
1920
2021var cfgFile string
2122
2223// rootCmd represents the base command when called without any subcommands
2324var rootCmd = & cobra.Command {
2425 Use : "polylint" ,
25- Short : "A brief description of your application" ,
26- Long : `A longer description that spans multiple lines and likely contains
27- examples and usage of using your application. For example:
28-
29- Cobra is a CLI library for Go that empowers applications.
30- This application is a tool to generate the needed files
31- to quickly create a Cobra application.` ,
32- // Uncomment the following line if your bare application
33- // has an action associated with it:
34- // Run: func(cmd *cobra.Command, args []string) { },
26+ Short : "Polylint: Extensible generalized linter" ,
3527}
3628
37- // Execute adds all child commands to the root command and sets flags appropriately.
38- // This is called by main.main(). It only needs to happen once to the rootCmd.
3929func Execute () {
4030 err := rootCmd .Execute ()
4131 if err != nil {
@@ -44,22 +34,20 @@ func Execute() {
4434}
4535
4636func init () {
37+ rootCmd .Version = version
4738 cobra .OnInitialize (initConfig )
4839 cobra .OnInitialize (setVersion )
49-
50- // Here you will define your flags and configuration settings.
51- // Cobra supports persistent flags, which, if defined here,
52- // will be global for your application.
40+ rootCmd .SetVersionTemplate (fmt .Sprintf ("polylint\n Version: %s\n Commit: %s\n Date: %s\n " , version , commit , date ))
5341
5442 rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "config file (default is $HOME/.polylint.yaml)" )
55-
56- // Cobra also supports local flags, which will only run
57- // when this action is called directly.
58- rootCmd .Flags ().BoolP ("toggle" , "t" , false , "Help message for toggle" )
5943}
6044
6145func setVersion () {
46+ if version == "v0.0.1" {
47+ version = pkg .LibMetadata .Version
48+ }
6249 viper .Set ("binary_version" , version )
50+ rootCmd .Version = version
6351}
6452
6553// initConfig reads in config file and ENV variables if set.
@@ -80,8 +68,7 @@ func initConfig() {
8068
8169 viper .AutomaticEnv () // read in environment variables that match
8270
83- // If a config file is found, read it in.
84- if err := viper .ReadInConfig (); err == nil {
85- fmt .Fprintln (os .Stderr , "Using config file:" , viper .ConfigFileUsed ())
71+ if err := viper .ReadInConfig (); err != nil {
72+ fmt .Fprintln (os .Stderr , "Error reading config file:" , viper .ConfigFileUsed ())
8673 }
8774}
0 commit comments