-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Add state package with sqlite implementation
- Loading branch information
1 parent
23f4222
commit 65dd32d
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |