2024-10-13 21:03:44 +03:00
|
|
|
package app
|
2024-10-13 16:16:19 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2024-10-13 21:03:44 +03:00
|
|
|
"git.a71.su/Andrew71/pye/internal/auth"
|
|
|
|
"git.a71.su/Andrew71/pye/internal/config"
|
|
|
|
"git.a71.su/Andrew71/pye/internal/logging"
|
|
|
|
"git.a71.su/Andrew71/pye/internal/storage"
|
|
|
|
"git.a71.su/Andrew71/pye/internal/storage/sqlite"
|
2024-10-13 16:16:19 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "pye",
|
|
|
|
Short: "Pye is a simple JWT system",
|
2024-10-13 21:26:03 +03:00
|
|
|
Long: `An HTTP JSON Web Token authentication system`,
|
2024-10-13 16:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgFile string
|
|
|
|
cfgDb string
|
|
|
|
debugMode *bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func initConfig() {
|
2024-10-13 17:18:53 +03:00
|
|
|
logging.Load(*debugMode)
|
|
|
|
config.MustLoad(cfgFile)
|
2024-10-13 16:16:19 +03:00
|
|
|
if cfgDb != "" {
|
|
|
|
config.Cfg.SQLiteFile = cfgDb
|
|
|
|
}
|
|
|
|
|
2024-10-13 16:38:13 +03:00
|
|
|
auth.MustLoadKey()
|
2024-10-13 17:18:53 +03:00
|
|
|
storage.Data = sqlite.MustLoad(config.Cfg.SQLiteFile)
|
2024-10-13 16:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
2024-10-13 16:38:13 +03:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.json", "config file")
|
2024-10-13 16:16:19 +03:00
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgDb, "db", "", "database to use")
|
|
|
|
debugMode = rootCmd.PersistentFlags().BoolP("debug", "d", false, "enable debug mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|