util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "fmt"
  16. "os"
  17. "reflect"
  18. "regexp"
  19. )
  20. // Takes a list of strings and compiles them into a list of regular expressions
  21. func CompileRegexps(regexpStrings []string) ([]*regexp.Regexp, error) {
  22. regexps := []*regexp.Regexp{}
  23. for _, regexpStr := range regexpStrings {
  24. r, err := regexp.Compile(regexpStr)
  25. if err != nil {
  26. return []*regexp.Regexp{}, err
  27. }
  28. regexps = append(regexps, r)
  29. }
  30. return regexps, nil
  31. }
  32. // Detects if using systemd as the init system
  33. // Please note that simply reading /proc/1/cmdline can be misleading because
  34. // some installation of various init programs can automatically make /sbin/init
  35. // a symlink or even a renamed version of their main program.
  36. // TODO(dchen1107): realiably detects the init system using on the system:
  37. // systemd, upstart, initd, etc.
  38. func UsingSystemdInitSystem() bool {
  39. if _, err := os.Stat("/run/systemd/system"); err == nil {
  40. return true
  41. }
  42. return false
  43. }
  44. // Tests whether all pointer fields in a struct are nil. This is useful when,
  45. // for example, an API struct is handled by plugins which need to distinguish
  46. // "no plugin accepted this spec" from "this spec is empty".
  47. //
  48. // This function is only valid for structs and pointers to structs. Any other
  49. // type will cause a panic. Passing a typed nil pointer will return true.
  50. func AllPtrFieldsNil(obj interface{}) bool {
  51. v := reflect.ValueOf(obj)
  52. if !v.IsValid() {
  53. panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
  54. }
  55. if v.Kind() == reflect.Ptr {
  56. if v.IsNil() {
  57. return true
  58. }
  59. v = v.Elem()
  60. }
  61. for i := 0; i < v.NumField(); i++ {
  62. if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
  63. return false
  64. }
  65. }
  66. return true
  67. }
  68. func FileExists(filename string) (bool, error) {
  69. if _, err := os.Stat(filename); os.IsNotExist(err) {
  70. return false, nil
  71. } else if err != nil {
  72. return false, err
  73. }
  74. return true, nil
  75. }
  76. // borrowed from ioutil.ReadDir
  77. // ReadDir reads the directory named by dirname and returns
  78. // a list of directory entries, minus those with lstat errors
  79. func ReadDirNoExit(dirname string) ([]os.FileInfo, []error, error) {
  80. if dirname == "" {
  81. dirname = "."
  82. }
  83. f, err := os.Open(dirname)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. defer f.Close()
  88. names, err := f.Readdirnames(-1)
  89. list := make([]os.FileInfo, 0, len(names))
  90. errs := make([]error, 0, len(names))
  91. for _, filename := range names {
  92. fip, lerr := os.Lstat(dirname + "/" + filename)
  93. if os.IsNotExist(lerr) {
  94. // File disappeared between readdir + stat.
  95. // Just treat it as if it didn't exist.
  96. continue
  97. }
  98. list = append(list, fip)
  99. errs = append(errs, lerr)
  100. }
  101. return list, errs, nil
  102. }
  103. // IntPtr returns a pointer to an int
  104. func IntPtr(i int) *int {
  105. o := i
  106. return &o
  107. }
  108. // Int32Ptr returns a pointer to an int32
  109. func Int32Ptr(i int32) *int32 {
  110. o := i
  111. return &o
  112. }
  113. // IntPtrDerefOr dereference the int ptr and returns it i not nil,
  114. // else returns def.
  115. func IntPtrDerefOr(ptr *int, def int) int {
  116. if ptr != nil {
  117. return *ptr
  118. }
  119. return def
  120. }
  121. // Int32PtrDerefOr dereference the int32 ptr and returns it i not nil,
  122. // else returns def.
  123. func Int32PtrDerefOr(ptr *int32, def int32) int32 {
  124. if ptr != nil {
  125. return *ptr
  126. }
  127. return def
  128. }