refactor(ax): rename workspace provider surface
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
619f731e5e
commit
9f0e155d62
4 changed files with 29 additions and 22 deletions
2
io.go
2
io.go
|
|
@ -128,7 +128,7 @@ func init() {
|
|||
var err error
|
||||
Local, err = local.New("/")
|
||||
if err != nil {
|
||||
core.Warn("io: failed to initialise Local medium, io.Local will be nil", "error", err)
|
||||
core.Warn("io.Local init failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
// Example: service, _ := workspace.New(workspace.Options{CryptProvider: cryptProvider})
|
||||
// Example: service, _ := workspace.New(workspace.Options{KeyPairProvider: keyPairProvider})
|
||||
// Example: workspaceID, _ := service.CreateWorkspace("alice", "pass123")
|
||||
// Example: _ = service.SwitchWorkspace(workspaceID)
|
||||
// Example: _ = service.WorkspaceFileSet("notes/todo.txt", "ship it")
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import (
|
|||
"dappco.re/go/core/io"
|
||||
)
|
||||
|
||||
// Example: service, _ := workspace.New(workspace.Options{CryptProvider: cryptProvider})
|
||||
// Example: service, _ := workspace.New(workspace.Options{KeyPairProvider: keyPairProvider})
|
||||
type Workspace interface {
|
||||
CreateWorkspace(identifier, password string) (string, error)
|
||||
SwitchWorkspace(workspaceID string) error
|
||||
|
|
@ -19,11 +19,13 @@ type Workspace interface {
|
|||
WorkspaceFileSet(workspaceFilePath, content string) error
|
||||
}
|
||||
|
||||
// Example: key, _ := cryptProvider.CreateKeyPair("alice", "pass123")
|
||||
type CryptProvider interface {
|
||||
// Example: key, _ := keyPairProvider.CreateKeyPair("alice", "pass123")
|
||||
type KeyPairProvider interface {
|
||||
CreateKeyPair(name, passphrase string) (string, error)
|
||||
}
|
||||
|
||||
type CryptProvider = KeyPairProvider
|
||||
|
||||
const (
|
||||
WorkspaceCreateAction = "workspace.create"
|
||||
WorkspaceSwitchAction = "workspace.switch"
|
||||
|
|
@ -37,14 +39,15 @@ type WorkspaceCommand struct {
|
|||
WorkspaceID string
|
||||
}
|
||||
|
||||
// Example: service, _ := workspace.New(workspace.Options{CryptProvider: cryptProvider})
|
||||
// Example: service, _ := workspace.New(workspace.Options{KeyPairProvider: keyPairProvider})
|
||||
type Options struct {
|
||||
CryptProvider CryptProvider
|
||||
KeyPairProvider KeyPairProvider
|
||||
CryptProvider CryptProvider
|
||||
}
|
||||
|
||||
// Example: service, _ := workspace.New(workspace.Options{CryptProvider: cryptProvider})
|
||||
// Example: service, _ := workspace.New(workspace.Options{KeyPairProvider: keyPairProvider})
|
||||
type Service struct {
|
||||
cryptProvider CryptProvider
|
||||
keyPairProvider KeyPairProvider
|
||||
activeWorkspaceID string
|
||||
rootPath string
|
||||
medium io.Medium
|
||||
|
|
@ -53,7 +56,7 @@ type Service struct {
|
|||
|
||||
var _ Workspace = (*Service)(nil)
|
||||
|
||||
// Example: service, _ := workspace.New(workspace.Options{CryptProvider: cryptProvider})
|
||||
// Example: service, _ := workspace.New(workspace.Options{KeyPairProvider: keyPairProvider})
|
||||
// Example: workspaceID, _ := service.CreateWorkspace("alice", "pass123")
|
||||
func New(options Options) (*Service, error) {
|
||||
home := resolveWorkspaceHomeDirectory()
|
||||
|
|
@ -62,14 +65,18 @@ func New(options Options) (*Service, error) {
|
|||
}
|
||||
rootPath := core.Path(home, ".core", "workspaces")
|
||||
|
||||
if options.CryptProvider == nil {
|
||||
return nil, core.E("workspace.New", "crypt provider is required", fs.ErrInvalid)
|
||||
keyPairProvider := options.KeyPairProvider
|
||||
if keyPairProvider == nil {
|
||||
keyPairProvider = options.CryptProvider
|
||||
}
|
||||
if keyPairProvider == nil {
|
||||
return nil, core.E("workspace.New", "key pair provider is required", fs.ErrInvalid)
|
||||
}
|
||||
|
||||
service := &Service{
|
||||
cryptProvider: options.CryptProvider,
|
||||
rootPath: rootPath,
|
||||
medium: io.Local,
|
||||
keyPairProvider: keyPairProvider,
|
||||
rootPath: rootPath,
|
||||
medium: io.Local,
|
||||
}
|
||||
|
||||
if err := service.medium.EnsureDir(rootPath); err != nil {
|
||||
|
|
@ -84,8 +91,8 @@ func (service *Service) CreateWorkspace(identifier, password string) (string, er
|
|||
service.stateLock.Lock()
|
||||
defer service.stateLock.Unlock()
|
||||
|
||||
if service.cryptProvider == nil {
|
||||
return "", core.E("workspace.CreateWorkspace", "crypt provider not available", nil)
|
||||
if service.keyPairProvider == nil {
|
||||
return "", core.E("workspace.CreateWorkspace", "key pair provider not available", nil)
|
||||
}
|
||||
|
||||
hash := sha256.Sum256([]byte(identifier))
|
||||
|
|
@ -105,7 +112,7 @@ func (service *Service) CreateWorkspace(identifier, password string) (string, er
|
|||
}
|
||||
}
|
||||
|
||||
privKey, err := service.cryptProvider.CreateKeyPair(identifier, password)
|
||||
privKey, err := service.keyPairProvider.CreateKeyPair(identifier, password)
|
||||
if err != nil {
|
||||
return "", core.E("workspace.CreateWorkspace", "failed to generate keys", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type stubCryptProvider struct {
|
||||
type stubKeyPairProvider struct {
|
||||
key string
|
||||
err error
|
||||
}
|
||||
|
||||
func (provider stubCryptProvider) CreateKeyPair(_, _ string) (string, error) {
|
||||
func (provider stubKeyPairProvider) CreateKeyPair(_, _ string) (string, error) {
|
||||
if provider.err != nil {
|
||||
return "", provider.err
|
||||
}
|
||||
|
|
@ -26,12 +26,12 @@ func newTestService(t *testing.T) (*Service, string) {
|
|||
tempHome := t.TempDir()
|
||||
t.Setenv("HOME", tempHome)
|
||||
|
||||
service, err := New(Options{CryptProvider: stubCryptProvider{key: "private-key"}})
|
||||
service, err := New(Options{KeyPairProvider: stubKeyPairProvider{key: "private-key"}})
|
||||
require.NoError(t, err)
|
||||
return service, tempHome
|
||||
}
|
||||
|
||||
func TestService_New_MissingCryptProvider_Bad(t *testing.T) {
|
||||
func TestService_New_MissingKeyPairProvider_Bad(t *testing.T) {
|
||||
_, err := New(Options{})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue