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.

58 lines
963 B

5 years ago
  1. package govue
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. )
  10. func pathExists(path string) bool {
  11. _, err := os.Stat(path)
  12. if err == nil {
  13. return true
  14. }
  15. if os.IsNotExist(err) {
  16. return false
  17. }
  18. return false
  19. }
  20. var selfFilePath string
  21. func getSelfFilePath() string {
  22. if selfFilePath == "" {
  23. file, err := exec.LookPath(os.Args[0])
  24. if err != nil {
  25. return ""
  26. }
  27. path, err := filepath.Abs(file)
  28. if err != nil {
  29. return ""
  30. }
  31. i := strings.LastIndex(path, "/")
  32. if i < 0 {
  33. i = strings.LastIndex(path, "\\")
  34. }
  35. if i < 0 {
  36. return ""
  37. }
  38. selfFilePath, _ = filepath.Abs(string(path[0 : i+1]))
  39. }
  40. return selfFilePath
  41. }
  42. func jsonToFile(file string, v interface{}) bool {
  43. data, err := json.Marshal(v)
  44. if err != nil {
  45. return false
  46. }
  47. return ioutil.WriteFile(file, data, 0644) == nil
  48. }
  49. func jsonByFile(file string, v interface{}) {
  50. data, _ := ioutil.ReadFile(file)
  51. json.Unmarshal(data, v)
  52. }