| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 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 | |
| 14 | package util |
| 15 | |
| 16 | import ( |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 17 | "errors" |
| 18 | "os" |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 19 | "strconv" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | // ParseUint32s parses a slice of strings into a slice of uint32s. |
| 24 | func ParseUint32s(ss []string) ([]uint32, error) { |
| 25 | us := make([]uint32, 0, len(ss)) |
| 26 | for _, s := range ss { |
| 27 | u, err := strconv.ParseUint(s, 10, 32) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | |
| 32 | us = append(us, uint32(u)) |
| 33 | } |
| 34 | |
| 35 | return us, nil |
| 36 | } |
| 37 | |
| 38 | // ParseUint64s parses a slice of strings into a slice of uint64s. |
| 39 | func ParseUint64s(ss []string) ([]uint64, error) { |
| 40 | us := make([]uint64, 0, len(ss)) |
| 41 | for _, s := range ss { |
| 42 | u, err := strconv.ParseUint(s, 10, 64) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | |
| 47 | us = append(us, u) |
| 48 | } |
| 49 | |
| 50 | return us, nil |
| 51 | } |
| 52 | |
| 53 | // ParsePInt64s parses a slice of strings into a slice of int64 pointers. |
| 54 | func ParsePInt64s(ss []string) ([]*int64, error) { |
| 55 | us := make([]*int64, 0, len(ss)) |
| 56 | for _, s := range ss { |
| 57 | u, err := strconv.ParseInt(s, 10, 64) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | us = append(us, &u) |
| 63 | } |
| 64 | |
| 65 | return us, nil |
| 66 | } |
| 67 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 68 | // Parses a uint64 from given hex in string. |
| 69 | func ParseHexUint64s(ss []string) ([]*uint64, error) { |
| 70 | us := make([]*uint64, 0, len(ss)) |
| 71 | for _, s := range ss { |
| 72 | u, err := strconv.ParseUint(s, 16, 64) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | |
| 77 | us = append(us, &u) |
| 78 | } |
| 79 | |
| 80 | return us, nil |
| 81 | } |
| 82 | |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 83 | // ReadUintFromFile reads a file and attempts to parse a uint64 from it. |
| 84 | func ReadUintFromFile(path string) (uint64, error) { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 85 | data, err := os.ReadFile(path) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 86 | if err != nil { |
| 87 | return 0, err |
| 88 | } |
| 89 | return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64) |
| 90 | } |
| 91 | |
| 92 | // ReadIntFromFile reads a file and attempts to parse a int64 from it. |
| 93 | func ReadIntFromFile(path string) (int64, error) { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 94 | data, err := os.ReadFile(path) |
| khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 95 | if err != nil { |
| 96 | return 0, err |
| 97 | } |
| 98 | return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64) |
| 99 | } |
| 100 | |
| 101 | // ParseBool parses a string into a boolean pointer. |
| 102 | func ParseBool(b string) *bool { |
| 103 | var truth bool |
| 104 | switch b { |
| 105 | case "enabled": |
| 106 | truth = true |
| 107 | case "disabled": |
| 108 | truth = false |
| 109 | default: |
| 110 | return nil |
| 111 | } |
| 112 | return &truth |
| 113 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 114 | |
| 115 | // ReadHexFromFile reads a file and attempts to parse a uint64 from a hexadecimal format 0xXX. |
| 116 | func ReadHexFromFile(path string) (uint64, error) { |
| 117 | data, err := os.ReadFile(path) |
| 118 | if err != nil { |
| 119 | return 0, err |
| 120 | } |
| 121 | hexString := strings.TrimSpace(string(data)) |
| 122 | if !strings.HasPrefix(hexString, "0x") { |
| 123 | return 0, errors.New("invalid format: hex string does not start with '0x'") |
| 124 | } |
| 125 | return strconv.ParseUint(hexString[2:], 16, 64) |
| 126 | } |