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.

112 lines
2.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "github.com/fatih/color"
  11. "github.com/fsnotify/fsnotify"
  12. )
  13. func readFile(path string) string {
  14. dat, err := ioutil.ReadFile(path)
  15. if err != nil {
  16. color.Red(path)
  17. }
  18. check(err)
  19. return string(dat)
  20. }
  21. func writeFile(path string, newContent string) {
  22. err := ioutil.WriteFile(path, []byte(newContent), 0644)
  23. check(err)
  24. color.Green(path)
  25. //color.Blue(newContent)
  26. }
  27. func getLines(text string) []string {
  28. lines := strings.Split(text, "\n")
  29. return lines
  30. }
  31. func concatStringsWithJumps(lines []string) string {
  32. var r string
  33. for _, l := range lines {
  34. r = r + l + "\n"
  35. }
  36. return r
  37. }
  38. func copyRaw(original string, destination string) {
  39. color.Green(original + " --> to --> " + destination)
  40. _, err := exec.Command("cp", "-rf", original, destination).Output()
  41. check(err)
  42. }
  43. var watcherInputFiles, watcherPublic *fsnotify.Watcher
  44. func watch(dir string) {
  45. var err error
  46. var watcher *fsnotify.Watcher
  47. if dir == "./blogo-input" {
  48. watcherInputFiles, err = fsnotify.NewWatcher()
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. watcher = watcherInputFiles
  53. } else {
  54. watcherPublic, err = fsnotify.NewWatcher()
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. watcher = watcherPublic
  59. }
  60. defer watcher.Close()
  61. if dir == "./blogo-input" {
  62. if err := filepath.Walk(dir, watchInputFilesDir); err != nil {
  63. log.Fatal(err)
  64. }
  65. } else {
  66. if err := filepath.Walk(dir, watchPublicDir); err != nil {
  67. log.Fatal(err)
  68. }
  69. }
  70. for {
  71. select {
  72. case event := <-watcher.Events:
  73. fmt.Printf("file system event: %#v\n", event)
  74. if dir == "./blogo-input" {
  75. generateHTML()
  76. }
  77. case err := <-watcher.Errors:
  78. log.Fatal("file system watcher error:", err)
  79. }
  80. }
  81. }
  82. // watchInputFilesDir gets run as a walk func, searching for directories to add watchers to
  83. func watchInputFilesDir(path string, fi os.FileInfo, err error) error {
  84. if fi.Mode().IsDir() {
  85. return watcherInputFiles.Add(path)
  86. }
  87. return nil
  88. }
  89. // watchPublicDir gets run as a walk func, searching for directories to add watchers to
  90. func watchPublicDir(path string, fi os.FileInfo, err error) error {
  91. if fi.Mode().IsDir() {
  92. return watcherPublic.Add(path)
  93. }
  94. return nil
  95. }