Modernize command
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
1eb949520c
commit
7f5cfe0d86
3 changed files with 28 additions and 20 deletions
|
@ -5,6 +5,7 @@ go 1.21
|
|||
require (
|
||||
github.com/Luzifer/rconfig/v2 v2.4.0
|
||||
github.com/Luzifer/twitch-bot/v3 v3.17.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
)
|
||||
|
||||
|
@ -28,7 +29,6 @@ require (
|
|||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
|
|
|
@ -22,7 +22,6 @@ github.com/go-irc/irc v2.1.0+incompatible/go.mod h1:jJILTRy8s/qOvusiKifAEfhQMVwf
|
|||
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
|
||||
github.com/gofrs/uuid/v3 v3.1.2 h1:V3IBv1oU82x6YIr5txe3azVHgmOKYdyKQTowm9moBlY=
|
||||
github.com/gofrs/uuid/v3 v3.1.2/go.mod h1:xPwMqoocQ1L5G6pXX5BcE7N5jlzn2o19oqAKxwZW/kI=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
|
|
45
dice/main.go
45
dice/main.go
|
@ -9,7 +9,8 @@ import (
|
|||
"os"
|
||||
"text/template"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Luzifer/rconfig/v2"
|
||||
"github.com/Luzifer/twitch-bot/v3/plugins"
|
||||
|
@ -29,51 +30,59 @@ var (
|
|||
version = "dev"
|
||||
)
|
||||
|
||||
func init() {
|
||||
func initApp() error {
|
||||
rconfig.AutoEnv(true)
|
||||
rconfig.SetVariableDefaults(map[string]string{
|
||||
"tpl": `I threw {{.count}}x {{if eq .type "coin"}}coin{{else}}W{{.sides}}{{end}} for you and got:{{ range .results}} {{ if eq $.type "coin" }}{{ if eq . 1 }}Head{{ else }}Number{{ end }}{{ else }}{{ . }}{{ end }}{{ end }}`,
|
||||
})
|
||||
|
||||
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
||||
log.Fatalf("Unable to parse commandline options: %s", err)
|
||||
return errors.Wrap(err, "parsing cli options")
|
||||
}
|
||||
|
||||
if cfg.VersionAndExit {
|
||||
fmt.Printf("dice %s\n", version)
|
||||
os.Exit(0)
|
||||
l, err := logrus.ParseLevel(cfg.LogLevel)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "parsing log-level")
|
||||
}
|
||||
logrus.SetLevel(l)
|
||||
|
||||
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
||||
log.WithError(err).Fatal("Unable to parse log level")
|
||||
} else {
|
||||
log.SetLevel(l)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
err error
|
||||
results []int64
|
||||
sum int64
|
||||
)
|
||||
|
||||
if err = initApp(); err != nil {
|
||||
logrus.WithError(err).Fatal("initializing app")
|
||||
}
|
||||
|
||||
if cfg.VersionAndExit {
|
||||
logrus.WithField("version", version).Info("twitch-bot-tools/dice")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
switch cfg.Type {
|
||||
case "coin":
|
||||
results, sum = getResults(2, cfg.Count)
|
||||
results, sum = getResults(2, cfg.Count) //nolint:gomnd // Makes no sense to extract
|
||||
|
||||
case "dice":
|
||||
if cfg.Sides == 0 {
|
||||
// There is no [0..0] dice.
|
||||
log.Fatal("There is no 0-sided dice")
|
||||
logrus.Fatal("there is no 0-sided dice")
|
||||
}
|
||||
results, sum = getResults(cfg.Sides, cfg.Count)
|
||||
|
||||
default:
|
||||
log.WithField("type", cfg.Type).Fatal("Don't know how to throw that")
|
||||
logrus.WithField("type", cfg.Type).Fatal("don't know how to throw that")
|
||||
}
|
||||
|
||||
t, err := template.New("output").Parse(cfg.Template)
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Unable to parse template")
|
||||
logrus.WithError(err).Fatal("parsing template")
|
||||
}
|
||||
|
||||
text := new(bytes.Buffer)
|
||||
|
@ -84,11 +93,11 @@ func main() {
|
|||
"sum": sum,
|
||||
"type": cfg.Type,
|
||||
}); err != nil {
|
||||
log.WithError(err).Fatal("Unable to execute template")
|
||||
logrus.WithError(err).Fatal("executing template")
|
||||
}
|
||||
|
||||
if !cfg.BotRespond {
|
||||
fmt.Println(text.String())
|
||||
fmt.Println(text.String()) //nolint:forbidigo // This is an expected stdout output
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -102,7 +111,7 @@ func main() {
|
|||
}
|
||||
|
||||
if err = json.NewEncoder(os.Stdout).Encode(output); err != nil {
|
||||
log.WithError(err).Fatal("Unable to encode bot response")
|
||||
logrus.WithError(err).Fatal("encoding bot response")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue