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.

140 lines
3.3 KiB

  1. package api
  2. import (
  3. //"github.com/writeas/writeas-cli/sync"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "github.com/writeas/writeas-cli/config"
  9. "github.com/writeas/writeas-cli/executable"
  10. "github.com/writeas/writeas-cli/fileutils"
  11. "github.com/writeas/writeas-cli/log"
  12. cli "gopkg.in/urfave/cli.v1"
  13. )
  14. const (
  15. PostFileExt = ".txt"
  16. userFilename = "writeas_user"
  17. )
  18. func CmdPull(c *cli.Context) error {
  19. cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
  20. if err != nil {
  21. return err
  22. }
  23. // Create posts directory if needed
  24. if cfg.Posts.Directory == "" {
  25. syncSetUp(c, cfg)
  26. }
  27. cl, err := newClient(c)
  28. if err != nil {
  29. return err
  30. }
  31. u, _ := config.LoadUser(c)
  32. if u != nil {
  33. cl.SetToken(u.AccessToken)
  34. } else {
  35. return fmt.Errorf("Not currently logged in. Authenticate with: " + executable.Name() + " auth <username>")
  36. }
  37. // Fetch posts
  38. posts, err := cl.GetUserPosts()
  39. if err != nil {
  40. return err
  41. }
  42. for _, p := range *posts {
  43. postFilename := p.ID
  44. collDir := ""
  45. if p.Collection != nil {
  46. postFilename = p.Slug
  47. // Create directory for collection
  48. collDir = p.Collection.Alias
  49. if !fileutils.Exists(filepath.Join(cfg.Posts.Directory, collDir)) {
  50. log.Info(c, "Creating folder "+collDir)
  51. err = os.Mkdir(filepath.Join(cfg.Posts.Directory, collDir), 0755)
  52. if err != nil {
  53. log.Errorln("Error creating blog directory %s: %s. Skipping post %s.", collDir, err, postFilename)
  54. continue
  55. }
  56. }
  57. }
  58. postFilename += PostFileExt
  59. // Write file
  60. txtFile := p.Content
  61. if p.Title != "" {
  62. txtFile = "# " + p.Title + "\n\n" + txtFile
  63. }
  64. err = ioutil.WriteFile(filepath.Join(cfg.Posts.Directory, collDir, postFilename), []byte(txtFile), 0644)
  65. if err != nil {
  66. log.Errorln("Error creating file %s: %s", postFilename, err)
  67. }
  68. log.Info(c, "Saved post "+postFilename)
  69. // Update mtime and atime on files
  70. modTime := p.Updated.Local()
  71. err = os.Chtimes(filepath.Join(cfg.Posts.Directory, collDir, postFilename), modTime, modTime)
  72. if err != nil {
  73. log.Errorln("Error setting time on %s: %s", postFilename, err)
  74. }
  75. }
  76. return nil
  77. }
  78. func syncSetUp(c *cli.Context, cfg *config.Config) error {
  79. // Get user information and fail early (before we make the user do
  80. // anything), if we're going to
  81. u, err := config.LoadUser(c)
  82. if err != nil {
  83. return err
  84. }
  85. // Prompt for posts directory
  86. defaultDir, err := os.Getwd()
  87. if err != nil {
  88. return err
  89. }
  90. var dir string
  91. fmt.Printf("Posts directory? [%s]: ", defaultDir)
  92. fmt.Scanln(&dir)
  93. if dir == "" {
  94. dir = defaultDir
  95. }
  96. // FIXME: This only works on non-Windows OSes (fix: https://www.reddit.com/r/golang/comments/5t3ezd/hidden_files_directories/)
  97. userFilepath := filepath.Join(dir, "."+userFilename)
  98. // Create directory if needed
  99. if !fileutils.Exists(dir) {
  100. err = os.MkdirAll(dir, 0700)
  101. if err != nil {
  102. if config.Debug() {
  103. log.Errorln("Error creating data directory: %s", err)
  104. }
  105. return err
  106. }
  107. // Create username file in directory
  108. err = ioutil.WriteFile(userFilepath, []byte(u.User.Username), 0644)
  109. fmt.Println("Created posts directory.")
  110. }
  111. // Save preference
  112. cfg.Posts.Directory = dir
  113. err = config.SaveConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]), cfg)
  114. if err != nil {
  115. if config.Debug() {
  116. log.Errorln("Unable to save config: %s", err)
  117. }
  118. return err
  119. }
  120. fmt.Println("Saved config.")
  121. return nil
  122. }