Refactor CLI functions; unify func signature

This commit is contained in:
Denis Arh
2019-04-04 00:11:29 +02:00
parent 5bdee34903
commit 14452b0ea5
5 changed files with 49 additions and 46 deletions
+12 -14
View File
@@ -9,26 +9,24 @@ import (
"github.com/crusttech/crust/internal/settings"
"github.com/crusttech/crust/system/internal/repository"
"github.com/crusttech/crust/system/internal/service"
)
func Init(ctx context.Context) {
// Main command.
rootCmd := &cobra.Command{Use: "system-cli"}
db := repository.DB(ctx)
var (
db = repository.DB(ctx)
settingsService = settings.NewService(settings.NewRepository(db, "sys_settings"))
settingsService := settings.NewService(settings.NewRepository(db, "sys_settings"))
cmd = &cobra.Command{Use: "system-cli"}
)
Settings(rootCmd, settingsService)
cmd.AddCommand(
settingsCmd(ctx, settingsService),
externalAuthCmd(ctx, settingsService),
usersCmd(ctx, db),
rolesCmd(ctx, db),
)
ExternalAuth(ctx, rootCmd, settingsService)
users(ctx, rootCmd, service.DefaultUser)
roles(ctx, rootCmd, db)
err := rootCmd.Execute()
if err != nil {
if err := cmd.Execute(); err != nil {
fmt.Println(err)
}
}
+9 -9
View File
@@ -10,8 +10,13 @@ import (
)
// Will perform OpenID connect auto-configuration
func ExternalAuth(ctx context.Context, rootCmd *cobra.Command, settingsService settings.Service) {
autoDiscover := &cobra.Command{
func externalAuthCmd(ctx context.Context, settingsService settings.Service) *cobra.Command {
cmd := &cobra.Command{
Use: "external-auth",
Short: "External authentication",
}
autoDiscoverCmd := &cobra.Command{
Use: "auto-discovery [name] [url]",
Short: "Auto discovers new OIDC client",
Args: cobra.ExactArgs(2),
@@ -30,12 +35,7 @@ func ExternalAuth(ctx context.Context, rootCmd *cobra.Command, settingsService s
},
}
settingsCmd := &cobra.Command{
Use: "external-auth",
Short: "External authentication",
}
cmd.AddCommand(autoDiscoverCmd)
settingsCmd.AddCommand(autoDiscover)
rootCmd.AddCommand(settingsCmd)
return cmd
}
+12 -12
View File
@@ -15,31 +15,31 @@ import (
"github.com/crusttech/crust/system/types"
)
func roles(ctx context.Context, rootCmd *cobra.Command, db *factory.DB) {
func rolesCmd(ctx context.Context, db *factory.DB) *cobra.Command {
cmd := &cobra.Command{
Use: "roles",
Short: "Role management",
}
resetCmd := &cobra.Command{
Use: "reset",
Short: "Reset roles",
Run: rolesReset(ctx, db),
Run: rolesResetCmd(ctx, db),
}
addUserCmd := &cobra.Command{
Use: "useradd [role-ID-or-name-or-handle] [user-ID-or-email]",
Short: "Add user to role",
Args: cobra.ExactArgs(2),
Run: rolesUserAdd(ctx, db),
Run: rolesUserAddCmd(ctx, db),
}
// Role management commands.
var cmdRole = &cobra.Command{
Use: "roles",
Short: "Role management",
}
cmd.AddCommand(resetCmd, addUserCmd)
cmdRole.AddCommand(resetCmd, addUserCmd)
rootCmd.AddCommand(cmdRole)
return cmd
}
func rolesReset(ctx context.Context, db *factory.DB) func(cmd *cobra.Command, args []string) {
func rolesResetCmd(ctx context.Context, db *factory.DB) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
var (
err error
@@ -158,7 +158,7 @@ func rolesReset(ctx context.Context, db *factory.DB) func(cmd *cobra.Command, ar
}
}
func rolesUserAdd(ctx context.Context, db *factory.DB) func(cmd *cobra.Command, args []string) {
func rolesUserAddCmd(ctx context.Context, db *factory.DB) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
// Create role and user repository.
var (
+6 -4
View File
@@ -1,13 +1,15 @@
package cli
import (
"context"
"github.com/spf13/cobra"
"github.com/crusttech/crust/internal/settings"
)
func Settings(rootCmd *cobra.Command, service settings.Service) {
settingsCmd := &cobra.Command{
func settingsCmd(ctx context.Context, service settings.Service) *cobra.Command {
cmd := &cobra.Command{
Use: "settings",
Short: "Settings management",
}
@@ -71,7 +73,7 @@ func Settings(rootCmd *cobra.Command, service settings.Service) {
},
}
settingsCmd.AddCommand(list, get, set, del)
cmd.AddCommand(list, get, set, del)
rootCmd.AddCommand(settingsCmd)
return cmd
}
+10 -7
View File
@@ -6,29 +6,30 @@ import (
"os"
"github.com/spf13/cobra"
"github.com/titpetric/factory"
"github.com/crusttech/crust/system/internal/service"
"github.com/crusttech/crust/system/internal/repository"
"github.com/crusttech/crust/system/types"
)
func users(ctx context.Context, rootCmd *cobra.Command, userService service.UserService) {
func usersCmd(ctx context.Context, db *factory.DB) *cobra.Command {
// User management commands.
var cmdUsers = &cobra.Command{
cmd := &cobra.Command{
Use: "users",
Short: "User management",
}
rootCmd.AddCommand(cmdUsers)
// List users.
var cmdUsersList = &cobra.Command{
cmdUsersList := &cobra.Command{
Use: "list",
Short: "List users",
Run: func(cmd *cobra.Command, args []string) {
userRepo := repository.User(ctx, db)
uf := &types.UserFilter{
OrderBy: "updated_at",
}
users, err := userService.With(ctx).Find(uf)
users, err := userRepo.Find(uf)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
@@ -52,6 +53,8 @@ func users(ctx context.Context, rootCmd *cobra.Command, userService service.User
}
},
}
cmdUsers.AddCommand(cmdUsersList)
cmd.AddCommand(cmdUsersList)
return cmd
}