Command line client for Write.as https://write.as/apps/cli
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

157 righe
4.9 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/writeas/writeas-cli/api"
  8. "github.com/writeas/writeas-cli/commands"
  9. "github.com/writeas/writeas-cli/config"
  10. "github.com/writeas/writeas-cli/executable"
  11. "github.com/writeas/writeas-cli/log"
  12. cli "gopkg.in/urfave/cli.v1"
  13. )
  14. func requireAuth(f cli.ActionFunc, action string) cli.ActionFunc {
  15. return func(c *cli.Context) error {
  16. // check for logged in users when host is provided without user
  17. if c.GlobalIsSet("host") && !c.GlobalIsSet("user") {
  18. // multiple users should display a list
  19. if num, users, err := usersLoggedIn(c); num > 1 && err == nil {
  20. return cli.NewExitError(fmt.Sprintf("Multiple logged in users, please use '-u' or '-user' to specify one of:\n%s", strings.Join(users, ", ")), 1)
  21. } else if num == 1 && err == nil {
  22. // single user found for host should be set as user flag so LoadUser can
  23. // succeed, and notify the client
  24. if err := c.GlobalSet("user", users[0]); err != nil {
  25. return cli.NewExitError(fmt.Sprintf("Failed to set user flag for only logged in user at host %s: %v", users[0], err), 1)
  26. }
  27. log.Info(c, "Host specified without user flag, using logged in user: %s\n", users[0])
  28. } else if err != nil {
  29. return cli.NewExitError(fmt.Sprintf("Failed to check for logged in users: %v", err), 1)
  30. }
  31. } else if !c.GlobalIsSet("host") && !c.GlobalIsSet("user") {
  32. // check for global configured pair host/user
  33. cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
  34. if err != nil {
  35. return cli.NewExitError(fmt.Sprintf("Failed to load config from file: %v", err), 1)
  36. // set flags if found
  37. }
  38. // set flags if both were found in config
  39. if cfg.Default.Host != "" && cfg.Default.User != "" {
  40. err = c.GlobalSet("host", cfg.Default.Host)
  41. if err != nil {
  42. return cli.NewExitError(fmt.Sprintf("Failed to set host from global config: %v", err), 1)
  43. }
  44. err = c.GlobalSet("user", cfg.Default.User)
  45. if err != nil {
  46. return cli.NewExitError(fmt.Sprintf("Failed to set user from global config: %v", err), 1)
  47. }
  48. }
  49. }
  50. u, err := config.LoadUser(c)
  51. if err != nil {
  52. return cli.NewExitError(fmt.Sprintf("Couldn't load user: %v", err), 1)
  53. }
  54. if u == nil {
  55. return cli.NewExitError("You must be authenticated to "+action+".\nLog in first with: "+executable.Name()+" auth <username>", 1)
  56. }
  57. return f(c)
  58. }
  59. }
  60. // usersLoggedIn checks for logged in users for the set host flag
  61. // it returns the number of users and a slice of usernames
  62. func usersLoggedIn(c *cli.Context) (int, []string, error) {
  63. path, err := config.UserHostDir(c)
  64. if err != nil {
  65. return 0, nil, err
  66. }
  67. dir, err := os.Open(path)
  68. if err != nil {
  69. return 0, nil, err
  70. }
  71. contents, err := dir.Readdir(0)
  72. if err != nil {
  73. return 0, nil, err
  74. }
  75. var names []string
  76. for _, file := range contents {
  77. if file.IsDir() {
  78. // stat user.json
  79. if _, err := os.Stat(filepath.Join(path, file.Name(), "user.json")); err == nil {
  80. names = append(names, file.Name())
  81. }
  82. }
  83. }
  84. return len(names), names, nil
  85. }
  86. func cmdAuth(c *cli.Context) error {
  87. err := commands.CmdAuth(c)
  88. if err != nil {
  89. return err
  90. }
  91. // Get the username from the command, just like commands.CmdAuth does
  92. username := c.Args().Get(0)
  93. // Update config if this is user's first auth
  94. cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
  95. if err != nil {
  96. log.Errorln("Not saving config. Unable to load config: %s", err)
  97. return err
  98. }
  99. if cfg.Default.Host == "" && cfg.Default.User == "" {
  100. // This is user's first auth, so save defaults
  101. cfg.Default.Host = api.HostURL(c)
  102. cfg.Default.User = username
  103. err = config.SaveConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]), cfg)
  104. if err != nil {
  105. log.Errorln("Not saving config. Unable to save config: %s", err)
  106. return err
  107. }
  108. fmt.Printf("Set %s on %s as default account.\n", username, c.GlobalString("host"))
  109. }
  110. return nil
  111. }
  112. func cmdLogOut(c *cli.Context) error {
  113. err := commands.CmdLogOut(c)
  114. if err != nil {
  115. return err
  116. }
  117. // Remove this from config if it's the default account
  118. cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
  119. if err != nil {
  120. log.Errorln("Not updating config. Unable to load: %s", err)
  121. return err
  122. }
  123. username, err := config.CurrentUser(c)
  124. if err != nil {
  125. log.Errorln("Not updating config. Unable to load current user: %s", err)
  126. return err
  127. }
  128. reqHost := api.HostURL(c)
  129. if reqHost == "" {
  130. // No --host given, so we're using the default host
  131. reqHost = cfg.Default.Host
  132. }
  133. if cfg.Default.Host == reqHost && cfg.Default.User == username {
  134. // We're logging out of default username + host, so remove from config file
  135. cfg.Default.Host = ""
  136. cfg.Default.User = ""
  137. err = config.SaveConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]), cfg)
  138. if err != nil {
  139. log.Errorln("Not updating config. Unable to save config: %s", err)
  140. return err
  141. }
  142. }
  143. return nil
  144. }