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.

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