tool.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2017 Xiaomi, Inc.
  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 g
  15. import (
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "strings"
  20. )
  21. func HasLogfile(name string) bool {
  22. if _, err := os.Stat(LogPath(name)); err != nil {
  23. return false
  24. }
  25. return true
  26. }
  27. func PreqOrder(moduleArgs []string) []string {
  28. if len(moduleArgs) == 0 {
  29. return []string{}
  30. }
  31. var modulesInOrder []string
  32. // get arguments which are found in the order
  33. for _, nameOrder := range AllModulesInOrder {
  34. for _, nameArg := range moduleArgs {
  35. if nameOrder == nameArg {
  36. modulesInOrder = append(modulesInOrder, nameOrder)
  37. }
  38. }
  39. }
  40. // get arguments which are not found in the order
  41. for _, nameArg := range moduleArgs {
  42. end := 0
  43. for _, nameOrder := range modulesInOrder {
  44. if nameOrder == nameArg {
  45. break
  46. }
  47. end++
  48. }
  49. if end == len(modulesInOrder) {
  50. modulesInOrder = append(modulesInOrder, nameArg)
  51. }
  52. }
  53. return modulesInOrder
  54. }
  55. func Rel(p string) string {
  56. wd, err := os.Getwd()
  57. if err != nil {
  58. return ""
  59. }
  60. // filepath.Abs() returns an error only when os.Getwd() returns an error;
  61. abs, _ := filepath.Abs(p)
  62. r, err := filepath.Rel(wd, abs)
  63. if err != nil {
  64. return ""
  65. }
  66. return r
  67. }
  68. func HasCfg(name string) bool {
  69. if _, err := os.Stat(Cfg(name)); err != nil {
  70. return false
  71. }
  72. return true
  73. }
  74. func HasModule(name string) bool {
  75. return Modules[name]
  76. }
  77. func setPid(name string) {
  78. output, _ := exec.Command("pgrep", "-f", ModuleApps[name]).Output()
  79. pidStr := strings.TrimSpace(string(output))
  80. PidOf[name] = pidStr
  81. }
  82. func Pid(name string) string {
  83. if PidOf[name] == "<NOT SET>" {
  84. setPid(name)
  85. }
  86. return PidOf[name]
  87. }
  88. func IsRunning(name string) bool {
  89. setPid(name)
  90. return Pid(name) != ""
  91. }
  92. func RmDup(args []string) []string {
  93. if len(args) == 0 {
  94. return []string{}
  95. }
  96. if len(args) == 1 {
  97. return args
  98. }
  99. ret := []string{}
  100. isDup := make(map[string]bool)
  101. for _, arg := range args {
  102. if isDup[arg] == true {
  103. continue
  104. }
  105. ret = append(ret, arg)
  106. isDup[arg] = true
  107. }
  108. return ret
  109. }