Skip to content

Commit

Permalink
Add support for new API properties [CLI-139] (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket committed Apr 30, 2021
1 parent 0f93aa7 commit 8f52e8f
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 66 deletions.
92 changes: 75 additions & 17 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/url"
"strconv"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
Expand Down Expand Up @@ -35,9 +36,23 @@ var (
Name: "Scopes",
LongForm: "scopes",
ShortForm: "s",
Help: "Comma-separated list of scopes.",
Help: "Comma-separated list of scopes (permissions).",
IsRequired: true,
}
apiTokenLifetime = Flag{
Name: "Token Lifetime",
LongForm: "token-lifetime",
ShortForm: "l",
Help: "The amount of time in seconds that the token will be valid after being issued. Default value is 86400 seconds (1 day).",
AlwaysPrompt: true,
}
apiOfflineAccess = Flag{
Name: "Allow Offline Access",
LongForm: "offline-access",
ShortForm: "o",
Help: "Whether Refresh Tokens can be issued for this API (true) or not (false).",
AlwaysPrompt: true,
}
)

func apisCmd(cli *cli) *cobra.Command {
Expand Down Expand Up @@ -147,9 +162,11 @@ auth0 apis show <id|audience>`,

func createApiCmd(cli *cli) *cobra.Command {
var inputs struct {
Name string
Identifier string
Scopes []string
Name string
Identifier string
Scopes []string
TokenLifetime int
AllowOfflineAccess bool
}

cmd := &cobra.Command{
Expand All @@ -158,8 +175,10 @@ func createApiCmd(cli *cli) *cobra.Command {
Short: "Create a new API",
Long: "Create a new API.",
Example: `auth0 apis create
auth0 apis create --name myapi
auth0 apis create -n myapi --identifier http://my-api`,
auth0 apis create --name myapi
auth0 apis create -n myapi --identifier http://my-api
auth0 apis create -n myapi --token-expiration 6100
auth0 apis create -n myapi -e 6100 --offline-access=true`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
Expand All @@ -176,9 +195,19 @@ auth0 apis create -n myapi --identifier http://my-api`,
return err
}

if err := apiTokenLifetime.Ask(cmd, &inputs.TokenLifetime, auth0.String("86400")); err != nil {
return err
}

if err :=apiOfflineAccess.AskBool(cmd, &inputs.AllowOfflineAccess, nil); err != nil {
return err
}

api := &management.ResourceServer{
Name: &inputs.Name,
Identifier: &inputs.Identifier,
Name: &inputs.Name,
Identifier: &inputs.Identifier,
AllowOfflineAccess: &inputs.AllowOfflineAccess,
TokenLifetime: &inputs.TokenLifetime,
}

if len(inputs.Scopes) > 0 {
Expand All @@ -199,15 +228,19 @@ auth0 apis create -n myapi --identifier http://my-api`,
apiName.RegisterString(cmd, &inputs.Name, "")
apiIdentifier.RegisterString(cmd, &inputs.Identifier, "")
apiScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil)
apiOfflineAccess.RegisterBool(cmd, &inputs.AllowOfflineAccess, false)
apiTokenLifetime.RegisterInt(cmd, &inputs.TokenLifetime, 0)

return cmd
}

func updateApiCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
Name string
Scopes []string
ID string
Name string
Scopes []string
TokenLifetime int
AllowOfflineAccess bool
}

cmd := &cobra.Command{
Expand All @@ -217,7 +250,9 @@ func updateApiCmd(cli *cli) *cobra.Command {
Long: "Update an API.",
Example: `auth0 apis update
auth0 apis update <id|audience>
auth0 apis update <id|audience> --name myapi`,
auth0 apis update <id|audience> --name myapi
auth0 apis update -n myapi --token-expiration 6100
auth0 apis update -n myapi -e 6100 --offline-access=true`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
Expand Down Expand Up @@ -249,7 +284,22 @@ auth0 apis update <id|audience> --name myapi`,
return err
}

api := &management.ResourceServer{}
currentTokenLifetime := strconv.Itoa(auth0.IntValue(current.TokenLifetime))
if err := apiTokenLifetime.AskU(cmd, &inputs.TokenLifetime, &currentTokenLifetime); err != nil {
return err
}

if !cmd.Flags().Changed(apiOfflineAccess.LongForm) {
inputs.AllowOfflineAccess = auth0.BoolValue(current.AllowOfflineAccess)
}

if err := apiOfflineAccess.AskBoolU(cmd, &inputs.AllowOfflineAccess, current.AllowOfflineAccess); err != nil {
return err
}

api := &management.ResourceServer{
AllowOfflineAccess: &inputs.AllowOfflineAccess,
}

if len(inputs.Name) == 0 {
api.Name = current.Name
Expand All @@ -263,6 +313,12 @@ auth0 apis update <id|audience> --name myapi`,
api.Scopes = apiScopesFor(inputs.Scopes)
}

if inputs.TokenLifetime == 0 {
api.TokenLifetime = current.TokenLifetime
} else {
api.TokenLifetime = &inputs.TokenLifetime
}

if err := ansi.Waiting(func() error {
return cli.api.ResourceServer.Update(current.GetID(), api)
}); err != nil {
Expand All @@ -276,6 +332,8 @@ auth0 apis update <id|audience> --name myapi`,

apiName.RegisterStringU(cmd, &inputs.Name, "")
apiScopes.RegisterStringSliceU(cmd, &inputs.Scopes, nil)
apiOfflineAccess.RegisterBoolU(cmd, &inputs.AllowOfflineAccess, false)
apiTokenLifetime.RegisterIntU(cmd, &inputs.TokenLifetime, 0)

return cmd
}
Expand Down Expand Up @@ -332,10 +390,10 @@ func openApiCmd(cli *cli) *cobra.Command {
}

cmd := &cobra.Command{
Use: "open",
Args: cobra.MaximumNArgs(1),
Short: "Open API settings page in Auth0 Manage",
Long: "Open API settings page in Auth0 Manage.",
Use: "open",
Args: cobra.MaximumNArgs(1),
Short: "Open API settings page in Auth0 Manage",
Long: "Open API settings page in Auth0 Manage.",
Example: `auth0 apis open
auth0 apis open <id|audience>`,
PreRun: func(cmd *cobra.Command, args []string) {
Expand Down
16 changes: 10 additions & 6 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func (f *Flag) AskManyU(cmd *cobra.Command, value interface{}, defaultValue *str
return askManyFlag(cmd, f, value, defaultValue, true)
}

func (f *Flag) AskBool(cmd *cobra.Command, value *bool, defaultValue *bool) {
askBoolFlag(cmd, f, value, defaultValue, false)
func (f *Flag) AskBool(cmd *cobra.Command, value *bool, defaultValue *bool) error {
return askBoolFlag(cmd, f, value, defaultValue, false)
}

func (f *Flag) AskBoolU(cmd *cobra.Command, value *bool, defaultValue *bool) {
askBoolFlag(cmd, f, value, defaultValue, true)
func (f *Flag) AskBoolU(cmd *cobra.Command, value *bool, defaultValue *bool) error {
return askBoolFlag(cmd, f, value, defaultValue, true)
}

func (f *Flag) Select(cmd *cobra.Command, value interface{}, options []string, defaultValue *string) error {
Expand Down Expand Up @@ -167,10 +167,14 @@ func askManyFlag(cmd *cobra.Command, f *Flag, value interface{}, defaultValue *s
return nil
}

func askBoolFlag(cmd *cobra.Command, f *Flag, value *bool, defaultValue *bool, isUpdate bool) {
func askBoolFlag(cmd *cobra.Command, f *Flag, value *bool, defaultValue *bool, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
askBool(cmd, f, value, defaultValue)
if err := askBool(cmd, f, value, defaultValue); err != nil {
return err
}
}

return nil
}

func selectFlag(cmd *cobra.Command, f *Flag, value interface{}, options []string, defaultValue *string, isUpdate bool) error {
Expand Down
9 changes: 6 additions & 3 deletions internal/cli/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ func ask(cmd *cobra.Command, i commandInput, value interface{}, defaultValue *st
return nil
}

func askBool(cmd *cobra.Command, i commandInput, value *bool, defaultValue *bool) {
result := prompt.ConfirmDefault(i.GetLabel(), auth0.BoolValue(defaultValue))
*value = result
func askBool(cmd *cobra.Command, i commandInput, value *bool, defaultValue *bool) error {
if err := prompt.AskBool(i.GetLabel(), value, auth0.BoolValue(defaultValue)); err != nil {
return handleInputError(err)
}

return nil
}

func _select(cmd *cobra.Command, i commandInput, value interface{}, options []string, defaultValue *string, isUpdate bool) error {
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,13 @@ auth0 rules update <rule-id> -n "My Updated Rule" --enabled=false`,
return err
}

ruleEnabled.AskBoolU(cmd, &inputs.Enabled, rule.Enabled)
if !cmd.Flags().Changed(ruleEnabled.LongForm) {
inputs.Enabled = auth0.BoolValue(rule.Enabled)
}

if err := ruleEnabled.AskBoolU(cmd, &inputs.Enabled, rule.Enabled); err != nil {
return err
}

// TODO(cyx): we can re-think this once we have
// `--stdin` based commands. For now we don't have
Expand Down
29 changes: 18 additions & 11 deletions internal/display/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package display
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
Expand All @@ -12,20 +13,22 @@ import (
)

type apiView struct {
ID string
Name string
Identifier string
Scopes string
ID string
Name string
Identifier string
Scopes string
TokenLifetime int
OfflineAccess bool

raw interface{}
}

func (v *apiView) AsTableHeader() []string {
return []string{"ID", "Name", "Identifier", "Scopes"}
return []string{}
}

func (v *apiView) AsTableRow() []string {
return []string{ansi.Faint(v.ID), v.Name, v.Identifier, fmt.Sprint(v.Scopes)}
return []string{}
}

func (v *apiView) KeyValues() [][]string {
Expand All @@ -34,6 +37,8 @@ func (v *apiView) KeyValues() [][]string {
{"NAME", v.Name},
{"IDENTIFIER", v.Identifier},
{"SCOPES", v.Scopes},
{"TOKEN LIFETIME", strconv.Itoa(v.TokenLifetime)},
{"ALLOW OFFLINE ACCESS", strconv.FormatBool(v.OfflineAccess)},
}
}

Expand Down Expand Up @@ -107,10 +112,12 @@ func (r *Renderer) ApiUpdate(api *management.ResourceServer) {
func makeApiView(api *management.ResourceServer) (*apiView, bool) {
scopes, scopesTruncated := getScopes(api.Scopes)
view := &apiView{
ID: auth0.StringValue(api.ID),
Name: auth0.StringValue(api.Name),
Identifier: auth0.StringValue(api.Identifier),
Scopes: auth0.StringValue(scopes),
ID: auth0.StringValue(api.ID),
Name: auth0.StringValue(api.Name),
Identifier: auth0.StringValue(api.Identifier),
Scopes: auth0.StringValue(scopes),
TokenLifetime: auth0.IntValue(api.TokenLifetime),
OfflineAccess: auth0.BoolValue(api.AllowOfflineAccess),

raw: api,
}
Expand Down Expand Up @@ -172,7 +179,7 @@ func makeScopeView(scope *management.ResourceServerScope) *scopeView {
func getScopes(scopes []*management.ResourceServerScope) (*string, bool) {
ellipsis := "..."
separator := " "
padding := 16 // the longest apiView key plus two spaces before and after in the label column
padding := 22 // the longest apiView key plus two spaces before and after in the label column
terminalWidth, _, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
terminalWidth = 80
Expand Down
3 changes: 2 additions & 1 deletion internal/display/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func (v *ruleView) AsTableRow() []string {
func (v *ruleView) KeyValues() [][]string {
return [][]string{
[]string{"NAME", v.Name},
[]string{"ID", v.ID},
[]string{"ID", ansi.Faint(v.ID)},
[]string{"ENABLED", strconv.FormatBool(v.Enabled)},
[]string{"ORDER", strconv.Itoa(v.Order)},
[]string{"SCRIPT", v.Script},
}
}
Expand Down
53 changes: 26 additions & 27 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ func askOne(prompt survey.Prompt, response interface{}) error {
return survey.AskOne(prompt, response, stdErrWriter, Icons)
}

func AskBool(message string, value *bool, defaultValue bool) error {
prompt := &survey.Confirm{
Message: message,
Default: defaultValue,
}

if err := askOne(prompt, value); err != nil {
return err
}

return nil
}

func Confirm(message string) bool {
result := false
prompt := &survey.Confirm{
Message: message,
}

if err := askOne(prompt, &result); err != nil {
return false
}

return result
}

func TextInput(name string, message string, help string, defaultValue string, required bool) *survey.Question {
input := &survey.Question{
Name: name,
Expand Down Expand Up @@ -66,30 +92,3 @@ func SelectInput(name string, message string, help string, options []string, def

return input
}

func Confirm(message string) bool {
result := false
prompt := &survey.Confirm{
Message: message,
}

if err := askOne(prompt, &result); err != nil {
return false
}

return result
}

func ConfirmDefault(message string, defaultValue bool) bool {
result := false
prompt := &survey.Confirm{
Message: message,
Default: defaultValue,
}

if err := askOne(prompt, &result); err != nil {
return defaultValue
}

return result
}

0 comments on commit 8f52e8f

Please sign in to comment.