utils.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package util
  7. import (
  8. "context"
  9. "fmt"
  10. "net"
  11. "net/url"
  12. "reflect"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/thejerf/suture/v4"
  17. )
  18. type defaultParser interface {
  19. ParseDefault(string) error
  20. }
  21. // SetDefaults sets default values on a struct, based on the default annotation.
  22. func SetDefaults(data interface{}) {
  23. s := reflect.ValueOf(data).Elem()
  24. t := s.Type()
  25. for i := 0; i < s.NumField(); i++ {
  26. f := s.Field(i)
  27. tag := t.Field(i).Tag
  28. v := tag.Get("default")
  29. if len(v) > 0 {
  30. if f.CanInterface() {
  31. if parser, ok := f.Interface().(defaultParser); ok {
  32. if err := parser.ParseDefault(v); err != nil {
  33. panic(err)
  34. }
  35. continue
  36. }
  37. }
  38. if f.CanAddr() && f.Addr().CanInterface() {
  39. if parser, ok := f.Addr().Interface().(defaultParser); ok {
  40. if err := parser.ParseDefault(v); err != nil {
  41. panic(err)
  42. }
  43. continue
  44. }
  45. }
  46. switch f.Interface().(type) {
  47. case string:
  48. f.SetString(v)
  49. case int, uint32, int32, int64, uint64:
  50. i, err := strconv.ParseInt(v, 10, 64)
  51. if err != nil {
  52. panic(err)
  53. }
  54. f.SetInt(i)
  55. case float64, float32:
  56. i, err := strconv.ParseFloat(v, 64)
  57. if err != nil {
  58. panic(err)
  59. }
  60. f.SetFloat(i)
  61. case bool:
  62. f.SetBool(v == "true")
  63. case []string:
  64. // We don't do anything with string slices here. Any default
  65. // we set will be appended to by the XML decoder, so we fill
  66. // those after decoding.
  67. default:
  68. panic(f.Type())
  69. }
  70. } else if f.CanSet() && f.Kind() == reflect.Struct && f.CanAddr() {
  71. if addr := f.Addr(); addr.CanInterface() {
  72. SetDefaults(addr.Interface())
  73. }
  74. }
  75. }
  76. }
  77. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  78. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  79. fromStruct := reflect.ValueOf(from).Elem()
  80. fromType := fromStruct.Type()
  81. toStruct := reflect.ValueOf(to).Elem()
  82. toType := toStruct.Type()
  83. if fromType != toType {
  84. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  85. }
  86. for i := 0; i < toStruct.NumField(); i++ {
  87. fromField := fromStruct.Field(i)
  88. toField := toStruct.Field(i)
  89. if !toField.CanSet() {
  90. // Unexported fields
  91. continue
  92. }
  93. structTag := toType.Field(i).Tag
  94. v := structTag.Get(tag)
  95. if shouldCopy(v) {
  96. toField.Set(fromField)
  97. }
  98. }
  99. }
  100. // UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
  101. func UniqueTrimmedStrings(ss []string) []string {
  102. // Trim all first
  103. for i, v := range ss {
  104. ss[i] = strings.Trim(v, " ")
  105. }
  106. var m = make(map[string]struct{}, len(ss))
  107. var us = make([]string, 0, len(ss))
  108. for _, v := range ss {
  109. if _, ok := m[v]; ok {
  110. continue
  111. }
  112. m[v] = struct{}{}
  113. us = append(us, v)
  114. }
  115. return us
  116. }
  117. func FillNilExceptDeprecated(data interface{}) {
  118. fillNil(data, true)
  119. }
  120. func FillNil(data interface{}) {
  121. fillNil(data, false)
  122. }
  123. func fillNil(data interface{}, skipDeprecated bool) {
  124. s := reflect.ValueOf(data).Elem()
  125. t := s.Type()
  126. for i := 0; i < s.NumField(); i++ {
  127. if skipDeprecated && strings.HasPrefix(t.Field(i).Name, "Deprecated") {
  128. continue
  129. }
  130. f := s.Field(i)
  131. for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
  132. newValue := reflect.New(f.Type().Elem())
  133. f.Set(newValue)
  134. f = f.Elem()
  135. }
  136. if f.CanSet() {
  137. if f.IsZero() {
  138. switch f.Kind() {
  139. case reflect.Map:
  140. f.Set(reflect.MakeMap(f.Type()))
  141. case reflect.Slice:
  142. f.Set(reflect.MakeSlice(f.Type(), 0, 0))
  143. case reflect.Chan:
  144. f.Set(reflect.MakeChan(f.Type(), 0))
  145. }
  146. }
  147. switch f.Kind() {
  148. case reflect.Slice:
  149. if f.Type().Elem().Kind() != reflect.Struct {
  150. continue
  151. }
  152. for i := 0; i < f.Len(); i++ {
  153. fillNil(f.Index(i).Addr().Interface(), skipDeprecated)
  154. }
  155. case reflect.Struct:
  156. if f.CanAddr() {
  157. if addr := f.Addr(); addr.CanInterface() {
  158. fillNil(addr.Interface(), skipDeprecated)
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. // FillNilSlices sets default value on slices that are still nil.
  166. func FillNilSlices(data interface{}) error {
  167. s := reflect.ValueOf(data).Elem()
  168. t := s.Type()
  169. for i := 0; i < s.NumField(); i++ {
  170. f := s.Field(i)
  171. tag := t.Field(i).Tag
  172. v := tag.Get("default")
  173. if len(v) > 0 {
  174. switch f.Interface().(type) {
  175. case []string:
  176. if f.IsNil() {
  177. // Treat the default as a comma separated slice
  178. vs := strings.Split(v, ",")
  179. for i := range vs {
  180. vs[i] = strings.TrimSpace(vs[i])
  181. }
  182. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  183. for i, v := range vs {
  184. rv.Index(i).SetString(v)
  185. }
  186. f.Set(rv)
  187. }
  188. }
  189. }
  190. }
  191. return nil
  192. }
  193. // Address constructs a URL from the given network and hostname.
  194. func Address(network, host string) string {
  195. u := url.URL{
  196. Scheme: network,
  197. Host: host,
  198. }
  199. return u.String()
  200. }
  201. // AddressUnspecifiedLess is a comparator function preferring least specific network address (most widely listening,
  202. // namely preferring 0.0.0.0 over some IP), if both IPs are equal, it prefers the less restrictive network (prefers tcp
  203. // over tcp4)
  204. func AddressUnspecifiedLess(a, b net.Addr) bool {
  205. aIsUnspecified := false
  206. bIsUnspecified := false
  207. if host, _, err := net.SplitHostPort(a.String()); err == nil {
  208. aIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  209. }
  210. if host, _, err := net.SplitHostPort(b.String()); err == nil {
  211. bIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  212. }
  213. if aIsUnspecified == bIsUnspecified {
  214. return len(a.Network()) < len(b.Network())
  215. }
  216. return aIsUnspecified
  217. }
  218. type FatalErr struct {
  219. Err error
  220. Status ExitStatus
  221. }
  222. func (e *FatalErr) Error() string {
  223. return e.Err.Error()
  224. }
  225. func (e *FatalErr) Unwrap() error {
  226. return e.Err
  227. }
  228. func (e *FatalErr) Is(target error) bool {
  229. return target == suture.ErrTerminateSupervisorTree
  230. }
  231. // NoRestartErr wraps the given error err (which may be nil) to make sure that
  232. // `errors.Is(err, suture.ErrDoNotRestart) == true`.
  233. func NoRestartErr(err error) error {
  234. if err == nil {
  235. return suture.ErrDoNotRestart
  236. }
  237. return &noRestartErr{err}
  238. }
  239. type noRestartErr struct {
  240. err error
  241. }
  242. func (e *noRestartErr) Error() string {
  243. return e.err.Error()
  244. }
  245. func (e *noRestartErr) Unwrap() error {
  246. return e.err
  247. }
  248. func (e *noRestartErr) Is(target error) bool {
  249. return target == suture.ErrDoNotRestart
  250. }
  251. type ExitStatus int
  252. const (
  253. ExitSuccess ExitStatus = 0
  254. ExitError ExitStatus = 1
  255. ExitNoUpgradeAvailable ExitStatus = 2
  256. ExitRestart ExitStatus = 3
  257. ExitUpgrade ExitStatus = 4
  258. )
  259. func (s ExitStatus) AsInt() int {
  260. return int(s)
  261. }
  262. // OnDone calls fn when ctx is cancelled.
  263. func OnDone(ctx context.Context, fn func()) {
  264. go func() {
  265. <-ctx.Done()
  266. fn()
  267. }()
  268. }
  269. func CallWithContext(ctx context.Context, fn func() error) error {
  270. var err error
  271. done := make(chan struct{})
  272. go func() {
  273. err = fn()
  274. close(done)
  275. }()
  276. select {
  277. case <-done:
  278. return err
  279. case <-ctx.Done():
  280. return ctx.Err()
  281. }
  282. }
  283. func NiceDurationString(d time.Duration) string {
  284. switch {
  285. case d > 24*time.Hour:
  286. d = d.Round(time.Hour)
  287. case d > time.Hour:
  288. d = d.Round(time.Minute)
  289. case d > time.Minute:
  290. d = d.Round(time.Second)
  291. case d > time.Second:
  292. d = d.Round(time.Millisecond)
  293. case d > time.Millisecond:
  294. d = d.Round(time.Microsecond)
  295. }
  296. return d.String()
  297. }