forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential_show.go
77 lines (62 loc) · 1.87 KB
/
credential_show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cli
import (
"fmt"
"os"
"text/tabwriter"
"github.com/gptscript-ai/gptscript/pkg/cache"
"github.com/gptscript-ai/gptscript/pkg/config"
"github.com/gptscript-ai/gptscript/pkg/credentials"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes"
"github.com/gptscript-ai/gptscript/pkg/runner"
"github.com/spf13/cobra"
)
type Show struct {
root *GPTScript
}
func (c *Show) Customize(cmd *cobra.Command) {
cmd.Use = "show <credential name>"
cmd.Aliases = []string{"reveal"}
cmd.SilenceUsage = true
cmd.Short = "Show the secret value of a stored credential"
cmd.Args = cobra.ExactArgs(1)
}
func (c *Show) Run(cmd *cobra.Command, args []string) error {
opts, err := c.root.NewGPTScriptOpts()
if err != nil {
return err
}
cfg, err := config.ReadCLIConfig(c.root.ConfigFile)
if err != nil {
return fmt.Errorf("failed to read CLI config: %w", err)
}
opts.Cache = cache.Complete(opts.Cache)
opts.Runner = runner.Complete(opts.Runner)
if opts.Runner.RuntimeManager == nil {
opts.Runner.RuntimeManager = runtimes.Default(opts.Cache.CacheDir)
}
credCtx := c.root.CredentialContext
if len(credCtx) == 0 {
credCtx = []string{credentials.DefaultCredentialContext}
}
if err = opts.Runner.RuntimeManager.SetUpCredentialHelpers(cmd.Context(), cfg); err != nil {
return err
}
store, err := credentials.NewStore(cfg, opts.Runner.RuntimeManager, credCtx, opts.Cache.CacheDir)
if err != nil {
return fmt.Errorf("failed to get credentials store: %w", err)
}
cred, exists, err := store.Get(cmd.Context(), args[0])
if err != nil {
return fmt.Errorf("failed to get credential: %w", err)
}
if !exists {
return fmt.Errorf("credential %q not found", args[0])
}
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
defer w.Flush()
_, _ = w.Write([]byte("ENV\tVALUE\n"))
for env, val := range cred.Env {
_, _ = fmt.Fprintf(w, "%s\t%s\n", env, val)
}
return nil
}