Skip to content

Commit

Permalink
Merge pull request #4 from cloudflare/elithrar/fields
Browse files Browse the repository at this point in the history
[feat] Add fields support
  • Loading branch information
elithrar authored Aug 22, 2017
2 parents 39af769 + 8d4fd32 commit 987f8ce
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 585 deletions.
8 changes: 1 addition & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion cmd/logshare-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ func run(conf *config) func(c *cli.Context) error {
conf.zoneID = id
}

client, err := logshare.New(conf.apiKey, conf.apiEmail, &logshare.Options{ByReceived: conf.byReceived})
client, err := logshare.New(
conf.apiKey,
conf.apiEmail,
&logshare.Options{
ByReceived: conf.byReceived,
Fields: conf.fields,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -88,6 +94,7 @@ func parseFlags(conf *config, c *cli.Context) error {
conf.endTime = c.Int64("end-time")
conf.count = c.Int("count")
conf.byReceived = c.Bool("by-received")
conf.fields = c.StringSlice("fields")

return conf.Validate()
}
Expand All @@ -102,13 +109,18 @@ type config struct {
endTime int64
count int
byReceived bool
fields []string
}

func (conf *config) Validate() error {
if conf.zoneID == "" && conf.zoneName == "" {
return errors.New("zone-name OR zone-id must be set")
}

if len(conf.fields) > 0 && !conf.byReceived {
return errors.New("specifying --fields is only supported when using the --by-received endpoint")
}

// if conf.count -1 || conf.count > 0 {
// return errors.New("count must be > 0, or set to -1 (no limit)")
// }
Expand Down Expand Up @@ -156,4 +168,8 @@ var flags = []cli.Flag{
Name: "by-received",
Usage: "Retrieve logs by the processing time on Cloudflare. This mode allows you to fetch all available logs vs. based on the log timestamps themselves.",
},
cli.StringSliceFlag{
Name: "fields",
Usage: "Select specific fields to retrieve in the log response. Pass a comma-separated list to fields to specify multiple fields.",
},
}
19 changes: 19 additions & 0 deletions logshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"os"
"strings"
"time"

"github.com/pkg/errors"
Expand All @@ -24,6 +25,7 @@ type Client struct {
apiKey string
apiEmail string
byReceived bool
fields []string
httpClient *http.Client
dest io.Writer
headers http.Header
Expand Down Expand Up @@ -79,6 +81,10 @@ func New(apiKey string, apiEmail string, options *Options) (*Client, error) {
byReceived: byReceived,
}

if options != nil && options.Fields != nil {
client.fields = options.Fields
}

return client, nil
}

Expand All @@ -91,6 +97,15 @@ func (c *Client) buildURL(zoneID string) string {
return fmt.Sprintf("%s/zones/%s/logs/%s", c.endpoint, zoneID, endpoint)
}

func (c *Client) addFieldParams(url string) string {
// The fields param is only supported on the Logpull endpoint
if !c.byReceived || len(c.fields) < 1 {
return url
}

return url + "&fields=" + strings.Join(c.fields, ",")
}

// GetFromRayID fetches logs for the given rayID, or starting at the given rayID
// if a non-zero end timestamp is provided.
func (c *Client) GetFromRayID(zoneID string, rayID string, end int64, count int) (*Meta, error) {
Expand All @@ -104,6 +119,8 @@ func (c *Client) GetFromRayID(zoneID string, rayID string, end int64, count int)
url += fmt.Sprintf("&count=%d", count)
}

url = c.addFieldParams(url)

return c.request(url)
}

Expand All @@ -120,6 +137,8 @@ func (c *Client) GetFromTimestamp(zoneID string, start int64, end int64, count i
url += fmt.Sprintf("&count=%d", count)
}

url = c.addFieldParams(url)

return c.request(url)
}

Expand Down
40 changes: 0 additions & 40 deletions vendor/github.com/cloudflare/logshare/.travis.yml

This file was deleted.

27 changes: 0 additions & 27 deletions vendor/github.com/cloudflare/logshare/LICENSE

This file was deleted.

143 changes: 0 additions & 143 deletions vendor/github.com/cloudflare/logshare/README.md

This file was deleted.

Loading

0 comments on commit 987f8ce

Please sign in to comment.