Command line client for Write.as https://write.as/apps/cli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.2 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/writeas/writeas-cli/api"
  7. "github.com/writeas/writeas-cli/commands"
  8. "github.com/writeas/writeas-cli/config"
  9. "github.com/writeas/writeas-cli/executable"
  10. "github.com/writeas/writeas-cli/log"
  11. cli "gopkg.in/urfave/cli.v1"
  12. )
  13. func requireAuth(f cli.ActionFunc, action string) cli.ActionFunc {
  14. return func(c *cli.Context) error {
  15. u, err := config.LoadUser(c)
  16. if err != nil {
  17. return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
  18. }
  19. if u == nil {
  20. return cli.NewExitError("You must be authenticated to "+action+".\nLog in first with: "+executable.Name()+" auth <username>", 1)
  21. }
  22. return f(c)
  23. }
  24. }
  25. // usersLoggedIn checks for logged in users for the set host flag
  26. // it returns the number of users and a slice of usernames
  27. func usersLoggedIn(c *cli.Context) (int, []string, error) {
  28. path, err := config.UserHostDir(c)
  29. if err != nil {
  30. return 0, nil, err
  31. }
  32. dir, err := os.Open(path)
  33. if err != nil {
  34. return 0, nil, err
  35. }
  36. contents, err := dir.Readdir(0)
  37. if err != nil {
  38. return 0, nil, err
  39. }
  40. var names []string
  41. for _, file := range contents {
  42. if file.IsDir() {
  43. // stat user.json
  44. if _, err := os.Stat(filepath.Join(path, file.Name(), "user.json")); err == nil {
  45. names = append(names, file.Name())
  46. }
  47. }
  48. }
  49. return len(names), names, nil
  50. }
  51. func cmdAuth(c *cli.Context) error {
  52. err := commands.CmdAuth(c)
  53. if err != nil {
  54. return err
  55. }
  56. // Get the username from the command, just like commands.CmdAuth does
  57. username := c.Args().Get(0)
  58. // Update config if this is user's first auth
  59. cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
  60. if err != nil {
  61. log.Errorln("Not saving config. Unable to load config: %s", err)
  62. return err
  63. }
  64. if cfg.Default.Host == "" && cfg.Default.User == "" {
  65. // This is user's first auth, so save defaults
  66. cfg.Default.Host = api.HostURL(c)
  67. cfg.Default.User = username
  68. err = config.SaveConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]), cfg)
  69. if err != nil {
  70. log.Errorln("Not saving config. Unable to save config: %s", err)
  71. return err
  72. }
  73. fmt.Printf("Set %s on %s as default account.\n", username, c.GlobalString("host"))
  74. }
  75. return nil
  76. }
  77. func cmdLogOut(c *cli.Context) error {
  78. err := commands.CmdLogOut(c)
  79. if err != nil {
  80. return err
  81. }
  82. // Remove this from config if it's the default account
  83. cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
  84. if err != nil {
  85. log.Errorln("Not updating config. Unable to load: %s", err)
  86. return err
  87. }
  88. username, err := config.CurrentUser(c)
  89. if err != nil {
  90. log.Errorln("Not updating config. Unable to load current user: %s", err)
  91. return err
  92. }
  93. reqHost := api.HostURL(c)
  94. if reqHost == "" {
  95. // No --host given, so we're using the default host
  96. reqHost = cfg.Default.Host
  97. }
  98. if cfg.Default.Host == reqHost && cfg.Default.User == username {
  99. // We're logging out of default username + host, so remove from config file
  100. cfg.Default.Host = ""
  101. cfg.Default.User = ""
  102. err = config.SaveConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]), cfg)
  103. if err != nil {
  104. log.Errorln("Not updating config. Unable to save config: %s", err)
  105. return err
  106. }
  107. }
  108. return nil
  109. }