static.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package static
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func init() {
  11. runnable = cmd{}
  12. }
  13. type logger interface {
  14. Debug(string, ...interface{})
  15. Error(string, ...interface{})
  16. Info(string, ...interface{})
  17. }
  18. type runner interface {
  19. Run(string, ...string) ([]byte, error)
  20. }
  21. var stat = os.Stat
  22. var isNotExist = os.IsNotExist
  23. var extensions = []string{".md", ".mkd", ".markdown"}
  24. var runnable runner
  25. type cmd struct{}
  26. func (self cmd) Run(command string, args ...string) ([]byte, error) {
  27. return exec.Command(command, args...).Output()
  28. }
  29. func exists(path string) (bool, error) {
  30. _, err := stat(path)
  31. if err == nil {
  32. return true, nil
  33. }
  34. if isNotExist(err) {
  35. return false, nil
  36. }
  37. return false, err
  38. }
  39. func version(dir string) string {
  40. version := strconv.FormatInt(time.Now().Unix(), 10)
  41. out, err := runnable.Run("sh", "-c", "git", "-C", dir, "rev-parse", "--short", "HEAD")
  42. if err == nil {
  43. version = strings.Trim(string(out), "\n")
  44. }
  45. return version
  46. }
  47. func basename(name string) string {
  48. return filepath.Base(strings.TrimSuffix(name, filepath.Ext(name)))
  49. }
  50. func isMarkdown(path string) bool {
  51. for i := range extensions {
  52. if strings.HasSuffix(path, extensions[i]) {
  53. return true
  54. }
  55. }
  56. return false
  57. }