Skip to content

Commit

Permalink
Re-shape everything to use the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx committed Jan 26, 2021
1 parent 14507cd commit aa58a15
Show file tree
Hide file tree
Showing 49 changed files with 5,195 additions and 69 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env make

generate:
go generate ./...
.PHONY: generate

test:
CGO_ENABLED=1 go test -race ./... -count 1
.PHONY: test
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/briandowns/spinner v1.11.1
github.com/fatih/color v1.9.0 // indirect
github.com/golang/mock v1.4.4
github.com/google/go-cmp v0.5.4
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/olekukonko/tablewriter v0.0.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down
6 changes: 6 additions & 0 deletions internal/ansi/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func Spinner(text string, fn func() error) error {
if err != nil {
s.FinalMSG = s.Prefix + spinnerTextFailed
}

// FIXME(cyx): this is causing a race condition. The problem is
// with our dependency on briandowns/spinner. For now adding an
// artificial sleep removes the race condition.
time.Sleep(time.Microsecond)

s.Stop()
}()

Expand Down
3 changes: 2 additions & 1 deletion internal/auth0/actions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//go:generate mockgen -source=actions.go -destination=actions_mock.go -package=auth0
package auth0

import "gopkg.in/auth0.v5/management"

type ActionsAPI interface {
type ActionAPI interface {
Create(a *management.Action) error
Read(id string) (*management.Action, error)
Update(id string, a *management.Action) error
Expand Down
115 changes: 115 additions & 0 deletions internal/auth0/actions_mock.go

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

49 changes: 40 additions & 9 deletions internal/auth0/auth0.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
package auth0

// API mimics `management.Management`s general interface, except it uses
// methods since we can't really mock fields.
type API interface {
Actions() ActionsAPI
Client() ClientAPI
Connection() ConnectionAPI
Log() LogAPI
Rule() RuleAPI
ResourceServer() ResourceServerAPI
import (
"gopkg.in/auth0.v5"
"gopkg.in/auth0.v5/management"
)

// API mimics `management.Management`s general interface, except it refers to
// the interfaces instead of the concrete structs.
type API struct {
Action ActionAPI
Client ClientAPI
Connection ConnectionAPI
Log LogAPI
Rule RuleAPI
ResourceServer ResourceServerAPI
}

func NewAPI(m *management.Management) *API {
return &API{
Action: m.Action,
Client: m.Client,
Connection: m.Connection,
Log: m.Log,
ResourceServer: m.ResourceServer,
Rule: m.Rule,
}
}

// Alias all the helper methods so we can keep just typing `auth0.Bool` and the
// compiler can autocomplete our internal package.
var (
Bool = auth0.Bool
BoolValue = auth0.BoolValue
String = auth0.String
StringValue = auth0.StringValue
Int = auth0.Int
IntValue = auth0.IntValue
Float64 = auth0.Float64
Float64Value = auth0.Float64Value
Time = auth0.Time
TimeValue = auth0.TimeValue
)
37 changes: 0 additions & 37 deletions internal/auth0/shim.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cli

import (
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/validators"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5"
"gopkg.in/auth0.v5/management"
)

Expand Down
6 changes: 3 additions & 3 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ auth0 apis create --name myapi --identifier http://my-api
return nil
},
}

cmd.Flags().StringVarP(&flags.name, "name", "n", "", "Name of the API.")
cmd.Flags().StringVarP(&flags.identifier, "identifier", "i", "", "Identifier of the API.")

Expand All @@ -107,7 +107,7 @@ auth0 apis update --id id --name myapi
`,
RunE: func(cmd *cobra.Command, args []string) error {
api := &management.ResourceServer{Name: &flags.name}
manager := &management.ResourceServerManager{Management: cli.api}
manager := cli.api.ResourceServer

err := ansi.Spinner("Updating API", func() error {
return manager.Update(flags.id, api)
Expand Down Expand Up @@ -143,7 +143,7 @@ func deleteApiCmd(cli *cli) *cobra.Command {
auth0 apis delete --id id
`,
RunE: func(cmd *cobra.Command, args []string) error {
manager := &management.ResourceServerManager{Management: cli.api}
manager := cli.api.ResourceServer

err := ansi.Spinner("Deleting API", func() error {
return manager.Delete(flags.id)
Expand Down
11 changes: 9 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"time"

"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/display"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
Expand Down Expand Up @@ -49,7 +50,8 @@ var errUnauthenticated = errors.New("Not yet configured. Try `auth0 login`.")
//
type cli struct {
// core primitives exposed to command builders.
api *management.Management
// api *management.Management
api *auth0.API
renderer *display.Renderer

// set of flags which are user specified.
Expand Down Expand Up @@ -92,9 +94,14 @@ func (c *cli) setup() error {
return errUnauthenticated

} else if t.AccessToken != "" {
c.api, err = management.New(t.Domain,
m, err := management.New(t.Domain,
management.WithStaticToken(t.AccessToken),
management.WithDebug(c.verbose))
if err != nil {
return err
}

c.api = auth0.NewAPI(m)
}

return err
Expand Down
44 changes: 44 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cli

import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/olekukonko/tablewriter"
)

// TODO(cyx): think about whether we should extract this function in the
// `display` package. For now duplication might be better and less premature.
func expectTable(t testing.TB, got string, header []string, data [][]string) {
w := &bytes.Buffer{}

tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader(header)

table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)

for _, v := range data {
table.Append(v)
}

table.Render()
fmt.Fprint(w, tableString.String())

want := w.String()

if got != want {
t.Fatal(cmp.Diff(want, got))
}
}
2 changes: 1 addition & 1 deletion internal/cli/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5"
"gopkg.in/auth0.v5/management"
)

Expand Down
Loading

0 comments on commit aa58a15

Please sign in to comment.