Skip to content

Commit

Permalink
cli: Add state package with sqlite implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspcr committed Jan 25, 2025
1 parent 23f4222 commit 65dd32d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/decli/internal/state/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package state

// Operations related to the State of the CLI application.g
type Operations interface {
AuthTokenOperations
}

// AuthTokenOperations is the set of operations related to the Authentication
// token received by the login command in the CLI.
type AuthTokenOperations interface {
// GetAuthToken gets the latest authentication token from the state store.
GetAuthToken() (string, error)
// InvalidateAuthToken invalidates the latest authentication token.
InvalidateAuthToken() error
// SaveAuthToken saves the authentication token as the latest token.
SaveAuthToken(string) error
}
39 changes: 39 additions & 0 deletions cmd/decli/internal/state/sqlite/auth_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sqlite

import (
"time"

"gorm.io/gorm"
)

type authTokenModel struct {
Token string `gorm:"primaryKey"`
CreatedAt time.Time
Deleted bool
}

type authTokenStore struct{ *gorm.DB }

// GetAuthToken gets the latest authentication token from the state store.
func (st *authTokenStore) GetAuthToken() (string, error) {
var model authTokenModel

res := st.Order("created_at DESC").First(&model)
if res.Error != nil {
return "", res.Error
}
return model.Token, nil
}

// InvalidateAuthToken invalidates the latest authentication token.
func (st *authTokenStore) InvalidateAuthToken() error {
res := st.Where("deleted = ?", false).Update("deleted", true)
return res.Error
}

// SaveAuthToken saves the authentication token as the latest token.
func (st *authTokenStore) SaveAuthToken(token string) error {
model := authTokenModel{Token: token, CreatedAt: time.Now()}
res := st.Create(&model)
return res.Error
}
8 changes: 8 additions & 0 deletions cmd/decli/internal/state/sqlite/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sqlite

type Config struct {
// Provider selects either 'memory' or 'file' as the storage location.
Provider string
// Filepath of file in which data will be saved.
Filepath string
}
42 changes: 42 additions & 0 deletions cmd/decli/internal/state/sqlite/sqlite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Package sqlite implements the methods defined in the CLI state interface.
package sqlite

import (
"context"
"errors"

"github.com/glebarez/sqlite"
"github.com/nicholaspcr/GoDE/cmd/decli/internal/state"
"gorm.io/gorm"
)

type store struct {
*authTokenStore
}

// New returns a new store that handles state operations for the CLI.
func New(ctx context.Context, cfg Config) (state.Operations, error) {
var dialector gorm.Dialector

switch cfg.Provider {
case "memory":
dialector = sqlite.Open(":memory:")
case "file":
dialector = sqlite.Open(cfg.Filepath)
default:
return nil, errors.New("invalid store type")
}

db, err := gorm.Open(dialector)
if err != nil {
return nil, err
}

if err := db.AutoMigrate(&authTokenModel{}); err != nil {
return nil, err
}

return &store{
authTokenStore: &authTokenStore{DB: db},
}, nil
}
8 changes: 8 additions & 0 deletions cmd/decli/internal/state/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Package state manages the state of the CLI.
package state

// State of the CLI application.
type State struct {
// AuthToken is the token received in the authentication operation.
AuthToken string
}

0 comments on commit 65dd32d

Please sign in to comment.