Skip to content

Commit db870a5

Browse files
Andrew Farriesroboquat
Andrew Farries
authored andcommitted
Make client init take a config struct not a path
* Rename the function to `New`. * Lift config file unmarshaling out of the package and into the consumer.
1 parent 136d46e commit db870a5

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

components/usage/cmd/run.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
package cmd
66

77
import (
8+
"encoding/json"
9+
"net"
10+
"os"
11+
"time"
12+
813
"github.com/gitpod-io/gitpod/common-go/baseserver"
914
"github.com/gitpod-io/gitpod/common-go/log"
1015
"github.com/gitpod-io/gitpod/usage/pkg/controller"
1116
"github.com/gitpod-io/gitpod/usage/pkg/db"
1217
"github.com/gitpod-io/gitpod/usage/pkg/stripe"
1318
"github.com/spf13/cobra"
14-
"net"
15-
"os"
16-
"time"
1719
)
1820

1921
func init() {
@@ -47,9 +49,20 @@ func run() *cobra.Command {
4749
var billingController controller.BillingController = &controller.NoOpBillingController{}
4850

4951
if apiKeyFile != "" {
50-
c, err := stripe.Authenticate(apiKeyFile)
52+
bytes, err := os.ReadFile(apiKeyFile)
53+
if err != nil {
54+
log.WithError(err).Fatal("Failed to read Stripe API keys.")
55+
}
56+
57+
var config stripe.ClientConfig
58+
err = json.Unmarshal(bytes, &config)
59+
if err != nil {
60+
log.WithError(err).Fatal("Failed to unmarshal Stripe API keys.")
61+
}
62+
63+
c, err := stripe.New(config)
5164
if err != nil {
52-
log.WithError(err).Fatal("Failed to initialize stripe client.")
65+
log.WithError(err).Fatal("Failed to initialize Stripe client.")
5366
}
5467
billingController = controller.NewStripeBillingController(c)
5568
}

components/usage/pkg/stripe/stripe.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
package stripe
66

77
import (
8-
"encoding/json"
98
"fmt"
109
"math"
11-
"os"
1210
"strings"
1311

1412
"github.com/gitpod-io/gitpod/common-go/log"
@@ -20,27 +18,14 @@ type Client struct {
2018
sc *client.API
2119
}
2220

23-
type stripeKeys struct {
21+
type ClientConfig struct {
2422
PublishableKey string `json:"publishableKey"`
2523
SecretKey string `json:"secretKey"`
2624
}
2725

28-
// Authenticate authenticates the Stripe client using a provided file containing a Stripe secret key.
29-
func Authenticate(apiKeyFile string) (*Client, error) {
30-
bytes, err := os.ReadFile(apiKeyFile)
31-
if err != nil {
32-
return nil, err
33-
}
34-
35-
var stripeKeys stripeKeys
36-
err = json.Unmarshal(bytes, &stripeKeys)
37-
if err != nil {
38-
return nil, err
39-
}
40-
41-
sc := &client.API{}
42-
sc.Init(stripeKeys.SecretKey, nil)
43-
return &Client{sc: sc}, nil
26+
// New authenticates a Stripe client using the provided config
27+
func New(config ClientConfig) (*Client, error) {
28+
return &Client{sc: client.New(config.SecretKey, nil)}, nil
4429
}
4530

4631
// UpdateUsage updates teams' Stripe subscriptions with usage data

0 commit comments

Comments
 (0)