main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "git.sr.ht/~blallo/papero/cli"
  6. "git.sr.ht/~blallo/papero/config"
  7. "github.com/withmandala/go-log"
  8. )
  9. var Session struct {
  10. Info cli.ProgramInfo
  11. Config *config.AccountData
  12. }
  13. var Log *log.Logger
  14. func init() {
  15. get := GetCmd{}
  16. set := SetCmd{}
  17. add := AddCmd{}
  18. // del := DelCmd{}
  19. Session.Info.Commands = cli.CommandMap{
  20. "get": get,
  21. "set": set,
  22. "add": add,
  23. // "del": del,
  24. }
  25. Session.Info.Name = "papero"
  26. flag.Usage = func() { cli.Usage(os.Stdout, Session.Info) }
  27. Log = log.New(os.Stdout)
  28. }
  29. func main() {
  30. var configPath string
  31. flag.StringVar(&Session.Info.Opts.ConfigPath, "config", "", "path to the configuration")
  32. flag.BoolVar(&Session.Info.Opts.Debug, "debug", false, "execute in debug mode")
  33. flag.StringVar(&Session.Info.Opts.ConfigStanza, "account", "", "specify which account to use")
  34. flag.Parse()
  35. args := flag.Args()
  36. if len(args) == 0 {
  37. cli.Usage(os.Stderr, Session.Info)
  38. os.Exit(1)
  39. }
  40. if Session.Info.Opts.Debug {
  41. Log.WithDebug()
  42. Log.Debugf(
  43. "opts -> config: %s\tdebug: %v\taccount: %s\n",
  44. Session.Info.Opts.ConfigPath,
  45. Session.Info.Opts.Debug,
  46. Session.Info.Opts.ConfigStanza,
  47. )
  48. }
  49. if Session.Info.Opts.ConfigPath == "" {
  50. var err error
  51. configPath, err = config.Find()
  52. if err != nil {
  53. Log.Fatal(err)
  54. }
  55. } else {
  56. configPath = Session.Info.Opts.ConfigPath
  57. }
  58. fullConfig, err := config.Parse(configPath)
  59. if err != nil {
  60. Log.Error(err)
  61. os.Exit(2)
  62. }
  63. var ok bool
  64. var account string
  65. if Session.Info.Opts.ConfigStanza != "" {
  66. account = Session.Info.Opts.ConfigStanza
  67. Session.Config, ok = fullConfig.Accounts[account]
  68. } else {
  69. account = fullConfig.Default
  70. Session.Config, ok = fullConfig.Accounts[account]
  71. }
  72. if !ok {
  73. Log.Error("Account not found in config:", account)
  74. os.Exit(2)
  75. }
  76. err = Session.Info.Commands[args[0]].Func(args)
  77. if err != nil {
  78. Log.Error(err)
  79. os.Exit(-1)
  80. }
  81. }