diff --git a/server/internal/command/admin/cmd.go b/server/internal/command/admin/cmd.go new file mode 100644 index 00000000..cfe2552e --- /dev/null +++ b/server/internal/command/admin/cmd.go @@ -0,0 +1,21 @@ +package admin + +import ( + "github.com/shroff/phylum/server/internal/command/admin/schema" + "github.com/shroff/phylum/server/internal/command/admin/user" + "github.com/spf13/cobra" +) + +func SetupCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "admin", + Short: "Admin Commands", + } + cmd.AddCommand([]*cobra.Command{ + user.SetupCommand(), + schema.SetupCommand(), + // storage.SetupCommand(), + }...) + + return cmd +} diff --git a/server/internal/command/schema/cmd.go b/server/internal/command/admin/schema/cmd.go similarity index 79% rename from server/internal/command/schema/cmd.go rename to server/internal/command/admin/schema/cmd.go index c14235a6..48479626 100644 --- a/server/internal/command/schema/cmd.go +++ b/server/internal/command/admin/schema/cmd.go @@ -12,21 +12,20 @@ import ( func SetupCommand() *cobra.Command { cmd := &cobra.Command{ - GroupID: "admin", - Use: "schema", - Short: "Database Schema Management", + Use: "schema", + Short: "Database Schema Management", } cmd.AddCommand([]*cobra.Command{ - setupSchemaMigrateCommand(), - setupSchemaResetCommand(), + setupMigrateCommand(), + setupResetCommand(), }...) return cmd } -func setupSchemaResetCommand() *cobra.Command { +func setupResetCommand() *cobra.Command { return &cobra.Command{ Use: "reset", - Short: "Reset Database Schema", + Short: "Reset Schema", Run: func(cmd *cobra.Command, args []string) { db.Cfg.NoMigrate = true if err := db.DeleteSchema(context.Background()); err != nil { @@ -41,10 +40,10 @@ func setupSchemaResetCommand() *cobra.Command { } } -func setupSchemaMigrateCommand() *cobra.Command { +func setupMigrateCommand() *cobra.Command { return &cobra.Command{ Use: "migrate", - Short: "Migrate Database Schema", + Short: "Migrate Schema", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { ctx := context.Background() diff --git a/server/internal/command/storage/cmd.go b/server/internal/command/admin/storage/cmd.go similarity index 86% rename from server/internal/command/storage/cmd.go rename to server/internal/command/admin/storage/cmd.go index 37fea787..fd616a57 100644 --- a/server/internal/command/storage/cmd.go +++ b/server/internal/command/admin/storage/cmd.go @@ -15,20 +15,19 @@ import ( func SetupCommand() *cobra.Command { cmd := &cobra.Command{ - GroupID: "admin", - Use: "storage", - Short: "Storage Management", + Use: "storage", + Short: "Storage Management", } cmd.AddCommand([]*cobra.Command{ - setupStorageCreateCommand(), - setupStorageListCommand(), + setupCreateCommand(), + setupListCommand(), }...) return cmd } -func setupStorageCreateCommand() *cobra.Command { +func setupCreateCommand() *cobra.Command { return &cobra.Command{ Use: "create name driver", Short: "Create new storage backend", @@ -62,7 +61,7 @@ func setupStorageCreateCommand() *cobra.Command { } } -func setupStorageListCommand() *cobra.Command { +func setupListCommand() *cobra.Command { return &cobra.Command{ Use: "list", Short: "List all storage backends", diff --git a/server/internal/command/admin/user/cmd.go b/server/internal/command/admin/user/cmd.go new file mode 100644 index 00000000..8f3ba93f --- /dev/null +++ b/server/internal/command/admin/user/cmd.go @@ -0,0 +1,41 @@ +package user + +import ( + "context" + + "github.com/shroff/phylum/server/internal/core/user" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func SetupCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "user", + Short: "User Management", + } + cmd.AddCommand([]*cobra.Command{ + setupListCommand(), + setupInviteCommand(), + setupPwresetResetCommand(), + setupModCommand(), + setupPasswdCommand(), + }...) + + return cmd +} + +func setupListCommand() *cobra.Command { + return &cobra.Command{ + Use: "list", + Short: "List Users", + Run: func(cmd *cobra.Command, args []string) { + users, err := user.ManagerFromContext(context.Background()).ListUsers(nil) + if err != nil { + logrus.Fatal(err) + } + for _, user := range users { + logrus.Infof("%24s : %s", user.Email, user.DisplayName) + } + }, + } +} diff --git a/server/internal/command/user/invite.go b/server/internal/command/admin/user/invite.go similarity index 100% rename from server/internal/command/user/invite.go rename to server/internal/command/admin/user/invite.go diff --git a/server/internal/command/user/mod.go b/server/internal/command/admin/user/mod.go similarity index 97% rename from server/internal/command/user/mod.go rename to server/internal/command/admin/user/mod.go index e2a282e5..09031373 100644 --- a/server/internal/command/user/mod.go +++ b/server/internal/command/admin/user/mod.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/cobra" ) -func setupUserModCommand() *cobra.Command { +func setupModCommand() *cobra.Command { cmd := &cobra.Command{ Use: "mod email", Short: "Add User", diff --git a/server/internal/command/user/passwd.go b/server/internal/command/admin/user/passwd.go similarity index 97% rename from server/internal/command/user/passwd.go rename to server/internal/command/admin/user/passwd.go index c6cb9653..e511ab98 100644 --- a/server/internal/command/user/passwd.go +++ b/server/internal/command/admin/user/passwd.go @@ -11,7 +11,7 @@ import ( "golang.org/x/term" ) -func setupUserPasswdCommand() *cobra.Command { +func setupPasswdCommand() *cobra.Command { cmd := &cobra.Command{ Use: "passwd email", Short: "Change User Password", diff --git a/server/internal/command/user/password_reset.go b/server/internal/command/admin/user/password_reset.go similarity index 95% rename from server/internal/command/user/password_reset.go rename to server/internal/command/admin/user/password_reset.go index ce67fa1d..f1652bc6 100644 --- a/server/internal/command/user/password_reset.go +++ b/server/internal/command/admin/user/password_reset.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -func setupPasswordResetCommand() *cobra.Command { +func setupPwresetResetCommand() *cobra.Command { cmd := &cobra.Command{ Use: "pwreset email", Short: "pwreset email", diff --git a/server/internal/command/command.go b/server/internal/command/command.go index 5a052bdb..9e7958e9 100644 --- a/server/internal/command/command.go +++ b/server/internal/command/command.go @@ -14,13 +14,9 @@ import ( "github.com/knadh/koanf/providers/posflag" "github.com/knadh/koanf/providers/rawbytes" "github.com/knadh/koanf/v2" + "github.com/shroff/phylum/server/internal/command/admin" "github.com/shroff/phylum/server/internal/command/fs" - "github.com/shroff/phylum/server/internal/command/publink" - "github.com/shroff/phylum/server/internal/command/schema" "github.com/shroff/phylum/server/internal/command/serve" - storagecmd "github.com/shroff/phylum/server/internal/command/storage" - "github.com/shroff/phylum/server/internal/command/trash" - "github.com/shroff/phylum/server/internal/command/user" "github.com/shroff/phylum/server/internal/core/db" "github.com/shroff/phylum/server/internal/core/storage" "github.com/shroff/phylum/server/internal/mail" @@ -33,8 +29,8 @@ import ( var defaultConfig embed.FS func SetupCommand() { - var rootCmd = &cobra.Command{Use: path.Base(os.Args[0])} - flags := rootCmd.PersistentFlags() + var cmd = &cobra.Command{Use: path.Base(os.Args[0])} + flags := cmd.PersistentFlags() // Flags only. Not part of config file flags.StringP("workdir", "W", "", "Working Directory") @@ -59,7 +55,7 @@ func SetupCommand() { } uuid.EnableRandPool() - rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { + cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { dir, _ := cmd.Flags().GetString("workdir") if dir != "" && dir != "." { logrus.Info("Changing directory to " + dir) @@ -114,31 +110,13 @@ func SetupCommand() { db.Close() }() - rootCmd.AddGroup(&cobra.Group{ - ID: "admin", - Title: "Admin", - }) - rootCmd.AddCommand( - schema.SetupCommand(), - user.SetupCommand(), - storagecmd.SetupCommand(), - ) - - rootCmd.AddGroup(&cobra.Group{ - ID: "app", - Title: "App", - }) - rootCmd.AddCommand( + cmd.AddCommand( + admin.SetupCommand(), fs.SetupCommand(), - publink.SetupCommand(), - trash.SetupCommand(), + serve.SetupCommand(), ) - - rootCmd.AddGroup(&cobra.Group{ - ID: "server", - Title: "Server", - }) - rootCmd.AddCommand(serve.SetupCommand()) - - rootCmd.Execute() + cmd.AddGroup(&cobra.Group{ID: "misc", Title: "Misc"}) + cmd.SetHelpCommandGroupID("misc") + cmd.SetCompletionCommandGroupID("misc") + cmd.Execute() } diff --git a/server/internal/command/fs/cmd.go b/server/internal/command/fs/cmd.go index 6f49c02e..3593467e 100644 --- a/server/internal/command/fs/cmd.go +++ b/server/internal/command/fs/cmd.go @@ -1,17 +1,17 @@ package fs import ( + "github.com/shroff/phylum/server/internal/command/fs/publink" + "github.com/shroff/phylum/server/internal/command/fs/trash" "github.com/spf13/cobra" ) func SetupCommand() *cobra.Command { cmd := &cobra.Command{ - GroupID: "app", - Use: "fs", - Short: "Filesystem", + Use: "fs", + Short: "Filesystem", } - flags := cmd.PersistentFlags() - flags.StringP("user", "u", "", "User") + cmd.PersistentFlags().StringP("user", "u", "", "User") cmd.AddCommand( setupCatCommand(), @@ -21,9 +21,10 @@ func SetupCommand() *cobra.Command { setupLsCommand(), setupMkdirCommand(), setupMvCommand(), - setupPublinksCommand(), setupRmCommand(), setupSearchCommand(), + publink.SetupCommand(), + trash.SetupCommand(), ) return cmd diff --git a/server/internal/command/publink/cmd.go b/server/internal/command/fs/publink/cmd.go similarity index 55% rename from server/internal/command/publink/cmd.go rename to server/internal/command/fs/publink/cmd.go index 45f52a4e..493ebc6a 100644 --- a/server/internal/command/publink/cmd.go +++ b/server/internal/command/fs/publink/cmd.go @@ -6,15 +6,13 @@ import ( func SetupCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "publink", - Short: "Publink Management", - GroupID: "app", + Use: "publink", + Short: "Publink Management", } - flags := cmd.PersistentFlags() - flags.StringP("user", "u", "", "User") cmd.AddCommand( setupCreateCommand(), + setupListCommand(), ) return cmd diff --git a/server/internal/command/publink/create.go b/server/internal/command/fs/publink/create.go similarity index 97% rename from server/internal/command/publink/create.go rename to server/internal/command/fs/publink/create.go index a4fd67f9..00186ecb 100644 --- a/server/internal/command/publink/create.go +++ b/server/internal/command/fs/publink/create.go @@ -13,7 +13,7 @@ import ( func setupCreateCommand() *cobra.Command { cmd := cobra.Command{ - Use: "create ( | )", + Use: "create ", Short: "Create Publink", Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { diff --git a/server/internal/command/fs/publinks.go b/server/internal/command/fs/publink/list.go similarity index 87% rename from server/internal/command/fs/publinks.go rename to server/internal/command/fs/publink/list.go index 66d51bde..c9cfb16d 100644 --- a/server/internal/command/fs/publinks.go +++ b/server/internal/command/fs/publink/list.go @@ -1,4 +1,4 @@ -package fs +package publink import ( "fmt" @@ -9,9 +9,9 @@ import ( "github.com/spf13/cobra" ) -func setupPublinksCommand() *cobra.Command { +func setupListCommand() *cobra.Command { cmd := cobra.Command{ - Use: "publinks ", + Use: "list ", Short: "List Publinks", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { @@ -50,6 +50,5 @@ func setupPublinksCommand() *cobra.Command { } }, } - cmd.Flags().BoolP("parents", "p", false, "Make intermediate parents as needed") return &cmd } diff --git a/server/internal/command/trash/cmd.go b/server/internal/command/fs/trash/cmd.go similarity index 67% rename from server/internal/command/trash/cmd.go rename to server/internal/command/fs/trash/cmd.go index 8cb0a7ec..d951f5ff 100644 --- a/server/internal/command/trash/cmd.go +++ b/server/internal/command/fs/trash/cmd.go @@ -6,12 +6,9 @@ import ( func SetupCommand() *cobra.Command { cmd := &cobra.Command{ - GroupID: "app", - Use: "trash", - Short: "Trash", + Use: "trash", + Short: "Trash", } - flags := cmd.PersistentFlags() - flags.StringP("user", "u", "", "User") cmd.AddCommand( setupDeleteCommand(), diff --git a/server/internal/command/trash/delete.go b/server/internal/command/fs/trash/delete.go similarity index 100% rename from server/internal/command/trash/delete.go rename to server/internal/command/fs/trash/delete.go diff --git a/server/internal/command/trash/empty.go b/server/internal/command/fs/trash/empty.go similarity index 100% rename from server/internal/command/trash/empty.go rename to server/internal/command/fs/trash/empty.go diff --git a/server/internal/command/trash/list.go b/server/internal/command/fs/trash/list.go similarity index 100% rename from server/internal/command/trash/list.go rename to server/internal/command/fs/trash/list.go diff --git a/server/internal/command/trash/restore.go b/server/internal/command/fs/trash/restore.go similarity index 100% rename from server/internal/command/trash/restore.go rename to server/internal/command/fs/trash/restore.go diff --git a/server/internal/command/trash/summary.go b/server/internal/command/fs/trash/summary.go similarity index 100% rename from server/internal/command/trash/summary.go rename to server/internal/command/fs/trash/summary.go diff --git a/server/internal/command/serve/cmd.go b/server/internal/command/serve/cmd.go index 1ca272b3..504cc3bb 100644 --- a/server/internal/command/serve/cmd.go +++ b/server/internal/command/serve/cmd.go @@ -24,9 +24,8 @@ var Cfg Config func SetupCommand() *cobra.Command { var cmd = &cobra.Command{ - Use: "serve", - Short: "Run the server", - GroupID: "server", + Use: "serve", + Short: "Run the server", } flags := cmd.Flags() diff --git a/server/internal/command/user/bookmarks.go b/server/internal/command/user/bookmarks/cmd.go similarity index 85% rename from server/internal/command/user/bookmarks.go rename to server/internal/command/user/bookmarks/cmd.go index 20061d13..568e6a90 100644 --- a/server/internal/command/user/bookmarks.go +++ b/server/internal/command/user/bookmarks/cmd.go @@ -1,4 +1,4 @@ -package user +package bookmarks import ( "context" @@ -10,23 +10,22 @@ import ( "github.com/spf13/cobra" ) -func setupBookmarksCommand() *cobra.Command { +func SetupCommand() *cobra.Command { cmd := &cobra.Command{ Use: "bookmarks", - Short: "Manage User Bookmarks", + Short: "Manage Bookmarks", } - cmd.PersistentFlags().StringP("user", "u", "", "User") cmd.AddCommand( - setupBookmarksListCommand(), - setupBookmarksAddCommand(), - setupBookmarksRemoveCommand(), + setupListCommand(), + setupAddCommand(), + setupRemoveCommand(), ) return cmd } -func setupBookmarksListCommand() *cobra.Command { +func setupListCommand() *cobra.Command { cmd := &cobra.Command{ Use: "list", Short: "List Bookmarks", @@ -50,7 +49,7 @@ func setupBookmarksListCommand() *cobra.Command { return cmd } -func setupBookmarksRemoveCommand() *cobra.Command { +func setupRemoveCommand() *cobra.Command { cmd := &cobra.Command{ Use: "remove ", Short: "Remove Bookmark", @@ -77,7 +76,7 @@ func setupBookmarksRemoveCommand() *cobra.Command { return cmd } -func setupBookmarksAddCommand() *cobra.Command { +func setupAddCommand() *cobra.Command { cmd := &cobra.Command{ Use: "add (path | uuid) [name]", Short: "Add Bookmark", diff --git a/server/internal/command/user/cmd.go b/server/internal/command/user/cmd.go index 35151a8f..5ce8cbac 100644 --- a/server/internal/command/user/cmd.go +++ b/server/internal/command/user/cmd.go @@ -1,43 +1,19 @@ package user import ( - "context" - - "github.com/shroff/phylum/server/internal/core/user" - "github.com/sirupsen/logrus" + "github.com/shroff/phylum/server/internal/command/user/bookmarks" "github.com/spf13/cobra" ) func SetupCommand() *cobra.Command { cmd := &cobra.Command{ - GroupID: "admin", - Use: "user", - Short: "User Management", + Use: "user", + Short: "User Management", } + cmd.PersistentFlags().StringP("user", "u", "", "User") cmd.AddCommand([]*cobra.Command{ - setupInviteCommand(), - setupPasswordResetCommand(), - setupUserModCommand(), - setupUserListCommand(), - setupUserPasswdCommand(), - setupBookmarksCommand(), + bookmarks.SetupCommand(), }...) return cmd } - -func setupUserListCommand() *cobra.Command { - return &cobra.Command{ - Use: "list", - Short: "List Users", - Run: func(cmd *cobra.Command, args []string) { - users, err := user.ManagerFromContext(context.Background()).ListUsers(nil) - if err != nil { - logrus.Fatal(err) - } - for _, user := range users { - logrus.Infof("%24s : %s", user.Email, user.DisplayName) - } - }, - } -}