[server] Restructure commands

This commit is contained in:
Abhishek Shroff
2025-05-27 22:27:03 +05:30
parent 5aae871595
commit 5cda3e76a0
22 changed files with 122 additions and 115 deletions

View File

@@ -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
}

View File

@@ -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()

View File

@@ -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",

View File

@@ -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)
}
},
}
}

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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()
}

View File

@@ -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

View File

@@ -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

View File

@@ -13,7 +13,7 @@ import (
func setupCreateCommand() *cobra.Command {
cmd := cobra.Command{
Use: "create <name> (<path> | <uuid>)",
Use: "create <name> <path>",
Short: "Create Publink",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {

View File

@@ -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 <path>",
Use: "list <path>",
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
}

View File

@@ -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(),

View File

@@ -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()

View File

@@ -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 <path>",
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",

View File

@@ -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)
}
},
}
}