system.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package service
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/IceWhaleTech/CasaOS/pkg/config"
  8. command2 "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
  9. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  10. "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
  11. )
  12. type SystemService interface {
  13. UpSystemConfig(str string, widget string)
  14. UpdateSystemVersion(version string)
  15. GetSystemConfigDebug() []string
  16. GetCasaOSLogs(lineNumber int) string
  17. }
  18. type systemService struct {
  19. log loger.OLog
  20. }
  21. func (s *systemService) UpdateSystemVersion(version string) {
  22. //command2.OnlyExec(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version)
  23. //s.log.Error(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version)
  24. s.log.Error(command2.ExecResultStrArray("source " + config.AppInfo.ProjectPath + "/shell/tools.sh ;update " + version))
  25. //s.log.Error(command2.ExecResultStr(config.AppInfo.ProjectPath + "/shell/tool.sh -r " + version))
  26. }
  27. func (s *systemService) GetSystemConfigDebug() []string {
  28. return command2.ExecResultStrArray("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;GetSysInfo")
  29. }
  30. func (s *systemService) UpSystemConfig(str string, widget string) {
  31. if len(str) > 0 && str != config.SystemConfigInfo.ConfigStr {
  32. config.Cfg.Section("system").Key("ConfigStr").SetValue(str)
  33. config.SystemConfigInfo.ConfigStr = str
  34. }
  35. if len(widget) > 0 && widget != config.SystemConfigInfo.WidgetList {
  36. config.Cfg.Section("system").Key("WidgetList").SetValue(widget)
  37. config.SystemConfigInfo.WidgetList = widget
  38. }
  39. config.Cfg.SaveTo("conf/conf.ini")
  40. }
  41. func (s *systemService) GetCasaOSLogs(lineNumber int) string {
  42. reader, err := file.NewReadLineFromEnd(s.log.Path())
  43. if err != nil {
  44. return ""
  45. }
  46. defer reader.Close()
  47. test, err := reader.ReadLine()
  48. fmt.Println(err)
  49. fmt.Println(test)
  50. return string(test)
  51. file, _ := os.Open(s.log.Path())
  52. fileScanner := bufio.NewReader(file)
  53. lineNumber = 5
  54. lineCount := 1
  55. var r string
  56. for i := lineCount; i < lineNumber; i++ {
  57. line, _, err := fileScanner.ReadLine()
  58. r += string(line)
  59. if err == io.EOF {
  60. return r
  61. }
  62. // 如下是某些业务逻辑操作
  63. // 如下代码打印每次读取的文件行内容
  64. }
  65. defer file.Close()
  66. return r
  67. }
  68. func NewSystemService(log loger.OLog) SystemService {
  69. return &systemService{log: log}
  70. }