From b76e215dcdd1241699f28e75edb2f4e454e4403c Mon Sep 17 00:00:00 2001 From: Nicholas Cristofaro Date: Sat, 25 Jan 2025 00:27:09 -0300 Subject: [PATCH] cli: Use state package to handle authentication --- cmd/decli/internal/commands/auth/auth.go | 11 +++++++-- cmd/decli/internal/commands/auth/login.go | 4 ++-- cmd/decli/internal/commands/root.go | 27 +++++++++++++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd/decli/internal/commands/auth/auth.go b/cmd/decli/internal/commands/auth/auth.go index c067dbf..e12cb21 100644 --- a/cmd/decli/internal/commands/auth/auth.go +++ b/cmd/decli/internal/commands/auth/auth.go @@ -2,10 +2,14 @@ package authcmd import ( "github.com/nicholaspcr/GoDE/cmd/decli/internal/config" + "github.com/nicholaspcr/GoDE/cmd/decli/internal/state" "github.com/spf13/cobra" ) -var cfg *config.Config +var ( + cfg *config.Config + db state.Operations +) // authCmd encapsulates the authentication operations. var authCmd = &cobra.Command{ @@ -17,5 +21,8 @@ var authCmd = &cobra.Command{ // RegisterCommands adds the subset of commands into the provided cobra.Command func RegisterCommands(root *cobra.Command) { root.AddCommand(authCmd) } -// SetupConfig sets the config of this package to be the same as the geral config. +// SetupConfig sets the config of this package. func SetupConfig(rootCfg *config.Config) { cfg = rootCfg } + +// SetupStateHandler sets the state handler of this package +func SetupStateHandler(rootDB state.Operations) { db = rootDB } diff --git a/cmd/decli/internal/commands/auth/login.go b/cmd/decli/internal/commands/auth/login.go index e92df97..f0e7a2f 100644 --- a/cmd/decli/internal/commands/auth/login.go +++ b/cmd/decli/internal/commands/auth/login.go @@ -59,8 +59,8 @@ var loginCmd = &cobra.Command{ return err } - slog.Info("Logged in successfully", slog.String("token", resp.Token)) - return nil + slog.Debug("Logged in successfully", slog.String("token", resp.Token)) + return db.SaveAuthToken(resp.Token) }, } diff --git a/cmd/decli/internal/commands/root.go b/cmd/decli/internal/commands/root.go index 956555a..d5f95e4 100644 --- a/cmd/decli/internal/commands/root.go +++ b/cmd/decli/internal/commands/root.go @@ -11,6 +11,8 @@ import ( authcmd "github.com/nicholaspcr/GoDE/cmd/decli/internal/commands/auth" "github.com/nicholaspcr/GoDE/cmd/decli/internal/config" + "github.com/nicholaspcr/GoDE/cmd/decli/internal/state" + "github.com/nicholaspcr/GoDE/cmd/decli/internal/state/sqlite" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -20,6 +22,7 @@ var ( memProfileFile string cpuProfileFile string cfg *config.Config + db state.Operations ) // Executes the CLI. @@ -40,6 +43,8 @@ allows the usage of the algorithm locally and the ability to connect to a server. `, PersistentPreRunE: func(cmd *cobra.Command, _ []string) (err error) { + ctx := cmd.Context() + logOpts := []log.Option{ log.WithType(cfg.Log.Type), log.WithLevel(cfg.Log.Level), @@ -55,11 +60,22 @@ server. logger := log.New(logOpts...) slog.SetDefault(logger) + db, err = sqlite.New(ctx, cfg.Sqlite) + if err != nil { + return err + } + cpuProfile, err := os.Create(cpuProfileFile) if err != nil { return err } - return pprof.StartCPUProfile(cpuProfile) + if err := pprof.StartCPUProfile(cpuProfile); err != nil { + return err + } + + // NOTE: this function call has to be on the end of the PersistentPreRun. + setupCommands() + return nil }, RunE: func(cmd *cobra.Command, _ []string) error { slog.Debug("Initialization of CLI:", @@ -79,6 +95,12 @@ server. }, } +// Sets the config and state handler for isolated command packages. +func setupCommands() { + authcmd.SetupConfig(cfg) + authcmd.SetupStateHandler(db) +} + func init() { cobra.OnInitialize(initConfig) @@ -142,7 +164,4 @@ func initConfig() { if _, ok := err.(viper.ConfigFileNotFoundError); !ok { cobra.CheckErr(err) } - - // Setup config for isolated configurations. - authcmd.SetupConfig(cfg) }