diff --git a/system/cli/cli.go b/system/cli/cli.go index b43f74426..659ce37a9 100644 --- a/system/cli/cli.go +++ b/system/cli/cli.go @@ -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) } } diff --git a/system/cli/external-auth.go b/system/cli/external-auth.go index 9dce40f5b..8014a20e9 100644 --- a/system/cli/external-auth.go +++ b/system/cli/external-auth.go @@ -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 } diff --git a/system/cli/roles.go b/system/cli/roles.go index 8270caa68..e5a69a0e6 100644 --- a/system/cli/roles.go +++ b/system/cli/roles.go @@ -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 ( diff --git a/system/cli/settings.go b/system/cli/settings.go index f2e9ec40a..7327ce0a8 100644 --- a/system/cli/settings.go +++ b/system/cli/settings.go @@ -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 } diff --git a/system/cli/users.go b/system/cli/users.go index 4fe52a6ae..7074480c6 100644 --- a/system/cli/users.go +++ b/system/cli/users.go @@ -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 }