Skip to content

Commit 5fc7cdb

Browse files
committed
use an interface for credential store
Signed-off-by: Grant Linville <[email protected]>
1 parent b41c58f commit 5fc7cdb

File tree

9 files changed

+47
-17
lines changed

9 files changed

+47
-17
lines changed

pkg/credentials/noop.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package credentials
2+
3+
type NoopStore struct{}
4+
5+
func (s NoopStore) Get(_ string) (*Credential, bool, error) {
6+
return nil, false, nil
7+
}
8+
9+
func (s NoopStore) Add(_ Credential) error {
10+
return nil
11+
}
12+
13+
func (s NoopStore) Remove(_ string) error {
14+
return nil
15+
}
16+
17+
func (s NoopStore) List() ([]Credential, error) {
18+
return nil, nil
19+
}

pkg/credentials/store.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,31 @@ import (
1010
"github.com/gptscript-ai/gptscript/pkg/config"
1111
)
1212

13+
type CredentialStore interface {
14+
Get(toolName string) (*Credential, bool, error)
15+
Add(cred Credential) error
16+
Remove(toolName string) error
17+
List() ([]Credential, error)
18+
}
19+
1320
type Store struct {
1421
credCtx string
1522
credHelperDirs CredentialHelperDirs
1623
cfg *config.CLIConfig
1724
}
1825

19-
func NewStore(cfg *config.CLIConfig, credCtx, cacheDir string) (*Store, error) {
26+
func NewStore(cfg *config.CLIConfig, credCtx, cacheDir string) (CredentialStore, error) {
2027
if err := validateCredentialCtx(credCtx); err != nil {
2128
return nil, err
2229
}
23-
return &Store{
30+
return Store{
2431
credCtx: credCtx,
2532
credHelperDirs: GetCredentialHelperDirs(cacheDir),
2633
cfg: cfg,
2734
}, nil
2835
}
2936

30-
func (s *Store) Get(toolName string) (*Credential, bool, error) {
37+
func (s Store) Get(toolName string) (*Credential, bool, error) {
3138
store, err := s.getStore()
3239
if err != nil {
3340
return nil, false, err
@@ -50,7 +57,7 @@ func (s *Store) Get(toolName string) (*Credential, bool, error) {
5057
return &cred, true, nil
5158
}
5259

53-
func (s *Store) Add(cred Credential) error {
60+
func (s Store) Add(cred Credential) error {
5461
cred.Context = s.credCtx
5562
store, err := s.getStore()
5663
if err != nil {
@@ -63,15 +70,15 @@ func (s *Store) Add(cred Credential) error {
6370
return store.Store(auth)
6471
}
6572

66-
func (s *Store) Remove(toolName string) error {
73+
func (s Store) Remove(toolName string) error {
6774
store, err := s.getStore()
6875
if err != nil {
6976
return err
7077
}
7178
return store.Erase(toolNameWithCtx(toolName, s.credCtx))
7279
}
7380

74-
func (s *Store) List() ([]Credential, error) {
81+
func (s Store) List() ([]Credential, error) {
7582
store, err := s.getStore()
7683
if err != nil {
7784
return nil, err

pkg/openai/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type Client struct {
4444
cacheKeyBase string
4545
setSeed bool
4646
envs []string
47-
credStore *credentials.Store
47+
credStore credentials.CredentialStore
4848
}
4949

5050
type Options struct {
@@ -86,7 +86,7 @@ func complete(opts ...Options) (result Options, err error) {
8686
return result, err
8787
}
8888

89-
func NewClient(credStore *credentials.Store, opts ...Options) (*Client, error) {
89+
func NewClient(credStore credentials.CredentialStore, opts ...Options) (*Client, error) {
9090
opt, err := complete(opts...)
9191
if err != nil {
9292
return nil, err

pkg/prompt/credential.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/tidwall/gjson"
99
)
1010

11-
func GetModelProviderCredential(ctx context.Context, credStore *credentials.Store, credName, env, message string, envs []string) (string, error) {
11+
func GetModelProviderCredential(ctx context.Context, credStore credentials.CredentialStore, credName, env, message string, envs []string) (string, error) {
1212
cred, exists, err := credStore.Get(credName)
1313
if err != nil {
1414
return "", err

pkg/remote/remote.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ type Client struct {
2727
models map[string]*openai.Client
2828
runner *runner.Runner
2929
envs []string
30-
credStore *credentials.Store
30+
credStore credentials.CredentialStore
3131
}
3232

33-
func New(r *runner.Runner, envs []string, cache *cache.Client, credStore *credentials.Store) *Client {
33+
func New(r *runner.Runner, envs []string, cache *cache.Client, credStore credentials.CredentialStore) *Client {
3434
return &Client{
3535
cache: cache,
3636
runner: r,

pkg/repos/get.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ func New(cacheDir string, runtimes ...Runtime) *Manager {
6262
}
6363

6464
func (m *Manager) SetUpCredentialHelpers(ctx context.Context, cliCfg *config.CLIConfig, env []string) error {
65-
helperName := cliCfg.CredentialsStore
66-
suffix := ""
65+
var (
66+
helperName = cliCfg.CredentialsStore
67+
suffix string
68+
)
6769
if helperName == "wincred" {
6870
suffix = ".exe"
6971
}

pkg/repos/runtimes/golang/golang.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (r *Runtime) BuildCredentialHelper(ctx context.Context, helperName string,
5757
return nil
5858
}
5959

60-
suffix := ""
60+
var suffix string
6161
if helperName == "wincred" {
6262
suffix = ".exe"
6363
}

pkg/runner/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ type Runner struct {
8686
runtimeManager engine.RuntimeManager
8787
credMutex sync.Mutex
8888
credOverrides string
89-
credStore *credentials.Store
89+
credStore credentials.CredentialStore
9090
sequential bool
9191
}
9292

93-
func New(client engine.Model, credStore *credentials.Store, opts ...Options) (*Runner, error) {
93+
func New(client engine.Model, credStore credentials.CredentialStore, opts ...Options) (*Runner, error) {
9494
opt := complete(opts...)
9595

9696
runner := &Runner{

pkg/tests/tester/runner.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"testing"
1010

11+
"github.com/gptscript-ai/gptscript/pkg/credentials"
1112
"github.com/gptscript-ai/gptscript/pkg/loader"
1213
"github.com/gptscript-ai/gptscript/pkg/runner"
1314
"github.com/gptscript-ai/gptscript/pkg/types"
@@ -157,7 +158,8 @@ func NewRunner(t *testing.T) *Runner {
157158
t: t,
158159
}
159160

160-
run, err := runner.New(c, nil, runner.Options{
161+
var noopStore credentials.CredentialStore = credentials.NoopStore{}
162+
run, err := runner.New(c, &noopStore, runner.Options{
161163
Sequential: true,
162164
})
163165
require.NoError(t, err)

0 commit comments

Comments
 (0)