Skip to content

Commit

Permalink
Add --livemode flag to listen and logs tail commands (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Sep 6, 2019
1 parent 86491be commit 41a7831
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
4 changes: 3 additions & 1 deletion pkg/cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type listenCmd struct {
forwardConnectURL string
events []string
latestAPIVersion bool
livemode bool
loadFromWebhooksAPI bool
printJSON bool
skipVerify bool
Expand Down Expand Up @@ -66,6 +67,7 @@ to your localhost:
lc.cmd.Flags().StringVarP(&lc.forwardURL, "forward-to", "f", "", "The URL to forward webhook events to")
lc.cmd.Flags().StringVarP(&lc.forwardConnectURL, "forward-connect-to", "c", "", "The URL to forward Connect webhook events to (default: same as normal events)")
lc.cmd.Flags().BoolVarP(&lc.latestAPIVersion, "latest", "l", false, "Receive events formatted with the latest API version (default: your account's default API version)")
lc.cmd.Flags().BoolVar(&lc.livemode, "livemode", false, "Receive live mode events (default: test mode)")
lc.cmd.Flags().BoolVarP(&lc.printJSON, "print-json", "p", false, "Print full JSON objects to stdout")
lc.cmd.Flags().BoolVarP(&lc.loadFromWebhooksAPI, "load-from-webhooks-api", "a", false, "Load webhook endpoint configuration from the webhooks API")
lc.cmd.Flags().BoolVarP(&lc.skipVerify, "skip-verify", "", false, "Skip certificate verification when forwarding to HTTPS endpoints")
Expand All @@ -90,7 +92,7 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error {

endpointRoutes := make([]proxy.EndpointRoute, 0)

key, err := Config.Profile.GetAPIKey(false)
key, err := Config.Profile.GetAPIKey(lc.livemode)
if err != nil {
return err
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/cmd/logs/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type TailCmd struct {
cfg *config.Config
Cmd *cobra.Command
format string
livemode bool
LogFilters *logTailing.LogFilters
noWSS bool
}
Expand Down Expand Up @@ -53,6 +54,13 @@ Acceptable values:
'JSON' - Output logs in JSON format`,
)

tailCmd.Cmd.Flags().BoolVar(
&tailCmd.livemode,
"livemode",
false,
"Tail live mode logs (default: test mode)",
)

// Log filters
tailCmd.Cmd.Flags().StringSliceVar(
&tailCmd.LogFilters.FilterAccount,
Expand Down Expand Up @@ -132,7 +140,7 @@ func (tailCmd *TailCmd) runTailCmd(cmd *cobra.Command, args []string) error {
return err
}

key, err := tailCmd.cfg.Profile.GetAPIKey(false)
key, err := tailCmd.cfg.Profile.GetAPIKey(tailCmd.livemode)
if err != nil {
return err
}
Expand Down
35 changes: 19 additions & 16 deletions pkg/proxy/stripeevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,34 @@ import "fmt"
// stripeEvent is a minimal representation of a Stripe `event` object, used
// to extract the event's ID and type for logging purposes.
type stripeEvent struct {
ID string `json:"id"`
Type string `json:"type"`
Account string `json:"account"`
Account string `json:"account"`
ID string `json:"id"`
Livemode bool `json:"livemode"`
Type string `json:"type"`
}

func (e *stripeEvent) isConnect() bool {
return e.Account != ""
}

func (e *stripeEvent) urlForEventID() string {
url := ""
if e.isConnect() {
url = fmt.Sprintf("https://dashboard.stripe.com/%s/test/events/%s", e.Account, e.ID)
} else {
url = fmt.Sprintf("https://dashboard.stripe.com/test/events/%s", e.ID)
}
return url
return fmt.Sprintf("%s/events/%s", baseDashboardURL(e.Livemode, e.Account), e.ID)
}

func (e *stripeEvent) urlForEventType() string {
url := ""
if e.isConnect() {
url = fmt.Sprintf("https://dashboard.stripe.com/%s/test/events?type=%s", e.Account, e.Type)
} else {
url = fmt.Sprintf("https://dashboard.stripe.com/test/events?type=%s", e.Type)
return fmt.Sprintf("%s/events?type=%s", baseDashboardURL(e.Livemode, e.Account), e.Type)
}

func baseDashboardURL(livemode bool, account string) string {
maybeTest := ""
if !livemode {
maybeTest = "/test"
}
return url

maybeAccount := ""
if account != "" {
maybeAccount = fmt.Sprintf("/%s", account)
}

return fmt.Sprintf("https://dashboard.stripe.com%s%s", maybeAccount, maybeTest)
}
28 changes: 20 additions & 8 deletions pkg/proxy/stripeevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@ func TestIsConnect(t *testing.T) {
}

func TestURLForEventID(t *testing.T) {
evt1 := &stripeEvent{ID: "evt_123", Type: "customer.created"}
require.Equal(t, "https://dashboard.stripe.com/test/events/evt_123", evt1.urlForEventID())
evt := &stripeEvent{ID: "evt_123", Livemode: false, Type: "customer.created"}
require.Equal(t, "https://dashboard.stripe.com/test/events/evt_123", evt.urlForEventID())

evt2 := &stripeEvent{ID: "evt_123", Type: "customer.created", Account: "acct_123"}
require.Equal(t, "https://dashboard.stripe.com/acct_123/test/events/evt_123", evt2.urlForEventID())
evt = &stripeEvent{ID: "evt_123", Livemode: false, Type: "customer.created", Account: "acct_123"}
require.Equal(t, "https://dashboard.stripe.com/acct_123/test/events/evt_123", evt.urlForEventID())

evt = &stripeEvent{ID: "evt_123", Livemode: true, Type: "customer.created"}
require.Equal(t, "https://dashboard.stripe.com/events/evt_123", evt.urlForEventID())

evt = &stripeEvent{ID: "evt_123", Livemode: true, Type: "customer.created", Account: "acct_123"}
require.Equal(t, "https://dashboard.stripe.com/acct_123/events/evt_123", evt.urlForEventID())
}

func TestURLForEventType(t *testing.T) {
evt1 := &stripeEvent{ID: "evt_123", Type: "customer.created"}
require.Equal(t, "https://dashboard.stripe.com/test/events?type=customer.created", evt1.urlForEventType())
evt := &stripeEvent{ID: "evt_123", Livemode: false, Type: "customer.created"}
require.Equal(t, "https://dashboard.stripe.com/test/events?type=customer.created", evt.urlForEventType())

evt2 := &stripeEvent{ID: "evt_123", Type: "customer.created", Account: "acct_123"}
require.Equal(t, "https://dashboard.stripe.com/acct_123/test/events?type=customer.created", evt2.urlForEventType())
evt = &stripeEvent{ID: "evt_123", Livemode: false, Type: "customer.created", Account: "acct_123"}
require.Equal(t, "https://dashboard.stripe.com/acct_123/test/events?type=customer.created", evt.urlForEventType())

evt = &stripeEvent{ID: "evt_123", Livemode: true, Type: "customer.created"}
require.Equal(t, "https://dashboard.stripe.com/events?type=customer.created", evt.urlForEventType())

evt = &stripeEvent{ID: "evt_123", Livemode: true, Type: "customer.created", Account: "acct_123"}
require.Equal(t, "https://dashboard.stripe.com/acct_123/events?type=customer.created", evt.urlForEventType())
}

0 comments on commit 41a7831

Please sign in to comment.