Skip to content

Commit

Permalink
cmd: Add clients list command
Browse files Browse the repository at this point in the history
Signed-off-by: Shota SAWADA <xiootas@gmail.com>
  • Loading branch information
sawadashota committed Mar 14, 2019
1 parent 0fc875a commit 1aa9d12
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/cli/handler_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,25 @@ func (h *ClientHandler) GetClient(cmd *cobra.Command, args []string) {
checkResponse(err, http.StatusOK, response)
fmt.Println(cmdx.FormatResponse(&cl))
}

func (h *ClientHandler) ListClient(cmd *cobra.Command, args []string) {
m := h.newClientManager(cmd)

limit := flagx.MustGetInt(cmd, "limit")
page := flagx.MustGetInt(cmd, "page")
offset := (limit * page) - limit

cls, response, err := m.ListOAuth2Clients(int64(limit), int64(offset))
checkResponse(err, http.StatusOK, response)

table := newTable()
table.SetHeader([]string{"Client ID", "Name", "Response Type", "Scope"})

data := make([][]string, len(cls))
for i, cl := range cls {
data[i] = []string{cl.ClientId, cl.ClientName, strings.Join(cl.ResponseTypes, ","), cl.Scope}
}

table.AppendBulk(data)
table.Render()
}
16 changes: 16 additions & 0 deletions cmd/cli/handler_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"os"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

hydra "github.com/ory/hydra/sdk/go/hydra/swagger"
Expand Down Expand Up @@ -77,3 +79,17 @@ func formatResponse(response interface{}) string {
cmdx.Must(err, `Command failed because an error ("%s") occurred while prettifying output`, err)
return string(out)
}

// newTable is table renderer at console
// And defines table layout option
//
// https://github.com/olekukonko/tablewriter
func newTable() *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)

// render options
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")

return table
}
19 changes: 19 additions & 0 deletions cmd/clients_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import "github.com/spf13/cobra"

var clientsListCmd = &cobra.Command{
Use: "list",
Short: "List OAuth 2.0 Clients",
Long: `This command list an OAuth 2.0 Clients.
Example:
hydra clients list`,
Run: cmdHandler.Clients.ListClient,
}

func init() {
clientsCmd.AddCommand(clientsListCmd)
clientsListCmd.Flags().Int("limit", 20, "The maximum amount of policies returned.")
clientsListCmd.Flags().Int("page", 1, "The number of page.")
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ require (
github.com/julienschmidt/httprouter v1.2.0
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/lib/pq v1.0.0
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/meatballhat/negroni-logrus v0.0.0-20170801195057-31067281800f
github.com/mendsley/gojwk v0.0.0-20141217222730-4d5ec6e58103
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/oleiade/reflections v1.0.0
github.com/olekukonko/tablewriter v0.0.1
github.com/opentracing/opentracing-go v1.0.2
github.com/ory/dockertest v3.3.2+incompatible
github.com/ory/fosite v0.29.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/markbates/oncer v0.0.0-20181014194634-05fccaae8fc4 h1:Mlji5gkcpzkqTROyE4ZxZ8hN7osunMb2RuGVrbvMvCc=
github.com/markbates/oncer v0.0.0-20181014194634-05fccaae8fc4/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
Expand All @@ -177,6 +179,8 @@ github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJE
github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
github.com/oleiade/reflections v1.0.0 h1:0ir4pc6v8/PJ0yw5AEtMddfXpWBXg9cnG7SgSoJuCgY=
github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w=
github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI=
Expand Down Expand Up @@ -387,6 +391,7 @@ golang.org/x/sys v0.0.0-20181218192612-074acd46bca6/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190226215855-775f8194d0f9 h1:N26gncmS+iqc/W/SKhX3ElI5pkt72XYoRLgi5Z70LSc=
golang.org/x/sys v0.0.0-20190226215855-775f8194d0f9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190305064518-30e92a19ae4a h1:wsSB0WNK6x5F2PxWYOQpGTzp/IH7X8V603VJwSXZUWc=
golang.org/x/sys v0.0.0-20190305064518-30e92a19ae4a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 1aa9d12

Please sign in to comment.