Skip to content

Commit

Permalink
Extract psapi into github.com/planetscale/planetscale-go
Browse files Browse the repository at this point in the history
This PR extracts the PlanetScale Go API client from this repo into
github.com/planetscale/planetscale-go. The repo is already ready for
use, so we also migrate with this PR by removing `psapi` and using
this new package.

This is needed for several reasons:

* Other Go services at PlanetScale also need to interact with the public
  API, hence these services should be able to use the Go API client
  without having to rely on the CLI. The first service to use this new
  API will be https://github.com/planetscale/sql-proxy, which is
  required to fetch databases and
* We eventually want to release an Open-Source Go library for the
  developer community. People will not only use our CLI; they will also
  create various kinds of services, and having an official Go client
  will make it simpler for them to create new tools. Almost all "cloud"
  specific tools are written in Go.

  In addition to this, automation tools like `Terraform` will also need
  a Go client in the future if we want to add Terraform support for
  PlanetScale DB.

A couple of notes that will change our workflow:

* Because the Go package (planetscale-go) is still private, locally
  you'll have to configure Go. By default, Go will try to access the
  default Go module proxy (https:/proxy.golang.org). Because the package
  is private, it'll throw an error (this will only happen if you
  re-download the package). To fix the error, you need to configure Go,
  so it doesn't contact the proxy for our private package. Luckily
  executing a single command can do it:

```
go env -w GOPRIVATE="github.com/planetscale/*"
```

* There is no versioning yet for the `planetscale-go` package. Because
  we're going to change/break things until everything is fleshed out,
  you'll see that the version in `go.mod` is something like:
  `v0.0.0-20210109112707-8edffde9c156`.

* If you want to add a new endpoint or new functionality, you'll have to
  add it to `planetscale-go`. To use them in the CLI, you can download
  and vendor the latest release with:

```
go get github.com/planetscale/planetscale-go
go mod vendor
```
  • Loading branch information
fatih committed Jan 11, 2021
1 parent bddfc03 commit 763ea74
Show file tree
Hide file tree
Showing 14 changed files with 438 additions and 135 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,26 @@ In order to get setup and running with this project, you can run `script/setup`
You can install and use the program using `script/build` or `go install ./...`. After doing so, you can run the tool using `psctl <command> [subcommand] [flags]`.



For using the CLI locally, there's some extra setup involved to get authenticated.

### Updating the vendored API package


To update the vendored `github.com/planetscale/planetscale-go` API package,
first make sure to set up Go for private repositories:

```
go env -w GOPRIVATE="github.com/planetscale/*"
```

Then, use the following commands to fetch the `latest` HEAD version of the CLI:

```
go get github.com/planetscale/planetscale-go
go mod vendor
go mod tidy
```

#### For logging in:
1. Create a Rails OAuth application at `http://auth.planetscaledb.local:3000/oauth/applications/new`. Make sure the `Confidential` box is unchecked and that the scopes are `read_database write_database`. You can make the `Redirect URI` be anything.
2. After creating this application, keep note of the client ID.
Expand Down
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path"

"github.com/mitchellh/go-homedir"
"github.com/planetscale/cli/psapi"
ps "github.com/planetscale/planetscale-go"
)

const (
Expand Down Expand Up @@ -36,7 +36,7 @@ func New() *Config {

return &Config{
AccessToken: string(accessToken),
BaseURL: psapi.DefaultBaseURL,
BaseURL: ps.DefaultBaseURL,
}
}

Expand All @@ -56,8 +56,8 @@ func AccessTokenPath() string {
}

// NewClientFromConfig creates a PlaentScale API client from our configuration
func (c *Config) NewClientFromConfig(opts ...psapi.ClientOption) (*psapi.Client, error) {
args := []psapi.ClientOption{psapi.SetBaseURL(c.BaseURL)}
func (c *Config) NewClientFromConfig(opts ...ps.ClientOption) (*ps.Client, error) {
args := []ps.ClientOption{ps.SetBaseURL(c.BaseURL)}
args = append(args, opts...)
return psapi.NewClientFromToken(c.AccessToken, args...)
return ps.NewClientFromToken(c.AccessToken, args...)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/browser v0.0.0-20201112035734-206646e67786
github.com/pkg/errors v0.9.1
github.com/planetscale/planetscale-go v0.0.0-20210109112707-8edffde9c156
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.6.1
golang.org/x/oauth2 v0.0.0-20201203001011-0b49973bad19
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/planetscale/planetscale-go v0.0.0-20210109112707-8edffde9c156 h1:HAAXn5h2s2+mRR8xY7K0PRHPzev8SoYWxUorGaQRxBc=
github.com/planetscale/planetscale-go v0.0.0-20210109112707-8edffde9c156/go.mod h1:Q5/JMffdDyOcW21MXiCDI9Pf1H/qPQi3tl1JLBZzzJs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down Expand Up @@ -369,8 +371,8 @@ golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4Iltr
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20201203001011-0b49973bad19 h1:ZD+2Sd/BnevwJp8PSli8WgGAGzb9IZtxBsv1iZMYeEA=
golang.org/x/oauth2 v0.0.0-20201203001011-0b49973bad19/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5 h1:Lm4OryKCca1vehdsWogr9N4t7NfZxLbJoc/H0w4K4S4=
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/database/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (

"github.com/pkg/browser"
"github.com/planetscale/cli/config"
"github.com/planetscale/cli/psapi"
ps "github.com/planetscale/planetscale-go"

"github.com/spf13/cobra"
)

// CreateCmd is the command for creating a database.
func CreateCmd(cfg *config.Config) *cobra.Command {
createReq := &psapi.CreateDatabaseRequest{
Database: new(psapi.Database),
createReq := &ps.CreateDatabaseRequest{
Database: new(ps.Database),
}
cmd := &cobra.Command{
Use: "create",
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/planetscale/cli/config"
"github.com/planetscale/cli/pkg/cmd/auth"
"github.com/planetscale/cli/pkg/cmd/database"
"github.com/planetscale/cli/psapi"
ps "github.com/planetscale/planetscale-go"
"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -68,7 +68,7 @@ func init() {

cfg := config.New()

rootCmd.PersistentFlags().StringVar(&cfg.BaseURL, "api-url", psapi.DefaultBaseURL, "The base URL for the PlanetScale API.")
rootCmd.PersistentFlags().StringVar(&cfg.BaseURL, "api-url", ps.DefaultBaseURL, "The base URL for the PlanetScale API.")
rootCmd.PersistentFlags().StringVar(&cfg.AccessToken, "api-token", cfg.AccessToken, "The API token to use for authenticating against the PlanetScale API.")
rootCmd.AddCommand(auth.AuthCmd(cfg))
rootCmd.AddCommand(database.DatabaseCmd(cfg))
Expand Down
113 changes: 0 additions & 113 deletions psapi/client_test.go

This file was deleted.

10 changes: 10 additions & 0 deletions vendor/github.com/planetscale/planetscale-go/README.md

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

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

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

10 changes: 10 additions & 0 deletions vendor/github.com/planetscale/planetscale-go/go.mod

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

Loading

0 comments on commit 763ea74

Please sign in to comment.