1
0

computed.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package conf
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. "sync"
  9. )
  10. // ℹ️ README: This file contains configuration values that require computation to be useful.
  11. // IsWindowsRuntime returns true if the current runtime in Windows.
  12. func IsWindowsRuntime() bool {
  13. return runtime.GOOS == "windows"
  14. }
  15. // IsProdMode returns true if the application is running in production mode.
  16. func IsProdMode() bool {
  17. return strings.EqualFold(App.RunMode, "prod")
  18. }
  19. var (
  20. appPath string
  21. appPathOnce sync.Once
  22. )
  23. // AppPath returns the absolute path of the application's binary.
  24. func AppPath() string {
  25. appPathOnce.Do(func() {
  26. var err error
  27. appPath, err = exec.LookPath(os.Args[0])
  28. if err != nil {
  29. panic("look executable path: " + err.Error())
  30. }
  31. appPath, err = filepath.Abs(appPath)
  32. if err != nil {
  33. panic("get absolute executable path: " + err.Error())
  34. }
  35. })
  36. return appPath
  37. }
  38. var (
  39. workDir string
  40. workDirOnce sync.Once
  41. )
  42. // WorkDir returns the absolute path of work directory. It reads the value of environment
  43. // variable GOGS_WORK_DIR. When not set, it uses the directory where the application's
  44. // binary is located.
  45. func WorkDir() string {
  46. workDirOnce.Do(func() {
  47. workDir = os.Getenv("GOGS_WORK_DIR")
  48. if workDir != "" {
  49. return
  50. }
  51. workDir = filepath.Dir(AppPath())
  52. })
  53. return workDir
  54. }
  55. var (
  56. customDir string
  57. customDirOnce sync.Once
  58. )
  59. // CustomDir returns the absolute path of the custom directory that contains local overrides.
  60. // It reads the value of environment variable GOGS_CUSTOM. When not set, it uses the work
  61. // directory returned by WorkDir function.
  62. func CustomDir() string {
  63. customDirOnce.Do(func() {
  64. customDir = os.Getenv("GOGS_CUSTOM")
  65. if customDir != "" {
  66. return
  67. }
  68. customDir = filepath.Join(WorkDir(), "custom")
  69. })
  70. return customDir
  71. }
  72. var (
  73. homeDir string
  74. homeDirOnce sync.Once
  75. )
  76. // HomeDir returns the home directory by reading environment variables. It may return empty
  77. // string when environment variables are not set.
  78. func HomeDir() string {
  79. homeDirOnce.Do(func() {
  80. homeDir = os.Getenv("HOME")
  81. if homeDir != "" {
  82. return
  83. }
  84. homeDir = os.Getenv("USERPROFILE")
  85. if homeDir != "" {
  86. return
  87. }
  88. homeDir = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
  89. })
  90. return homeDir
  91. }