...

Source file src/github.com/prometheus/procfs/internal/util/parse.go

Documentation: github.com/prometheus/procfs/internal/util

     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  	"os"
    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  // ParsePInt64s parses a slice of strings into a slice of int64 pointers.
    53  func ParsePInt64s(ss []string) ([]*int64, error) {
    54  	us := make([]*int64, 0, len(ss))
    55  	for _, s := range ss {
    56  		u, err := strconv.ParseInt(s, 10, 64)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  
    61  		us = append(us, &u)
    62  	}
    63  
    64  	return us, nil
    65  }
    66  
    67  // Parses a uint64 from given hex in string.
    68  func ParseHexUint64s(ss []string) ([]*uint64, error) {
    69  	us := make([]*uint64, 0, len(ss))
    70  	for _, s := range ss {
    71  		u, err := strconv.ParseUint(s, 16, 64)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  
    76  		us = append(us, &u)
    77  	}
    78  
    79  	return us, nil
    80  }
    81  
    82  // ReadUintFromFile reads a file and attempts to parse a uint64 from it.
    83  func ReadUintFromFile(path string) (uint64, error) {
    84  	data, err := os.ReadFile(path)
    85  	if err != nil {
    86  		return 0, err
    87  	}
    88  	return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
    89  }
    90  
    91  // ReadIntFromFile reads a file and attempts to parse a int64 from it.
    92  func ReadIntFromFile(path string) (int64, error) {
    93  	data, err := os.ReadFile(path)
    94  	if err != nil {
    95  		return 0, err
    96  	}
    97  	return strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
    98  }
    99  
   100  // ParseBool parses a string into a boolean pointer.
   101  func ParseBool(b string) *bool {
   102  	var truth bool
   103  	switch b {
   104  	case "enabled":
   105  		truth = true
   106  	case "disabled":
   107  		truth = false
   108  	default:
   109  		return nil
   110  	}
   111  	return &truth
   112  }
   113  

View as plain text