khenaidoo | ffe076b | 2019-01-15 16:08:08 -0500 | [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 ( |
| 17 | "io/ioutil" |
| 18 | "strconv" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
| 22 | // ParseUint32s parses a slice of strings into a slice of uint32s. |
| 23 | func ParseUint32s(ss []string) ([]uint32, error) { |
| 24 | us := make([]uint32, 0, len(ss)) |
| 25 | for _, s := range ss { |
| 26 | u, err := strconv.ParseUint(s, 10, 32) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | us = append(us, uint32(u)) |
| 32 | } |
| 33 | |
| 34 | return us, nil |
| 35 | } |
| 36 | |
| 37 | // ParseUint64s parses a slice of strings into a slice of uint64s. |
| 38 | func ParseUint64s(ss []string) ([]uint64, error) { |
| 39 | us := make([]uint64, 0, len(ss)) |
| 40 | for _, s := range ss { |
| 41 | u, err := strconv.ParseUint(s, 10, 64) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | us = append(us, u) |
| 47 | } |
| 48 | |
| 49 | return us, nil |
| 50 | } |
| 51 | |
| 52 | // ReadUintFromFile reads a file and attempts to parse a uint64 from it. |
| 53 | func ReadUintFromFile(path string) (uint64, error) { |
| 54 | data, err := ioutil.ReadFile(path) |
| 55 | if err != nil { |
| 56 | return 0, err |
| 57 | } |
| 58 | return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64) |
| 59 | } |