-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
107 lines (89 loc) · 3 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package hfutils
import (
"io/ioutil"
"net/http"
"os"
"os/exec"
"time"
)
// HFHTTPClient a HTTP client with timeout
type HFHTTPClient interface {
Get(url string) (*http.Response, []byte, error)
// Post(url string, body []string, timeout time.Duration)
}
// DefaultHTTPTimeout provides the default HTTP request timeout value
var DefaultHTTPTimeout = 10 * time.Second
// HTTPClient is an implementation of the HFHTTPClient interface for http requets
// with modifications specific to this project
type HTTPClient struct {
ConnTimeout time.Duration
}
// Get is a simplified Get with a configurable timeout value.
// You can provide the default package timeout by using DefaultHTTPTimeout as the timeout
// value
func (ht *HTTPClient) Get(url string) (*http.Response, []byte, error) {
tr := &http.Transport{
IdleConnTimeout: ht.ConnTimeout,
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
return resp, body, err
}
// OSCommandProvider provides the ability to run commands on the OS level
type OSCommandProvider interface {
Run(string, ...string) (*[]byte, error)
}
// OSCommand implements OSCommandProvider to provide the ability to run commands on the OS
type OSCommand struct{}
// Run runs a command on the OS
func (osc *OSCommand) Run(name string, arg ...string) (*[]byte, error) {
out, err := exec.Command(name, arg...).Output()
return &out, err
}
// DirReaderProvider provides the ability to read file directories on disk
type DirReaderProvider interface {
ReadDir(dirname string) ([]os.FileInfo, error)
ReadFile(filepath string) ([]byte, error)
GetHomeDirPath() string
}
// DirReader implements DirReaderProvider to provide the ability to read file directories on disk
type DirReader struct{}
// ReadDir reads the contents of a directory
func (dr *DirReader) ReadDir(dirname string) ([]os.FileInfo, error) {
return ioutil.ReadDir(dirname)
}
// ReadFile reads the contents of a file
func (dr *DirReader) ReadFile(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
// GetHomeDirPath mocks the path to the user's home directory
func (dr *DirReader) GetHomeDirPath() string {
return os.Getenv("HOME")
}
// EnvReader provides the ability to look up environment variables
type EnvReader interface {
LookupEnv(key string) (string, bool)
}
// EnvRead implements EnvReader to provide the ability to look up environment variables
type EnvRead struct{}
// LookupEnv retrieves the value of the environment variable named
// by the key. If the variable is present in the environment the
// value (which may be empty) is returned and the boolean is true.
// Otherwise the returned value will be empty and the boolean will
// be false.
func (envr *EnvRead) LookupEnv(key string) (string, bool) {
return os.LookupEnv(key)
}