git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/sysinfo/util.go (about)

     1  // Copyright © 2016 Zlatko Čalušić
     2  //
     3  // Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
     4  
     5  package sysinfo
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"strings"
    11  )
    12  
    13  // Read one-liner text files, strip newline.
    14  func slurpFile(path string) string {
    15  	data, err := ioutil.ReadFile(path)
    16  	if err != nil {
    17  		return ""
    18  	}
    19  
    20  	return strings.TrimSpace(string(data))
    21  }
    22  
    23  // Write one-liner text files, add newline, ignore errors (best effort).
    24  func spewFile(path string, data string, perm os.FileMode) {
    25  	_ = ioutil.WriteFile(path, []byte(data+"\n"), perm)
    26  }