Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
cmd/tier: accept a pricing.json from a URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bmizerany committed Jan 10, 2023
1 parent 836ab6a commit a3edfc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
9 changes: 6 additions & 3 deletions cmd/tier/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ Print the version of the Tier CLI.

"push": `Usage:
tier [--live] push <filename | - >
tier [--live] push <filename | url | - >
Tier push pushes the pricing JSON in the provided filename to Stripe. If the
filename is ("-") then stdin is read.
"tier push" pushes pricing JSON to Stripe. The data may come from a file, url,
or stdin. If a URL is specified, push will use the response body from a GET
request as pricing JSON, if the status code is of the 2XX variety and the body
is valid pricing JSON. If the filename is ("-") then the pricing JSON is read
from stdin.
To learn more about how this works, please visit: https://tier.run/docs/cli/push
Expand Down
15 changes: 15 additions & 0 deletions cmd/tier/tier.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,21 @@ func fileOrStdin(fname string) (io.ReadCloser, error) {
if fname == "-" {
return io.NopCloser(stdin), nil
}
u, err := url.Parse(fname)
if err == nil {
// if no http then bail
if u.Scheme != "http" && u.Scheme != "https" {
return nil, fmt.Errorf("URL scheme must be http or https. Got %q", u.Scheme)
}
res, err := http.Get(fname)
if err != nil {
return nil, err
}
if res.StatusCode/100 != 2 {
return nil, fmt.Errorf("http error fetching pricing.json: %s", res.Status)
}
return res.Body, nil
}
return os.Open(fname)
}

Expand Down
11 changes: 10 additions & 1 deletion cmd/tier/tier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ func TestSwitchPreallocateTask(t *testing.T) {
}

func TestPushStdin(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/pricing.json" {
// be remote pricing.json
io.WriteString(w, `{}`)
return
}

// Be Stripe
io.WriteString(w, `{"id": "price_123"}`)
}))

Expand All @@ -226,6 +233,8 @@ func TestPushStdin(t *testing.T) {
{"{", "-", "unexpected EOF", false},

{"{}", "-", "", true},

{"", s.URL + "/pricing.json", "^$", true},
}

for _, c := range cases {
Expand Down

0 comments on commit a3edfc1

Please sign in to comment.