Files
phylum/server/internal/command/command.go
2025-05-15 08:59:29 +05:30

126 lines
3.4 KiB
Go

package command
import (
"errors"
"os"
"path"
"strings"
"github.com/google/uuid"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
"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/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func SetupCommand() {
viper.SetEnvPrefix("phylum")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
var rootCmd = &cobra.Command{Use: path.Base(os.Args[0])}
flags := rootCmd.PersistentFlags()
// Flags only. Not part of config file
flags.StringP("workdir", "W", "", "Set working directory")
flags.StringP("config-file", "c", "config.yml", "Set working directory")
flags.Bool("debug", false, "Debug mode")
flags.MarkHidden("debug")
viper.BindPFlag("debug", flags.Lookup("debug"))
flags.Bool("trace-queries", false, "Trace Database Queries")
flags.MarkHidden("trace-queries")
viper.SetDefault("db.host", "localhost")
viper.SetDefault("db.port", "5432")
viper.SetDefault("db.name", "phylum")
viper.SetDefault("db.user", "phylum")
viper.SetDefault("db.trace_queries", "false")
viper.SetDefault("db.skip_migration", "false")
viper.SetDefault("storage.location", "storage")
k := koanf.New(".")
k.Load(structs.Provider(defaultConfig, "koanf"), nil)
uuid.EnableRandPool()
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
dir, _ := cmd.Flags().GetString("workdir")
if dir != "" && dir != "." {
logrus.Info("Changing directory to " + dir)
os.Chdir(dir)
}
configFile, _ := cmd.Flags().GetString("config-file")
if configFile != "" {
if err := k.Load(file.Provider(configFile), yaml.Parser()); err != nil {
if errors.Is(err, os.ErrNotExist) {
logrus.Info("Config file does not exist. Skipping")
} else {
logrus.Fatal("Unable to load config: ", err.Error())
}
}
logrus.Info("Loaded config from ", configFile)
}
var cfg Config
k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{Tag: "koanf"})
if viper.GetBool("debug") {
logrus.SetLevel(logrus.TraceLevel)
logrus.Debug("Running in debug mode")
}
db.Config = viper.Sub("db")
// TODO: Hack. flag bindings don't work in viper for nested keys
db.Config.BindPFlag("trace_queries", flags.Lookup("trace-queries"))
storage.Config = viper.Sub("storage")
storage.Config.BindPFlag("location", flags.Lookup("location"))
}
defer func() {
logrus.Debug("Shutting Down App")
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(
fs.SetupCommand(),
publink.SetupCommand(),
trash.SetupCommand(),
)
rootCmd.AddGroup(&cobra.Group{
ID: "server",
Title: "Server",
})
rootCmd.AddCommand(serve.SetupCommand())
rootCmd.Execute()
}