assets.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2015 Light Code Labs, LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package caddy
  15. import (
  16. "os"
  17. "path/filepath"
  18. "runtime"
  19. )
  20. // AssetsPath returns the path to the folder
  21. // where the application may store data. If
  22. // CADDYPATH env variable is set, that value
  23. // is used. Otherwise, the path is the result
  24. // of evaluating "$HOME/.caddy".
  25. func AssetsPath() string {
  26. if caddyPath := os.Getenv("CADDYPATH"); caddyPath != "" {
  27. return caddyPath
  28. }
  29. return filepath.Join(userHomeDir(), ".caddy")
  30. }
  31. // userHomeDir returns the user's home directory according to
  32. // environment variables.
  33. //
  34. // Credit: http://stackoverflow.com/a/7922977/1048862
  35. func userHomeDir() string {
  36. if runtime.GOOS == "windows" {
  37. home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
  38. if home == "" {
  39. home = os.Getenv("USERPROFILE")
  40. }
  41. return home
  42. }
  43. return os.Getenv("HOME")
  44. }