util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // ReadDirNoStat returns a string of files/directories contained
  77. // in dirname without calling lstat on them.
  78. func ReadDirNoStat(dirname string) ([]string, error) {
  79. if dirname == "" {
  80. dirname = "."
  81. }
  82. f, err := os.Open(dirname)
  83. if err != nil {
  84. return nil, err
  85. }
  86. defer f.Close()
  87. return f.Readdirnames(-1)
  88. }
  89. // IntPtr returns a pointer to an int
  90. func IntPtr(i int) *int {
  91. o := i
  92. return &o
  93. }
  94. // Int32Ptr returns a pointer to an int32
  95. func Int32Ptr(i int32) *int32 {
  96. o := i
  97. return &o
  98. }
  99. // IntPtrDerefOr dereference the int ptr and returns it i not nil,
  100. // else returns def.
  101. func IntPtrDerefOr(ptr *int, def int) int {
  102. if ptr != nil {
  103. return *ptr
  104. }
  105. return def
  106. }
  107. // Int32PtrDerefOr dereference the int32 ptr and returns it i not nil,
  108. // else returns def.
  109. func Int32PtrDerefOr(ptr *int32, def int32) int32 {
  110. if ptr != nil {
  111. return *ptr
  112. }
  113. return def
  114. }