/
utils.go
49 lines (43 loc) · 1.01 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package openapi
import (
"encoding/json"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"log"
"net/url"
)
func prettyPrint(v interface{}) {
b, _ := json.MarshalIndent(v, "", " ")
log.Printf(string(b))
log.Println()
}
func sPrettyPrint(v interface{}) string {
if v == nil {
return "nil"
}
b, _ := json.MarshalIndent(v, "", " ")
return string(b)
}
func expandPath(filePath string) (string, error) {
fullPath, err := homedir.Expand(filePath)
if err != nil {
return "", err
}
return fullPath, err
}
func getFileContent(filePath string) (string, error) {
fullPath, err := expandPath(filePath)
if err != nil {
return "", err
}
// #nosec G304 the path is only used to read a file not to execute it, additionally the contents of the file is not logged anywhere either preventing data leaks
data, err := ioutil.ReadFile(fullPath)
if err != nil {
return "", err
}
return string(data), nil
}
func isURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}