github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/crossagent/crossagent.go (about)

     1  package crossagent
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  )
    10  
    11  var (
    12  	crossAgentDir = func() string {
    13  		if s := os.Getenv("NEW_RELIC_CROSS_AGENT_TESTS"); s != "" {
    14  			return s
    15  		}
    16  		_, here, _, _ := runtime.Caller(0)
    17  		return filepath.Join(filepath.Dir(here), "cross_agent_tests")
    18  	}()
    19  )
    20  
    21  // ReadFile reads a file from the crossagent tests directory given as with
    22  // ioutil.ReadFile.
    23  func ReadFile(name string) ([]byte, error) {
    24  	return ioutil.ReadFile(filepath.Join(crossAgentDir, name))
    25  }
    26  
    27  // ReadJSON takes the name of a file and parses it using JSON.Unmarshal into
    28  // the interface given.
    29  func ReadJSON(name string, v interface{}) error {
    30  	data, err := ReadFile(name)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	return json.Unmarshal(data, v)
    35  }
    36  
    37  // ReadDir reads a directory relative to crossagent tests and returns an array
    38  // of absolute filepaths of the files in that directory.
    39  func ReadDir(name string) ([]string, error) {
    40  	dir := filepath.Join(crossAgentDir, name)
    41  
    42  	entries, err := ioutil.ReadDir(dir)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	var files []string
    48  	for _, info := range entries {
    49  		if !info.IsDir() {
    50  			files = append(files, filepath.Join(dir, info.Name()))
    51  		}
    52  	}
    53  	return files, nil
    54  }