Improve initial loading

This commit is contained in:
Andrew-71 2024-10-13 16:38:13 +03:00
parent 1f50b8621e
commit acab4bc68b
8 changed files with 47 additions and 44 deletions

View file

@ -2,9 +2,9 @@ package cmd
import (
"fmt"
"log/slog"
"os"
"git.a71.su/Andrew71/pye/auth"
"git.a71.su/Andrew71/pye/config"
"git.a71.su/Andrew71/pye/logging"
"git.a71.su/Andrew71/pye/storage"
@ -26,22 +26,19 @@ var (
func initConfig() {
logging.LogInit(*debugMode)
if cfgFile != "" {
err := config.LoadConfig(cfgFile)
if err != nil {
slog.Error("error loading custom config", "error", err)
}
}
config.MustLoadConfig(cfgFile)
if cfgDb != "" {
config.Cfg.SQLiteFile = cfgDb
}
auth.MustLoadKey()
storage.Data = sqlite.MustLoadSQLite(config.Cfg.SQLiteFile)
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "config.json", "config file")
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.json", "config file")
rootCmd.PersistentFlags().StringVar(&cfgDb, "db", "", "database to use")
debugMode = rootCmd.PersistentFlags().BoolP("debug", "d", false, "enable debug mode")
}

View file

@ -18,19 +18,19 @@ var (
func init() {
verifyCmd.Flags().StringVarP(&verifyToken, "token", "t", "", "token to verify")
verifyCmd.MarkFlagRequired("token")
verifyCmd.Flags().StringVarP(&verifyFile, "file", "f", "", "file to use")
verifyCmd.Flags().StringVarP(&verifyFile, "file", "f", "", "PEM file to use")
rootCmd.AddCommand(verifyCmd)
}
var verifyCmd = &cobra.Command{
Use: "verify",
Short: "Verify a JWT token",
Long: `Pass a JWT token and a path to PEM-encoded file with a public key
to verify whether it is legit.`,
Long: `Pass a JWT token (and optionally a path to a PEM-formatted file with the public key)
to verify whether it is valid.`,
Run: verifyFunc,
}
// TODO: Better name.
// TODO: Needs a better name?
func verifyFunc(cmd *cobra.Command, args []string) {
if verifyToken == "" {
fmt.Println("Empty token supplied!")
@ -50,5 +50,10 @@ func verifyFunc(cmd *cobra.Command, args []string) {
}
t, err = auth.VerifyJWT(verifyToken, key)
}
slog.Info("result", "token", t, "error", err, "ok", err == nil)
slog.Debug("result", "token", t, "error", err, "ok", err == nil)
if err == nil {
fmt.Println("Token valid!")
} else {
fmt.Println("Token invalid!", err)
}
}