-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ce687f
commit 44aecaa
Showing
20 changed files
with
270 additions
and
420 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
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
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
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
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,77 @@ | ||
package credentials | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/docker/docker-credential-helpers/client" | ||
"github.com/gptscript-ai/gptscript/pkg/config" | ||
"github.com/gptscript-ai/gptscript/pkg/types" | ||
) | ||
|
||
type ProgramLoaderRunner interface { | ||
Load(ctx context.Context, toolName string) (prg types.Program, err error) | ||
Run(ctx context.Context, prg types.Program, input string) (output string, err error) | ||
} | ||
|
||
func NewFactory(ctx context.Context, cfg *config.CLIConfig, plr ProgramLoaderRunner) (StoreFactory, error) { | ||
toolName := translateToolName(cfg.CredentialsStore) | ||
if toolName == config.FileCredHelper { | ||
return StoreFactory{ | ||
file: true, | ||
cfg: cfg, | ||
}, nil | ||
} | ||
|
||
prg, err := plr.Load(ctx, toolName) | ||
if err != nil { | ||
return StoreFactory{}, err | ||
} | ||
|
||
return StoreFactory{ | ||
ctx: ctx, | ||
prg: prg, | ||
runner: plr, | ||
cfg: cfg, | ||
}, nil | ||
} | ||
|
||
type StoreFactory struct { | ||
ctx context.Context | ||
prg types.Program | ||
file bool | ||
runner ProgramLoaderRunner | ||
cfg *config.CLIConfig | ||
} | ||
|
||
func (s *StoreFactory) NewStore(credCtxs []string) (CredentialStore, error) { | ||
if err := validateCredentialCtx(credCtxs); err != nil { | ||
return nil, err | ||
} | ||
if s.file { | ||
return Store{ | ||
credCtxs: credCtxs, | ||
cfg: s.cfg, | ||
}, nil | ||
} | ||
return Store{ | ||
credCtxs: credCtxs, | ||
cfg: s.cfg, | ||
program: s.program, | ||
}, nil | ||
} | ||
|
||
func (s *StoreFactory) program(args ...string) client.Program { | ||
return &runnerProgram{ | ||
factory: s, | ||
action: args[0], | ||
} | ||
} | ||
|
||
func translateToolName(toolName string) string { | ||
for _, helper := range config.Helpers { | ||
if helper == toolName { | ||
return "github.com/gptscript-ai/gptscript-credential-helpers/" + toolName + "/cmd" | ||
} | ||
} | ||
return toolName | ||
} |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package credentials | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
) | ||
|
||
type NoopStore struct{} | ||
|
||
|
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,29 @@ | ||
package credentials | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type runnerProgram struct { | ||
factory *StoreFactory | ||
action string | ||
output string | ||
err error | ||
} | ||
|
||
func (r *runnerProgram) Output() ([]byte, error) { | ||
return []byte(r.output), r.err | ||
} | ||
|
||
func (r *runnerProgram) Input(in io.Reader) { | ||
input, err := io.ReadAll(in) | ||
if err != nil { | ||
r.err = err | ||
return | ||
} | ||
|
||
prg := r.factory.prg | ||
prg.EntryToolID = prg.ToolSet[prg.EntryToolID].LocalTools[r.action] | ||
|
||
r.output, r.err = r.factory.runner.Run(r.factory.ctx, prg, string(input)) | ||
} |
Oops, something went wrong.