-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathconfigure.go
123 lines (97 loc) · 2.76 KB
/
configure.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package httpu
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"github.com/hazbo/httpu/resource"
"github.com/hazbo/httpu/resource/request"
utils "github.com/hazbo/httpu/utils/common"
)
// Config is a wrapper for the Base config.
type Config struct {
Project Project `json:"project"`
}
// the base URL which is then extended in "request" Resources using a URI and a
// path within a Variant.
//
// Headers that are set here will apply to all requests, but can be overridden
// within an individual request / variant.
//
// The resources are read as []string from the JSON, however as they are being
// unmarsheled, the JSON for the filename is fetched and is unmarsheled into the
// Resources that exist within Base.
type Project struct {
URL url.URL `json:"url"`
ResourceFiles resource.FilePaths `json:"resourceFiles"`
ProjectPath string
Requests map[string]request.Request
}
const (
packagesDir = ".httpu/packages"
projectFileName = "project.json"
)
// ConfigureFromFile reads in a base JSON config file and decodes it into Config
func ConfigureFromFile(filePath string) error {
var (
cfg []byte
err error
c Config
)
// Set a default base project path
utils.ProjectPath = "./"
if cfg, err = ioutil.ReadFile(
fmt.Sprintf("./%s/%s", filePath, projectFileName)); err != nil {
// Look for project in the home packages directory
hd, _ := utils.HomeDir()
cfg, err = ioutil.ReadFile(fmt.Sprintf(
"%s/%s/%s/%s", hd, packagesDir, filePath, projectFileName))
if err != nil {
return fmt.Errorf("Error loading config: %s", err)
}
// Override the project path to the home directory path
utils.ProjectPath = fmt.Sprintf("%s/%s", hd, packagesDir)
}
err = json.Unmarshal(cfg, &c)
if err != nil {
return fmt.Errorf("Unable to parse JSON: %s", err)
}
c.Project.ProjectPath = fmt.Sprintf("%s/%s", utils.ProjectPath, filePath)
for _, rf := range c.Project.ResourceFiles {
err := rf.Load()
if err != nil {
return err
}
}
c.Project.Requests = resource.Requests
session = c.Project
return nil
}
var session Project
func Session() Project {
return session
}
// UnmarshalJSON is an implemenation of json.Unmarshaler and is used to parse
// the URL into a native url.URL type and the Headers into http.Header.
//
// If an invalid URL is passed, an error will occur at this point.
func (p *Project) UnmarshalJSON(j []byte) error {
type Alias Project
aux := &struct {
URL string `json:"url"`
*Alias
}{
Alias: (*Alias)(p),
}
if err := json.Unmarshal(j, &aux); err != nil {
return err
}
// Parse the URL to check it is valid
urlp, err := url.Parse(aux.URL)
if err != nil {
return fmt.Errorf("url must be a valid URL: %s", err)
}
// Set the URL to a parsed url of type url.URL
p.URL = *urlp
return nil
}