main.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. os.Exit(2)
  61. }
  62. var ok bool
  63. var account string
  64. if Session.Info.Opts.ConfigStanza != "" {
  65. account = Session.Info.Opts.ConfigStanza
  66. Session.Config, ok = fullConfig.Accounts[account]
  67. } else {
  68. account = fullConfig.Default
  69. Session.Config, ok = fullConfig.Accounts[account]
  70. }
  71. if !ok {
  72. Log.Error("Account not found in config:", account)
  73. os.Exit(2)
  74. }
  75. err = Session.Info.Commands[args[0]].Func(args)
  76. if err != nil {
  77. Log.Error(err)
  78. os.Exit(-1)
  79. }
  80. }