diff --git a/.goreleaser.yml b/.goreleaser.yml index ca8b978..6191411 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -4,7 +4,8 @@ before: hooks: - go mod tidy builds: - - env: + - main: ./main.go + env: - CGO_ENABLED=0 goos: - linux @@ -12,7 +13,21 @@ builds: - windows goarch: - amd64 - main: ./main.go + mod_timestamp: "{{ .CommitTimestamp }}" + flags: + - -trimpath + ldflags: + - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} + +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" + - Merge pull request + - Merge branch + - go mod tidy archives: - format_overrides: diff --git a/cmd/config.go b/cmd/config.go index 5fff603..04fd1c5 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -22,12 +22,8 @@ THE SOFTWARE. package cmd import ( - "os" - "github.com/elonzh/trumpet/transformers" "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/spf13/viper" ) var BuiltinTransformers = []*transformers.Transformer{ @@ -111,49 +107,3 @@ func (c *Config) GetTransformer(name string) (*transformers.Transformer, bool) { t, ok := c.m[name] return t, ok } - -func initConfig() { - if cfgFile != "" { - viper.SetConfigFile(cfgFile) - } else { - viper.AddConfigPath(".") - viper.SetConfigName("config") - } - var err error - if err = viper.ReadInConfig(); os.IsNotExist(err) { - logrus.WithError(err).Fatalln() - } - logrus.WithField("ConfigFile", viper.ConfigFileUsed()).Infoln("read in config") - err = viper.Unmarshal(cfg) - if err != nil { - logrus.WithError(err).Fatalln("error when unmarshal config") - } - level, err := logrus.ParseLevel(cfg.LogLevel) - if err != nil { - logrus.WithError(err).Fatalln() - } - logrus.SetLevel(level) - if level >= logrus.DebugLevel { - logrus.WithField("Config", cfg).Debug() - } - - cfg.LoadAllTransformers() -} - -var ( - cfg = &Config{ - LogLevel: logrus.InfoLevel.String(), - } -) - -// configCmd represents the config command -var configCmd = &cobra.Command{ - Use: "config", - Short: "", - Long: ``, -} - -func init() { - rootCmd.AddCommand(configCmd) - cobra.OnInitialize(initConfig) -} diff --git a/cmd/root.go b/cmd/root.go index 4a311fb..4f4697a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,30 +22,72 @@ THE SOFTWARE. package cmd import ( + "os" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" ) -var cfgFile string +var ( + cfg = &Config{ + LogLevel: logrus.InfoLevel.String(), + } +) + +func newRootCmd(version string) *cobra.Command { + var cfgFile string + var rootCmd = &cobra.Command{ + Version: version, + Use: "trumpet", + Short: "🎺simple webhook transform server", + Long: ``, + } + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./config.yaml)") + rootCmd.PersistentFlags().String("logLevel", "info", "") + err := viper.BindPFlag("logLevel", rootCmd.PersistentFlags().Lookup("logLevel")) + if err != nil { + panic(err) + } -var rootCmd = &cobra.Command{ - Use: "trumpet", - Short: "🎺simple webhook transform server", - Long: ``, + cobra.OnInitialize(func() { + initConfig(cfgFile) + }) + rootCmd.AddCommand(serveCmd) + return rootCmd } -func Execute() { +func Execute(version string) { + rootCmd := newRootCmd(version) if err := rootCmd.Execute(); err != nil { logrus.WithError(err).Fatalln() } } -func init() { - rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./config.yaml)") - rootCmd.PersistentFlags().String("logLevel", "info", "") - err := viper.BindPFlag("logLevel", rootCmd.PersistentFlags().Lookup("logLevel")) +func initConfig(cfgFile string) { + if cfgFile != "" { + viper.SetConfigFile(cfgFile) + } else { + viper.AddConfigPath(".") + viper.SetConfigName("config") + } + var err error + if err = viper.ReadInConfig(); os.IsNotExist(err) { + logrus.WithError(err).Fatalln() + } + logrus.WithField("ConfigFile", viper.ConfigFileUsed()).Infoln("read in config") + err = viper.Unmarshal(cfg) if err != nil { - panic(err) + logrus.WithError(err).Fatalln("error when unmarshal config") } + level, err := logrus.ParseLevel(cfg.LogLevel) + if err != nil { + logrus.WithError(err).Fatalln() + } + logrus.SetLevel(level) + if level >= logrus.DebugLevel { + logrus.WithField("Config", cfg).Debug() + } + + cfg.LoadAllTransformers() } diff --git a/cmd/serve.go b/cmd/serve.go index bf87186..44519ec 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -76,7 +76,3 @@ var serveCmd = &cobra.Command{ return r.Run() }, } - -func init() { - rootCmd.AddCommand(serveCmd) -} diff --git a/main.go b/main.go index 5a4b4d5..a6c2300 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,30 @@ THE SOFTWARE. */ package main -import "github.com/elonzh/trumpet/cmd" +import ( + "fmt" + + "github.com/elonzh/trumpet/cmd" +) + +// nolint: gochecknoglobals +var ( + version = "dev" + commit = "" + date = "" +) func main() { - cmd.Execute() + cmd.Execute(buildVersion(version, commit, date)) +} + +func buildVersion(version, commit, date string) string { + var result = version + if commit != "" { + result = fmt.Sprintf("%s\ncommit: %s", result, commit) + } + if date != "" { + result = fmt.Sprintf("%s\nbuilt at: %s", result, date) + } + return result }