Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

98 wiersze
2.3 KiB

  1. // Copyright 2018 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package util
  14. import (
  15. "os"
  16. "strconv"
  17. "strings"
  18. )
  19. // ParseUint32s parses a slice of strings into a slice of uint32s.
  20. func ParseUint32s(ss []string) ([]uint32, error) {
  21. us := make([]uint32, 0, len(ss))
  22. for _, s := range ss {
  23. u, err := strconv.ParseUint(s, 10, 32)
  24. if err != nil {
  25. return nil, err
  26. }
  27. us = append(us, uint32(u))
  28. }
  29. return us, nil
  30. }
  31. // ParseUint64s parses a slice of strings into a slice of uint64s.
  32. func ParseUint64s(ss []string) ([]uint64, error) {
  33. us := make([]uint64, 0, len(ss))
  34. for _, s := range ss {
  35. u, err := strconv.ParseUint(s, 10, 64)
  36. if err != nil {
  37. return nil, err
  38. }
  39. us = append(us, u)
  40. }
  41. return us, nil
  42. }
  43. // ParsePInt64s parses a slice of strings into a slice of int64 pointers.
  44. func ParsePInt64s(ss []string) ([]*int64, error) {
  45. us := make([]*int64, 0, len(ss))
  46. for _, s := range ss {
  47. u, err := strconv.ParseInt(s, 10, 64)
  48. if err != nil {
  49. return nil, err
  50. }
  51. us = append(us, &u)
  52. }
  53. return us, nil
  54. }
  55. // ReadUintFromFile reads a file and attempts to parse a uint64 from it.
  56. func ReadUintFromFile(path string) (uint64, error) {
  57. data, err := os.ReadFile(path)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  62. }
  63. // ReadIntFromFile reads a file and attempts to parse a int64 from it.
  64. func ReadIntFromFile(path string) (int64, error) {
  65. data, err := os.ReadFile(path)
  66. if err != nil {
  67. return 0, err
  68. }
  69. return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
  70. }
  71. // ParseBool parses a string into a boolean pointer.
  72. func ParseBool(b string) *bool {
  73. var truth bool
  74. switch b {
  75. case "enabled":
  76. truth = true
  77. case "disabled":
  78. truth = false
  79. default:
  80. return nil
  81. }
  82. return &truth
  83. }