main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "github.com/alecthomas/kong"
  6. "github.com/ayntgl/discordo/config"
  7. "github.com/ayntgl/discordo/ui"
  8. "github.com/zalando/go-keyring"
  9. )
  10. var cli struct {
  11. Token string `name:"token" short:"t" help:"The authentication token."`
  12. ConfigPath string `name:"config" short:"c" help:"The path to the configuration file" type:"path" default:"${configPath}"`
  13. LogPath string `name:"log" short:"l" help:"The path to the log file" type:"path" default:"${logPath}"`
  14. }
  15. func main() {
  16. kong.Parse(&cli, kong.Vars{
  17. "configPath": config.DefaultConfigPath(),
  18. "logPath": config.DefaultLogPath(),
  19. })
  20. if cli.LogPath != "" {
  21. // Set the standard logger output to the provided log file.
  22. f, err := os.OpenFile(cli.LogPath, os.O_CREATE|os.O_WRONLY, 0666)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. log.SetOutput(f)
  27. log.SetFlags(log.LstdFlags | log.Lshortfile)
  28. }
  29. cfg := config.New()
  30. if err := cfg.Load(cli.ConfigPath); err != nil {
  31. log.Fatal(err)
  32. }
  33. if cli.Token != "" {
  34. go keyring.Set(config.Name, "token", cli.Token)
  35. } else {
  36. var err error
  37. cli.Token, err = keyring.Get(config.Name, "token")
  38. if err != nil {
  39. log.Println(err)
  40. }
  41. }
  42. app := ui.NewApplication(cfg)
  43. app.Run(cli.Token)
  44. }