init.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * @Author: LinkLeong link@icewhale.com
  3. * @Date: 2022-05-13 18:15:46
  4. * @LastEditors: LinkLeong
  5. * @LastEditTime: 2022-09-05 11:58:02
  6. * @FilePath: /CasaOS/pkg/config/init.go
  7. * @Description:
  8. * @Website: https://www.casaos.io
  9. * Copyright (c) 2022 by icewhale, All Rights Reserved.
  10. */
  11. package config
  12. import (
  13. "fmt"
  14. "log"
  15. "os"
  16. "path/filepath"
  17. "github.com/IceWhaleTech/CasaOS-Common/utils/constants"
  18. "github.com/IceWhaleTech/CasaOS/common"
  19. "github.com/IceWhaleTech/CasaOS/model"
  20. "github.com/go-ini/ini"
  21. )
  22. var (
  23. SysInfo = &model.SysInfoModel{}
  24. AppInfo = &model.APPModel{
  25. DBPath: constants.DefaultDataPath,
  26. LogPath: constants.DefaultLogPath,
  27. LogSaveName: common.SERVICENAME,
  28. LogFileExt: "log",
  29. ShellPath: "/usr/share/casaos/shell",
  30. UserDataPath: filepath.Join(constants.DefaultDataPath, "conf"),
  31. }
  32. CommonInfo = &model.CommonModel{
  33. RuntimePath: constants.DefaultRuntimePath,
  34. }
  35. ServerInfo = &model.ServerModel{}
  36. SystemConfigInfo = &model.SystemConfig{}
  37. FileSettingInfo = &model.FileSetting{}
  38. Cfg *ini.File
  39. ConfigFilePath string
  40. )
  41. // 初始化设置,获取系统的部分信息。
  42. func InitSetup(config string, sample string) {
  43. ConfigFilePath = CasaOSConfigFilePath
  44. if len(config) > 0 {
  45. ConfigFilePath = config
  46. }
  47. // create default config file if not exist
  48. if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) {
  49. fmt.Println("config file not exist, create it")
  50. // create config file
  51. file, err := os.Create(ConfigFilePath)
  52. if err != nil {
  53. panic(err)
  54. }
  55. defer file.Close()
  56. // write default config
  57. _, err = file.WriteString(sample)
  58. if err != nil {
  59. panic(err)
  60. }
  61. }
  62. var err error
  63. // 读取文件
  64. Cfg, err = ini.Load(ConfigFilePath)
  65. if err != nil {
  66. panic(err)
  67. }
  68. mapTo("app", AppInfo)
  69. mapTo("server", ServerInfo)
  70. mapTo("system", SystemConfigInfo)
  71. mapTo("file", FileSettingInfo)
  72. mapTo("common", CommonInfo)
  73. }
  74. // 映射
  75. func mapTo(section string, v interface{}) {
  76. err := Cfg.Section(section).MapTo(v)
  77. if err != nil {
  78. log.Fatalf("Cfg.MapTo %s err: %v", section, err)
  79. }
  80. }