github.com/blend/go-sdk@v1.20240719.1/certutil/read_files.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package certutil
     9  
    10  import (
    11  	"os"
    12  
    13  	"github.com/blend/go-sdk/ex"
    14  )
    15  
    16  // Errors
    17  const (
    18  	ErrInvalidCertPEM ex.Class = "failed to add cert to pool as pem"
    19  )
    20  
    21  // MustBytes panics on an error or returns the contents.
    22  func MustBytes(contents []byte, err error) []byte {
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  	return contents
    27  }
    28  
    29  // BytesWithError returns a bytes error response with the error
    30  // as an ex.
    31  func BytesWithError(bytes []byte, err error) ([]byte, error) {
    32  	return bytes, ex.New(err)
    33  }
    34  
    35  // ReadFiles reads a list of files as bytes.
    36  func ReadFiles(files ...string) (data [][]byte, err error) {
    37  	var contents []byte
    38  	for _, path := range files {
    39  		contents, err = os.ReadFile(path)
    40  		if err != nil {
    41  			return nil, ex.New(err)
    42  		}
    43  		data = append(data, contents)
    44  	}
    45  	return
    46  }