Skip to content

Commit

Permalink
feat: add version info
Browse files Browse the repository at this point in the history
  • Loading branch information
elonzh committed Nov 6, 2020
1 parent ff06066 commit afb8bf2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 69 deletions.
19 changes: 17 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@ before:
hooks:
- go mod tidy
builds:
- env:
- main: ./main.go
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- 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:
Expand Down
50 changes: 0 additions & 50 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
64 changes: 53 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 0 additions & 4 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,3 @@ var serveCmd = &cobra.Command{
return r.Run()
},
}

func init() {
rootCmd.AddCommand(serveCmd)
}
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit afb8bf2

Please sign in to comment.