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