computed.go 2.4 KB

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