This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
233 lines (194 loc) · 5.37 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Pgg
* Copyright (C) 2019 Nicolò Santamaria
*
* Pgg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pgg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"os"
"io"
"fmt"
"flag"
"bytes"
"regexp"
"strings"
"net/url"
"net/http"
"io/ioutil"
"path/filepath"
"mime/multipart"
. "github.com/logrusorgru/aurora"
term "golang.org/x/crypto/ssh/terminal"
)
const PROGRAM_NAME = "pgg"
// TODO: find a way to do this in pure go
func isatty() bool {
fd := os.Stdout.Fd()
return term.IsTerminal(int(fd))
}
func escapeVars(rawVars []string) []string {
var esc []string
for _, s := range rawVars {
tok := strings.Split(s, "=")
esc = append(esc, fmt.Sprintf("{{%s}}", tok[0]), tok[1])
}
return esc
}
// Replace the variables in the url with their values defined in the config.
func formatUrl(rawUrl string, env Env) string {
var url string
var vars []string
vars = escapeVars(env.Vars)
r := strings.NewReplacer(vars...)
url = r.Replace(rawUrl)
if ok, _ := regexp.MatchString(`[a-z]+:\/\/`, url); !ok {
url = fmt.Sprintf("%s%s", env.Scheme, url)
}
return url
}
func die(msg interface{}) {
fmt.Println(BrightRed(msg))
os.Exit(1)
}
func check(err error) {
if err != nil {
die(err)
}
}
func getFileRequest(url, fpath, fieldname string) *http.Request {
var body *bytes.Buffer
file, err := os.Open(fpath)
check(err)
defer file.Close()
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(fieldname, filepath.Base(file.Name()))
check(err)
io.Copy(part, file)
writer.Close()
request, err := http.NewRequest("POST", url, body)
check(err)
request.Header.Add("Content-Type", writer.FormDataContentType())
return request
}
func doRequest(request *http.Request) (string, string) {
var client = &http.Client{}
response, err := client.Do(request)
check(err)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
check(err)
return string(body), response.Status
}
func populateForm(form *url.Values, data map[string]string) {
for key, value := range data {
form.Add(key, value)
}
}
func usage() {
var msg = `pgg - Post from the Get-Go
Pgg is a tool that allows you to make http request.
When starting pgg looks for configuration files in the following order:
1. ~/.config/pgg/config
2. ~/.pgg/config
SYNOPSIS
pgg [options] http://foobar.org
OPTIONS
-m, -method
Specify the request method. (default GET)
-e, -env
Specify the environment to use.
-c, -cfg
Specify an alternative config file.
-f, -file
Specify a file to upload.
-fo, -form
Specify the form to use.
-h, -help
Prints this help message.`
fmt.Println(msg)
}
func main() {
var env Env
var err error
var cfg Config
var fmtUrl string
var method string // the request method
var envName string // the environment name
var cfgPath string // path to the config file
var fileFlag string // path to the file to upload
var formFlag string // form to use in the request
var form url.Values
var request *http.Request
// parse the argument and gets the flags values.
flag.StringVar(&method, "method", "GET", "Request method")
flag.StringVar(&method, "m", "GET", "Request method")
flag.StringVar(&envName, "env", "", "Environment to use")
flag.StringVar(&envName, "e", "", "Environment to use")
flag.StringVar(&cfgPath, "cfg", "", "Config file")
flag.StringVar(&cfgPath, "c", "", "Config file")
flag.StringVar(&fileFlag, "file", "", "Path to the file to upload")
flag.StringVar(&fileFlag, "f", "", "Path to the file to upload")
flag.StringVar(&formFlag, "fo", "", "Form to use")
flag.StringVar(&formFlag, "form", "", "Form to use")
flag.Usage = usage
flag.Parse()
if flag.NArg() == 0 {
usage()
return
}
// If no cfg file specified in argument look in the default paths.
if cfgPath == "" {
cfgPath, err = configLookup()
check(err)
}
cfg, err = loadConfig(cfgPath)
check(err)
// The flag value overrides the default.
if envName == "" {
envName = cfg.DefaultEnv
}
var ok bool
if env, ok = cfg.Envs[envName]; !ok {
die(fmt.Sprintf("error: cannot find environment %s.", envName))
}
fmtUrl = formatUrl(flag.Arg(flag.NArg()-1), env)
if formFlag != "" {
frm, ok := cfg.Forms[formFlag]
if !ok {
die(fmt.Sprintf("error: cannot find form %s.", formFlag))
}
populateForm(&form, frm)
}
// handle the file upload
if fileFlag != "" {
tokens := strings.Split(fileFlag, "=")
request = getFileRequest(fmtUrl, tokens[1], tokens[0])
} else {
request, err = http.NewRequest(strings.ToUpper(method), fmtUrl, strings.NewReader(form.Encode()))
check(err)
}
body, status := doRequest(request)
if isatty() {
status := BrightMagenta(fmt.Sprintf("Status: %s", status))
url := BrightGreen(fmtUrl)
if body != "" {
fmt.Printf("%s\n%s\n%s\n", body, status, url)
} else {
fmt.Printf("%s\n%s\n", status, url)
}
} else {
fmt.Println(body)
}
}