Skip to content

Commit 335b10b

Browse files
committed
Add option to specify proxy
1 parent 9bf173f commit 335b10b

File tree

14 files changed

+41
-21
lines changed

14 files changed

+41
-21
lines changed

cmd/pbcli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
s := &settings.Settings{
3434
URL: cmds.URL,
3535
Username: cmds.Username,
36+
Proxy: cmds.Proxy,
3637
}
3738

3839
password := ui.GetPassword("Current password of user " + s.Username + ": ")

internal/api/mod.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,26 @@ func buildURL(baseStr string, endpoint string) *url.URL {
2525
}
2626

2727
// Request sends a HTTP request to the server.
28-
func Request(base, endpoint, method, username, password string, hasBody bool, data interface{}) (interface{}, error) {
29-
client := &http.Client{}
28+
func Request(base, endpoint, method, proxy, username, password string, hasBody bool, data interface{}) (interface{}, error) {
29+
transport := http.DefaultTransport
30+
31+
if len(proxy) > 0 {
32+
log.Printf("Using proxy '%s'", proxy)
33+
34+
proxyURL, err := url.Parse(proxy)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
transport = &http.Transport{
40+
Proxy: http.ProxyURL(proxyURL),
41+
}
42+
}
43+
44+
client := &http.Client{
45+
Transport: transport,
46+
}
47+
3048
url := buildURL(base, endpoint)
3149

3250
var reqBodyReader io.Reader
@@ -77,21 +95,21 @@ func Request(base, endpoint, method, username, password string, hasBody bool, da
7795
}
7896

7997
// Delete sends a HTTP DELETE request to the server.
80-
func Delete(base, endpoint, username, password string) (interface{}, error) {
81-
return Request(base, endpoint, "DELETE", username, password, false, nil)
98+
func Delete(base, endpoint, proxy, username, password string) (interface{}, error) {
99+
return Request(base, endpoint, "DELETE", proxy, username, password, false, nil)
82100
}
83101

84102
// Get sends a HTTP GET request to the server.
85-
func Get(base, endpoint, username, password string) (interface{}, error) {
86-
return Request(base, endpoint, "GET", username, password, false, nil)
103+
func Get(base, endpoint, proxy, username, password string) (interface{}, error) {
104+
return Request(base, endpoint, "GET", proxy, username, password, false, nil)
87105
}
88106

89107
// Post sends a HTTP POST request to the server.
90-
func Post(base, endpoint, username, password string, data interface{}) (interface{}, error) {
91-
return Request(base, endpoint, "POST", username, password, true, data)
108+
func Post(base, endpoint, proxy, username, password string, data interface{}) (interface{}, error) {
109+
return Request(base, endpoint, "POST", proxy, username, password, true, data)
92110
}
93111

94112
// Put sends a HTTP PUT request to the server.
95-
func Put(base, endpoint, username, password string, data interface{}) (interface{}, error) {
96-
return Request(base, endpoint, "PUT", username, password, true, data)
113+
func Put(base, endpoint, proxy, username, password string, data interface{}) (interface{}, error) {
114+
return Request(base, endpoint, "PUT", proxy, username, password, true, data)
97115
}

internal/application/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (c *createCommand) Run(s *settings.Settings, password string) {
3030
"strict_compatibility": c.StrictCompatibility,
3131
}
3232

33-
resp, err := api.Post(s.URL, createEndpoint, s.Username, password, data)
33+
resp, err := api.Post(s.URL, createEndpoint, s.Proxy, s.Username, password, data)
3434
if err != nil {
3535
log.Fatal(err)
3636
}

internal/application/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c *deleteCommand) Execute(args []string) error {
2727
func (c *deleteCommand) Run(s *settings.Settings, password string) {
2828
populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.Arguments.ID)
2929

30-
resp, err := api.Delete(s.URL, populatedEndpoint, s.Username, password)
30+
resp, err := api.Delete(s.URL, populatedEndpoint, s.Proxy, s.Username, password)
3131
if err != nil {
3232
log.Fatal(err)
3333
}

internal/application/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (c *listCommand) Execute(args []string) error {
2020
}
2121

2222
func (c *listCommand) Run(s *settings.Settings, password string) {
23-
resp, err := api.Get(s.URL, listEndpoint, s.Username, password)
23+
resp, err := api.Get(s.URL, listEndpoint, s.Proxy, s.Username, password)
2424
if err != nil {
2525
log.Fatal(err)
2626
}

internal/application/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c *showCommand) Execute(args []string) error {
2727
func (c *showCommand) Run(s *settings.Settings, password string) {
2828
populatedEndpoint := fmt.Sprintf(showEndpoint, c.Arguments.ID)
2929

30-
resp, err := api.Get(s.URL, populatedEndpoint, s.Username, password)
30+
resp, err := api.Get(s.URL, populatedEndpoint, s.Proxy, s.Username, password)
3131
if err != nil {
3232
log.Fatal(err)
3333
}

internal/application/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (c *updateCommand) Run(s *settings.Settings, password string) {
4343

4444
populatedEndpoint := fmt.Sprintf(updateEndpoint, c.Arguments.ID)
4545

46-
resp, err := api.Put(s.URL, populatedEndpoint, s.Username, password, data)
46+
resp, err := api.Put(s.URL, populatedEndpoint, s.Proxy, s.Username, password, data)
4747
if err != nil {
4848
log.Fatal(err)
4949
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package buildconfig
22

3-
var Version string
3+
var Version = "unknown"

internal/settings/mod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package settings
44
type Settings struct {
55
URL string `long:"url" description:"The URL where the server listens for requests" required:"true"`
66
Username string `long:"username" description:"The username for authenticating on the server" required:"true"`
7+
Proxy string `short:"x" long:"proxy" default:"" description:"The proxy to use for HTTP requests"`
78
}
89

910
// Runnable defines an interface for subcommands that take the global settings and a password.

internal/user/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c *createCommand) Run(s *settings.Settings, password string) {
3333
"matrix_id": c.Arguments.MatrixID,
3434
}
3535

36-
resp, err := api.Post(s.URL, createEndpoint, s.Username, password, data)
36+
resp, err := api.Post(s.URL, createEndpoint, s.Proxy, s.Username, password, data)
3737
if err != nil {
3838
log.Fatal(err)
3939
}

0 commit comments

Comments
 (0)