go.undefinedlabs.com/scopeagent@v0.4.2/agent/util.go (about) 1 package agent 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "github.com/vmihailenco/msgpack" 7 "os" 8 ) 9 10 func addToMapIfEmpty(dest map[string]interface{}, source map[string]interface{}) { 11 if source == nil { 12 return 13 } 14 for k, newValue := range source { 15 if oldValue, ok := dest[k]; !ok || oldValue == "" { 16 dest[k] = newValue 17 } 18 } 19 } 20 21 func addElementToMapIfEmpty(source map[string]interface{}, key string, value interface{}) { 22 if val, ok := source[key]; !ok || val == "" { 23 source[key] = value 24 } 25 } 26 27 func getSourceRootFromEnv(key string) string { 28 if value, ok := os.LookupEnv(key); ok { 29 // We check if is a valid and existing folder 30 if fInfo, err := os.Stat(value); err == nil && fInfo.IsDir() { 31 return value 32 } 33 } 34 return "" 35 } 36 37 // Encodes `payload` using msgpack and compress it with gzip 38 func msgPackEncodePayload(payload interface{}) (*bytes.Buffer, error) { 39 binaryPayload, err := msgpack.Marshal(payload) 40 if err != nil { 41 return nil, err 42 } 43 44 var buf bytes.Buffer 45 zw := gzip.NewWriter(&buf) 46 _, err = zw.Write(binaryPayload) 47 if err != nil { 48 return nil, err 49 } 50 if err := zw.Close(); err != nil { 51 return nil, err 52 } 53 54 return &buf, nil 55 } 56 57 // Gets if the slice contains an item 58 func sliceContains(slice []string, item string) bool { 59 if slice == nil { 60 return false 61 } 62 for _, a := range slice { 63 if a == item { 64 return true 65 } 66 } 67 return false 68 }