Skip to content

Commit

Permalink
cli: Use state package to handle authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspcr committed Jan 25, 2025
1 parent 8b53aaf commit b76e215
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
11 changes: 9 additions & 2 deletions cmd/decli/internal/commands/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 }
4 changes: 2 additions & 2 deletions cmd/decli/internal/commands/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
27 changes: 23 additions & 4 deletions cmd/decli/internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -20,6 +22,7 @@ var (
memProfileFile string
cpuProfileFile string
cfg *config.Config
db state.Operations
)

// Executes the CLI.
Expand All @@ -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),
Expand All @@ -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:",
Expand All @@ -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)

Expand Down Expand Up @@ -142,7 +164,4 @@ func initConfig() {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
cobra.CheckErr(err)
}

// Setup config for isolated configurations.
authcmd.SetupConfig(cfg)
}

0 comments on commit b76e215

Please sign in to comment.