Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/mattn/go-shellwords v1.0.12
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/planetscale/planetscale-go v0.143.0
github.com/planetscale/planetscale-go v0.145.0
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7
github.com/spf13/cobra v1.10.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
github.com/planetscale/planetscale-go v0.143.0 h1:3LeJXrPYIkXdxUcGIK5rhitA44D6HMp9ZxmVTy7ozO0=
github.com/planetscale/planetscale-go v0.143.0/go.mod h1:PheYDHAwF14wfCBak1M0J64AdPW8NUeyvgPgWqe7zpI=
github.com/planetscale/planetscale-go v0.145.0 h1:jdmAzU5sfdBZxVGMQXkT+BBxvOcND7cakCIQc0vdeVg=
github.com/planetscale/planetscale-go v0.145.0/go.mod h1:PheYDHAwF14wfCBak1M0J64AdPW8NUeyvgPgWqe7zpI=
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4 h1:Xv5pj20Rhfty1Tv0OVcidg4ez4PvGrpKvb6rvUwQgDs=
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4/go.mod h1:M52h5IWxAcbdQ1hSZrLAGQC4ZXslxEsK/Wh9nu3wdWs=
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7 h1:aRd6vdE1fyuSI4RVj7oCr8lFmgqXvpnPUmN85VbZCp8=
Expand Down
14 changes: 13 additions & 1 deletion internal/cmd/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
var name string

cmd := &cobra.Command{
Use: "create",
Short: "create a service token for the organization",
Expand All @@ -22,6 +24,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {

req := &planetscale.CreateServiceTokenRequest{
Organization: ch.Config.Organization,
Name: stringPtrOrNil(name),
}

end := ch.Printer.PrintProgress(fmt.Sprintf("Creating service token in org %s", printer.BoldBlue(ch.Config.Organization)))
Expand All @@ -39,9 +42,18 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {

end()

return ch.Printer.PrintResource(toServiceToken(token))
return ch.Printer.PrintResource(toServiceTokenWithSecret(token))
},
}

cmd.Flags().StringVar(&name, "name", "", "optional name for the service token")

return cmd
}

func stringPtrOrNil(s string) *string {
if s == "" {
return nil
}
return &s
}
54 changes: 52 additions & 2 deletions internal/cmd/token/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"testing"
"time"

"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/config"
Expand All @@ -24,12 +25,14 @@ func TestServiceToken_CreateCmd(t *testing.T) {

org := "planetscale"
id := "123456"
createdAt := time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC)

orig := &ps.ServiceToken{ID: id}
orig := &ps.ServiceToken{ID: id, CreatedAt: createdAt}

svc := &mock.ServiceTokenService{
CreateFn: func(ctx context.Context, req *ps.CreateServiceTokenRequest) (*ps.ServiceToken, error) {
c.Assert(req.Organization, qt.Equals, org)
c.Assert(req.Name, qt.IsNil)
return orig, nil
},
}
Expand All @@ -52,6 +55,53 @@ func TestServiceToken_CreateCmd(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(svc.CreateFnInvoked, qt.IsTrue)

res := &ServiceToken{orig: orig}
res := &ServiceTokenWithSecret{orig: orig}
c.Assert(buf.String(), qt.JSONEquals, res)
}

func TestServiceToken_CreateCmdWithName(t *testing.T) {
c := qt.New(t)

var buf bytes.Buffer
format := printer.JSON
p := printer.NewPrinter(&format)
p.SetResourceOutput(&buf)

org := "planetscale"
id := "123456"
name := "my-token"
createdAt := time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC)

orig := &ps.ServiceToken{ID: id, Name: &name, CreatedAt: createdAt}

svc := &mock.ServiceTokenService{
CreateFn: func(ctx context.Context, req *ps.CreateServiceTokenRequest) (*ps.ServiceToken, error) {
c.Assert(req.Organization, qt.Equals, org)
c.Assert(req.Name, qt.IsNotNil)
c.Assert(*req.Name, qt.Equals, name)
return orig, nil
},
}

ch := &cmdutil.Helper{
Printer: p,
Config: &config.Config{
Organization: org,
},
Client: func() (*ps.Client, error) {
return &ps.Client{
ServiceTokens: svc,
}, nil
},
}

cmd := CreateCmd(ch)
cmd.SetArgs([]string{"--name", name})
err := cmd.Execute()

c.Assert(err, qt.IsNil)
c.Assert(svc.CreateFnInvoked, qt.IsTrue)

res := &ServiceTokenWithSecret{orig: orig}
c.Assert(buf.String(), qt.JSONEquals, res)
}
9 changes: 7 additions & 2 deletions internal/cmd/token/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"testing"
"time"

qt "github.com/frankban/quicktest"
"github.com/planetscale/cli/internal/cmdutil"
Expand All @@ -22,10 +23,14 @@ func TestServiceToken_ListCmd(t *testing.T) {
p.SetResourceOutput(&buf)

org := "planetscale"
name1 := "token-one"
createdAt1 := time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC)
lastUsedAt1 := time.Date(2025, 1, 20, 14, 45, 0, 0, time.UTC)
createdAt2 := time.Date(2025, 1, 16, 11, 0, 0, 0, time.UTC)

orig := []*ps.ServiceToken{
{ID: "1"},
{ID: "2"},
{ID: "1", Name: &name1, CreatedAt: createdAt1, LastUsedAt: &lastUsedAt1},
{ID: "2", CreatedAt: createdAt2},
}

svc := &mock.ServiceTokenService{
Expand Down
60 changes: 53 additions & 7 deletions internal/cmd/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ func TokenCmd(ch *cmdutil.Helper) *cobra.Command {
return cmd
}

// ServiceToken returns a table and json serializable schema snapshot.
// ServiceToken returns a table and json serializable service token for listing.
type ServiceToken struct {
ID string `header:"id" json:"id"`
Token string `header:"token" json:"token"`
ID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
LastUsedAt int64 `header:"last_used_at,timestamp(ms|utc|human)" json:"last_used_at"`
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`

orig *ps.ServiceToken
}
Expand All @@ -58,12 +60,56 @@ func (s *ServiceToken) MarshalJSON() ([]byte, error) {
}

// toServiceToken returns a struct that prints out the various fields
// of a schema snapshot model.
// of a service token model.
func toServiceToken(st *ps.ServiceToken) *ServiceToken {
var name string
if st.Name != nil {
name = *st.Name
}

// Avoid using GetMillisecondsIfExists as the table printer
// will not order the columns correctly if lastUsedAt is *int64
var lastUsedAt int64
if st.LastUsedAt != nil {
lastUsedAt = printer.GetMilliseconds(*st.LastUsedAt)
}

return &ServiceToken{
ID: st.ID,
Token: st.Token,
orig: st,
ID: st.ID,
Name: name,
LastUsedAt: lastUsedAt,
CreatedAt: printer.GetMilliseconds(st.CreatedAt),
orig: st,
}
}

// ServiceTokenWithSecret is used for the create response where the token is returned.
type ServiceTokenWithSecret struct {
ID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
Token string `header:"token" json:"token"`
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`

orig *ps.ServiceToken
}

func (s *ServiceTokenWithSecret) MarshalJSON() ([]byte, error) {
return json.MarshalIndent(s.orig, "", " ")
}

// toServiceTokenWithSecret returns a struct that includes the token secret
func toServiceTokenWithSecret(st *ps.ServiceToken) *ServiceTokenWithSecret {
var name string
if st.Name != nil {
name = *st.Name
}

return &ServiceTokenWithSecret{
ID: st.ID,
Name: name,
Token: st.Token,
CreatedAt: printer.GetMilliseconds(st.CreatedAt),
orig: st,
}
}

Expand Down