staticmd.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package staticmd
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // check that a path exists
  11. // does not care if it is a directory
  12. // will not say whether user has rw access, but
  13. // will throw an error if the user cannot read the parent directory
  14. func exists(path string) (bool, error) {
  15. _, err := os.Stat(path)
  16. if err == nil {
  17. return true, nil
  18. }
  19. if os.IsNotExist(err) {
  20. return false, nil
  21. }
  22. return false, err
  23. }
  24. // if within a git repo, gets git version as a short-hash
  25. // otherwise falls back to a unix timestamp
  26. func version() string {
  27. version := strconv.FormatInt(time.Now().Unix(), 10)
  28. out, err := exec.Command("sh", "-c", "git rev-parse --short HEAD").Output()
  29. if err == nil {
  30. version = strings.Trim(string(out), "\n")
  31. }
  32. return version
  33. }
  34. // remove the path and extension from a given filename
  35. func basename(name string) string {
  36. return filepath.Base(strings.TrimSuffix(name, filepath.Ext(name)))
  37. }
  38. type logger interface {
  39. Debug(string, ...interface{})
  40. Error(string, ...interface{})
  41. Info(string, ...interface{})
  42. }