conf.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2014 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 base
  5. import (
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "github.com/Unknwon/com"
  12. "github.com/Unknwon/goconfig"
  13. )
  14. var Cfg *goconfig.ConfigFile
  15. func exeDir() (string, error) {
  16. file, err := exec.LookPath(os.Args[0])
  17. if err != nil {
  18. return "", err
  19. }
  20. p, err := filepath.Abs(file)
  21. if err != nil {
  22. return "", err
  23. }
  24. return path.Dir(p), nil
  25. }
  26. func init() {
  27. var err error
  28. workDir, err := exeDir()
  29. if err != nil {
  30. fmt.Printf("Fail to get work directory: %s\n", err)
  31. os.Exit(2)
  32. }
  33. cfgPathPrefix := filepath.Join(workDir, "conf")
  34. cfgPath := filepath.Join(cfgPathPrefix, "app.ini")
  35. Cfg, err = goconfig.LoadConfigFile(cfgPath)
  36. if err != nil {
  37. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  38. os.Exit(2)
  39. }
  40. cfgPath = filepath.Join(cfgPathPrefix, "custom.ini")
  41. if com.IsFile(cfgPath) {
  42. if err = Cfg.AppendFiles(cfgPath); err != nil {
  43. fmt.Printf("Cannot load config file '%s'\n", cfgPath)
  44. os.Exit(2)
  45. }
  46. }
  47. Cfg.BlockMode = false
  48. }