Skip to content

Commit 82ebb87

Browse files
committed
adding loghttp flag
* add flag * add *log.Logger to client * add factory method for client
1 parent f8b2bd6 commit 82ebb87

8 files changed

Lines changed: 73 additions & 23 deletions

File tree

client/client.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"fmt"
77
"github.com/spf13/viper"
88
"io/ioutil"
9+
"log"
910
"net/http"
1011
)
1112

1213
type rclient struct {
1314
c *http.Client
1415
config *viper.Viper
16+
logger *log.Logger
1517
}
1618

1719
const urlKey = "remoteit_url"
@@ -179,18 +181,39 @@ func (rc *rclient) do(req *http.Request, resp interface{}) (interface{}, error)
179181

180182
hresp, err := rc.c.Do(req)
181183

184+
var body []byte
185+
var errBodyRead error
186+
var statusCode int
187+
188+
// remote sensitve information from headers
189+
req.Header.Del("Authorization")
190+
req.Header.Del(devKeyHeader)
191+
req.Header.Del(tokenKeyHeader)
192+
193+
// try to read the body
194+
if err == nil && hresp != nil {
195+
statusCode = hresp.StatusCode
196+
body, errBodyRead = ioutil.ReadAll(hresp.Body)
197+
defer hresp.Body.Close()
198+
}
199+
200+
if rc.config.GetBool("loghttp") {
201+
rc.logger.Printf("Request: [%+v] Response Code: [%v] Response Body: [%s]", req, statusCode, string(body))
202+
}
203+
204+
182205
if err != nil {
183206
return nil, err
184207
}
185208

186-
defer hresp.Body.Close()
209+
if errBodyRead != nil {
210+
return nil, errBodyRead
211+
}
187212

188-
if hresp.StatusCode != http.StatusOK {
213+
if statusCode != http.StatusOK {
189214
return nil, fmt.Errorf("invalid status from server: %v %s", hresp.StatusCode, hresp.Status)
190215
}
191216

192-
body, err := ioutil.ReadAll(hresp.Body)
193-
194217
if err != nil {
195218
return nil, err
196219
}
@@ -200,7 +223,7 @@ func (rc *rclient) do(req *http.Request, resp interface{}) (interface{}, error)
200223
return resp, err
201224
}
202225

203-
func NewClient(config *viper.Viper, httpclient *http.Client) Client {
226+
func NewClient(config *viper.Viper, httpclient *http.Client, logger *log.Logger) Client {
204227
rc := new(rclient)
205228

206229
rc.config = config
@@ -211,5 +234,7 @@ func NewClient(config *viper.Viper, httpclient *http.Client) Client {
211234
rc.c = httpclient
212235
}
213236

237+
rc.logger = logger
238+
214239
return rc
215240
}

cmd/connect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var connectCmd = &cobra.Command{
2424
// big ugly function follows
2525
config := viper.GetViper()
2626

27-
rc := client.NewClient(config, nil)
27+
rc := getClient(config)
2828

2929
token := config.GetString("token")
3030

cmd/devices.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var deviceCmd = &cobra.Command{
2424

2525
config := viper.GetViper()
2626

27-
rc := client.NewClient(config, nil)
27+
rc := getClient(config)
2828

2929
token := config.GetString("token")
3030

cmd/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var loginCmd = &cobra.Command{
2626
// big ugly function follows
2727
config := viper.GetViper()
2828

29-
rc := client.NewClient(config, nil)
29+
rc := getClient(config)
3030

3131
apikey := config.GetString("apikey")
3232

cmd/root.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package cmd
22

33
import (
44
"fmt"
5+
"github.com/jasongerard/remoteit-cli/client"
56
"github.com/jasongerard/remoteit-cli/storage"
67
"github.com/spf13/cobra"
78
"github.com/spf13/viper"
9+
"log"
810
"os"
911
"strings"
1012
)
@@ -47,6 +49,9 @@ func init() {
4749

4850
rootCmd.PersistentFlags().Bool("noheader", false, "Disables header in output.")
4951
viper.BindPFlag("noheader", rootCmd.PersistentFlags().Lookup("noheader"))
52+
53+
rootCmd.PersistentFlags().Bool("loghttp", false, "Log HTTP request/response from API to ~/.remoteit/http.log")
54+
viper.BindPFlag("loghttp", rootCmd.PersistentFlags().Lookup("loghttp"))
5055
}
5156

5257
func Execute() {
@@ -65,6 +70,19 @@ func errorAndExit(err error, code int) {
6570
os.Exit(code)
6671
}
6772

73+
func getClient(config *viper.Viper) client.Client {
74+
75+
out, err := storage.GetHTTPLogWriter()
76+
77+
if err != nil {
78+
panic(err)
79+
}
80+
81+
logger := log.New(out, "HTTPLOG ", log.LstdFlags)
82+
83+
return client.NewClient(config, nil, logger)
84+
}
85+
6886
func getTokenFromFile() string {
6987
token, err := storage.GetToken()
7088

go.mod

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
module github.com/jasongerard/remoteit-cli
22

33
require (
4-
github.com/TylerBrock/colorjson v0.0.0-20180527164720-95ec53f28296
5-
github.com/fatih/color v1.7.0 // indirect
6-
github.com/mattn/go-colorable v0.0.9 // indirect
7-
github.com/mattn/go-isatty v0.0.4 // indirect
4+
github.com/BurntSushi/toml v0.3.1 // indirect
5+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
86
github.com/mitchellh/go-homedir v1.0.0
9-
github.com/pkg/errors v0.8.0
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
108
github.com/spf13/cobra v0.0.3
119
github.com/spf13/pflag v1.0.3 // indirect
1210
github.com/spf13/viper v1.2.1
11+
github.com/stretchr/testify v1.2.2 // indirect
1312
golang.org/x/crypto v0.0.0-20181106171534-e4dc69e5b2fd
1413
)

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
github.com/TylerBrock/colorjson v0.0.0-20180527164720-95ec53f28296 h1:JYWTroLXcNzSCgu66NMgdjwoMHQRbv2SoOVNFb4kRkE=
2-
github.com/TylerBrock/colorjson v0.0.0-20180527164720-95ec53f28296/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
1+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
5-
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
65
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
76
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
87
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
98
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
9+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
10+
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
1011
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
1112
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
12-
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
13-
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
14-
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
15-
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
1613
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
1714
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
1815
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
1916
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
2017
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
2118
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
22-
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
23-
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
19+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
20+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2421
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
2522
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
2623
github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg=
@@ -34,12 +31,15 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
3431
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
3532
github.com/spf13/viper v1.2.1 h1:bIcUwXqLseLF3BDAZduuNfekWG87ibtFxi59Bq+oI9M=
3633
github.com/spf13/viper v1.2.1/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaNVlI=
34+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
35+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
3736
golang.org/x/crypto v0.0.0-20181106171534-e4dc69e5b2fd h1:VtIkGDhk0ph3t+THbvXHfMZ8QHgsBO39Nh52+74pq7w=
3837
golang.org/x/crypto v0.0.0-20181106171534-e4dc69e5b2fd/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
3938
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
4039
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4140
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
4241
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
42+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4343
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4444
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
4545
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

storage/storage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/jasongerard/remoteit-cli/client"
77
"github.com/mitchellh/go-homedir"
8+
"io"
89
"io/ioutil"
910
"os"
1011
)
@@ -13,6 +14,7 @@ type StorageFile string
1314

1415
const LoginFile StorageFile = "login"
1516
const DeviceCacheFile StorageFile = "devices"
17+
const HTTPLogFile StorageFile = "http.log"
1618

1719
var configDir string
1820

@@ -38,6 +40,12 @@ func getPath(name StorageFile) string {
3840
return fmt.Sprintf("%s/%s", configDir, name)
3941
}
4042

43+
func GetHTTPLogWriter() (io.Writer, error) {
44+
p := getPath(HTTPLogFile)
45+
46+
return os.OpenFile(p, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
47+
}
48+
4149
func WriteFile(name StorageFile, b []byte) error {
4250
p := getPath(name)
4351

0 commit comments

Comments
 (0)