Better error/exit handling in CLI
This commit is contained in:
parent
8e98ba4cb0
commit
22724b11cf
@ -9,6 +9,7 @@ import (
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/internal/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli"
|
||||
"github.com/cortezaproject/corteza-server/system/internal/auth/external"
|
||||
"github.com/cortezaproject/corteza-server/system/internal/repository"
|
||||
"github.com/cortezaproject/corteza-server/system/internal/service"
|
||||
@ -30,13 +31,13 @@ func Auth(ctx context.Context) *cobra.Command {
|
||||
var name, url = args[0], args[1]
|
||||
|
||||
if eas, err := external.ExternalAuthSettings(service.DefaultIntSettings); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else if eap, err := external.RegisterNewOpenIdClient(ctx, eas, name, url); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else if vv, err := eap.MakeValueSet("openid-connect." + name); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else if err := service.DefaultIntSettings.BulkSet(vv); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
@ -74,7 +75,7 @@ func Auth(ctx context.Context) *cobra.Command {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
user.SetRoles(rr.IDs())
|
||||
@ -94,14 +95,10 @@ func Auth(ctx context.Context) *cobra.Command {
|
||||
)
|
||||
|
||||
err = ntf.EmailConfirmation("en", args[0], "notification-testing-token")
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
cli.HandleError(err)
|
||||
|
||||
err = ntf.PasswordReset("en", args[0], "notification-testing-token")
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
cli.HandleError(err)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func exit(err error) {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
@ -8,6 +8,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli"
|
||||
"github.com/cortezaproject/corteza-server/system/internal/repository"
|
||||
"github.com/cortezaproject/corteza-server/system/types"
|
||||
)
|
||||
@ -42,11 +43,11 @@ func Roles(ctx context.Context) *cobra.Command {
|
||||
|
||||
// Try to find role by name and by ID
|
||||
if rr, err = roleRepo.Find(&types.RoleFilter{Query: roleStr}); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else if len(rr) == 1 {
|
||||
role = rr[0]
|
||||
} else if len(rr) > 1 {
|
||||
exit(errors.Errorf("too many roles found with name %q", roleStr))
|
||||
cli.HandleError(errors.Errorf("too many roles found with name %q", roleStr))
|
||||
} else if role == nil {
|
||||
if ID, err = strconv.ParseUint(roleStr, 10, 64); err != nil {
|
||||
// Could not parse ID out of role string
|
||||
@ -57,18 +58,18 @@ func Roles(ctx context.Context) *cobra.Command {
|
||||
}
|
||||
|
||||
if user, err = userRepo.FindByEmail(userStr); repository.ErrUserNotFound.Eq(err) {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else if user == nil || user.ID == 0 {
|
||||
if ID, err = strconv.ParseUint(userStr, 10, 64); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else if user, err = userRepo.FindByID(ID); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add user to role.
|
||||
if err = roleRepo.MemberAddByID(role.ID, user.ID); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
cmd.Printf("Added user [%d] %q to [%d] %q role\n", user.ID, user.Email, role.ID, role.Name)
|
||||
|
||||
@ -7,9 +7,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/titpetric/factory"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/internal/rand"
|
||||
"github.com/cortezaproject/corteza-server/internal/settings"
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli"
|
||||
"github.com/cortezaproject/corteza-server/system/internal/service"
|
||||
)
|
||||
|
||||
@ -71,9 +73,11 @@ func Settings(ctx context.Context) *cobra.Command {
|
||||
Use: "list",
|
||||
Short: "List all",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
factory.Database.MustGet("system").Profiler = nil
|
||||
|
||||
prefix := cmd.Flags().Lookup("prefix").Value.String()
|
||||
if kv, err := service.DefaultIntSettings.FindByPrefix(prefix); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else {
|
||||
for _, v := range kv {
|
||||
cmd.Printf("%s\t%v\n", v.Name, v.Value)
|
||||
@ -91,16 +95,15 @@ func Settings(ctx context.Context) *cobra.Command {
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if v, err := service.DefaultIntSettings.Get(args[0], 0); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
} else if v != nil {
|
||||
cmd.Printf("%v\n", v.Value)
|
||||
}
|
||||
exit(nil)
|
||||
},
|
||||
}
|
||||
|
||||
set := &cobra.Command{
|
||||
Use: "set [key to set] [value",
|
||||
Use: "set [key to set] [value]",
|
||||
Short: "Set value (raw JSON) for a specific key",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
@ -110,10 +113,10 @@ func Settings(ctx context.Context) *cobra.Command {
|
||||
}
|
||||
|
||||
if err := v.SetValueAsString(value); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
exit(service.DefaultIntSettings.Set(v))
|
||||
cli.HandleError(service.DefaultIntSettings.Set(v))
|
||||
},
|
||||
}
|
||||
|
||||
@ -122,7 +125,7 @@ func Settings(ctx context.Context) *cobra.Command {
|
||||
Short: "Set value (raw JSON) for a specific key",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
exit(service.DefaultIntSettings.Delete(args[0], 0))
|
||||
cli.HandleError(service.DefaultIntSettings.Delete(args[0], 0))
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ import (
|
||||
"github.com/titpetric/factory"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/cli"
|
||||
"github.com/cortezaproject/corteza-server/system/internal/repository"
|
||||
"github.com/cortezaproject/corteza-server/system/internal/service"
|
||||
"github.com/cortezaproject/corteza-server/system/types"
|
||||
@ -37,7 +38,7 @@ func Users(ctx context.Context) *cobra.Command {
|
||||
|
||||
users, err := userRepo.Find(uf)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
fmt.Println(" Created Updated EmailAddress")
|
||||
@ -78,23 +79,23 @@ func Users(ctx context.Context) *cobra.Command {
|
||||
)
|
||||
|
||||
if user, err = userRepo.Create(user); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
cmd.Printf("User created [%d].\n", user.ID)
|
||||
|
||||
cmd.Print("Set password: ")
|
||||
if password, err = terminal.ReadPassword(syscall.Stdin); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
if len(password) == 0 {
|
||||
// Password not set, that's ok too.
|
||||
exit(nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = authSvc.SetPassword(user.ID, string(password)); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
@ -116,21 +117,21 @@ func Users(ctx context.Context) *cobra.Command {
|
||||
)
|
||||
|
||||
if user, err = userRepo.FindByEmail(args[0]); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
cmd.Print("Set password: ")
|
||||
if password, err = terminal.ReadPassword(syscall.Stdin); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
if len(password) == 0 {
|
||||
// Password not set, that's ok too.
|
||||
exit(nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err = authSvc.SetPassword(user.ID, string(password)); err != nil {
|
||||
exit(err)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user