diff --git a/README.md b/README.md index fa1d1c2..066b433 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ where: * `--sdjson` is a self-describing JSON of the standard form `{ "schema": "iglu:...", "data": { ... } }` * `--schema` is a schema URI, most likely of the form `iglu:...` * `--json` is a (non-self-describing) JSON, of the form `{ ... }` +* `--ipaddress` is optional. It defaults to an empty string The idea here is that you can either send in a [**self-describing JSON**][sd-json], or pass in the constituent parts (i.e. a regular JSON plus a schema URI) and the Snowplow Tracking CLI will construct the final self-describing JSON for you. diff --git a/snowplowtrk.go b/snowplowtrk.go index bbe69bb..2ecf3bf 100644 --- a/snowplowtrk.go +++ b/snowplowtrk.go @@ -86,6 +86,11 @@ func main() { Name: "json, j", Usage: "Non-SelfDescribing JSON, of the form { ... }", }, + cli.StringFlag{ + Name: "ipaddress, ip", + Usage: "Track a custom IP Address (Optional)", + Value: "", + }, } // Set CLI Action @@ -97,6 +102,7 @@ func main() { sdjson := c.String("sdjson") schema := c.String("schema") jsonData := c.String("json") + ipAddress := c.String("ipaddress") // Check that collector domain exists if collector == "" { @@ -113,7 +119,7 @@ func main() { trackerChan := make(chan int, 1) // Send the event - tracker := initTracker(collector, appid, method, protocol, trackerChan) + tracker := initTracker(collector, appid, method, protocol, ipAddress, trackerChan) statusCode := trackSelfDescribingEvent(tracker, trackerChan, sdj) // Parse return code @@ -162,7 +168,7 @@ func getSdJSON(sdjson string, schema string, jsonData string) (*gt.SelfDescribin // initTracker creates a new Tracker ready for use // by the application. -func initTracker(collector string, appid string, method string, protocol string, trackerChan chan int) *gt.Tracker { +func initTracker(collector string, appid string, method string, protocol string, ipAddress string, trackerChan chan int) *gt.Tracker { // Create callback function callback := func(s []gt.CallbackResult, f []gt.CallbackResult) { @@ -185,6 +191,9 @@ func initTracker(collector string, appid string, method string, protocol string, gt.OptionStorage(gt.InitStorageMemory()), ) subject := gt.InitSubject() + if ipAddress != "" { + subject.SetIpAddress(ipAddress) + } tracker := gt.InitTracker( gt.RequireEmitter(emitter), gt.OptionSubject(subject), diff --git a/snowplowtrk_test.go b/snowplowtrk_test.go index 7e36bad..1112da5 100644 --- a/snowplowtrk_test.go +++ b/snowplowtrk_test.go @@ -74,7 +74,7 @@ func TestInitTracker(t *testing.T) { assert := assert.New(t) trackerChan := make(chan int, 1) - tracker := initTracker("com.acme", "myapp", "POST", "https", trackerChan) + tracker := initTracker("com.acme", "myapp", "POST", "https", "", trackerChan) assert.NotNil(tracker) assert.NotNil(tracker.Emitter) assert.NotNil(tracker.Subject) @@ -99,7 +99,7 @@ func TestTrackSelfDescribingEventGood(t *testing.T) { // Setup Tracker trackerChan := make(chan int, 1) - tracker := initTracker("com.acme", "myapp", "GET", "http", trackerChan) + tracker := initTracker("com.acme", "myapp", "GET", "http", "", trackerChan) assert.NotNil(tracker) // Make SDJ @@ -131,7 +131,7 @@ func TestTrackSelfDescribingEventBad(t *testing.T) { // Setup Tracker trackerChan := make(chan int, 1) - tracker := initTracker("com.acme", "myapp", "POST", "http", trackerChan) + tracker := initTracker("com.acme", "myapp", "POST", "http", "", trackerChan) assert.NotNil(tracker) // Make SDJ